# Spring 2/24上課內容
###### tags: `Spring`
# 32 Spring動態網站開發

```clike=
package tw.leonchen.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoStringAction {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
String value1 = context.getBean("string0", String.class);
System.out.println("value1:"+ value1);
String value2 = context.getBean("string1", String.class);
System.out.println("value2:"+value2);
((ConfigurableApplicationContext)context).close();
}
}
```
# 33 Spring動態網站開發

```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
</beans>
```

---

* **FullyQualifiedName**
* 
# 34 Spring動態網站開發

---
* property 參考bean id or value
* 
# 補昨天進度 4-1


```clike=
package tw.leonchen.model;
public class LoginDao {
public boolean checkLogin(String user, String pwd) {
if("john".equalsIgnoreCase(user) && "test123".equalsIgnoreCase(pwd)) {
return true;
}
return false;
}
}
```
代建構子
不代參數建構子

* **建構子**
* 定義:任何class一定有建構子

```clike=
package tw.leonchen.model;
public class LoginService {
private LoginDao loginDao;
public LoginService() {
}
public LoginService(LoginDao loginDao) {
this.loginDao = loginDao;
}
public boolean checkLogin(String user, String pwd) {
return loginDao.checkLogin(user, pwd);
}
}
```
# 35 Spring動態網站開發

```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
</beans>
```
---


```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.LoginDao;
public class DemoLoginAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
LoginDao myLoginDao = context.getBean("loginDao", LoginDao.class);
boolean result1 = myLoginDao.checkLogin("alice", "123Test");
System.out.println("result1:" + result1);
boolean result2 = myLoginDao.checkLogin("john", "test123");
System.out.println("result2:" + result2);
context.close();
}
}
```

```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.LoginDao;
import tw.leonchen.model.LoginService;
public class DemoLoginAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
LoginDao myLoginDao = context.getBean("loginDao", LoginDao.class);
boolean result1 = myLoginDao.checkLogin("alice", "123Test");
System.out.println("result1:" + result1);
boolean result2 = myLoginDao.checkLogin("john", "test123");
System.out.println("result2:" + result2);
LoginService loginService2 = context.getBean("loginService2", LoginService.class);
boolean result3 = loginService2.checkLogin("mary", "123test123");
System.out.println("result3:" + result3);
context.close();
}
}
```


* name屬性名子
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
</beans>
```
## DemoLoginAction
```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.LoginDao;
import tw.leonchen.model.LoginService;
public class DemoLoginAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
LoginDao myLoginDao = context.getBean("loginDao", LoginDao.class);
boolean result1 = myLoginDao.checkLogin("alice", "123Test");
System.out.println("result1:" + result1);
boolean result2 = myLoginDao.checkLogin("john", "test123");
System.out.println("result2:" + result2);
LoginService loginService2 = context.getBean("loginService2", LoginService.class);
boolean result3 = loginService2.checkLogin("mary", "123test123");
System.out.println("result3:" + result3);
LoginService loginService1 = context.getBean("loginService1", LoginService.class);
boolean result4 = loginService1.checkLogin("john", "test123");
System.out.println("result4:" + result4);
context.close();
}
}
```
# 36 Spring動態網站開發(透過Setter/Constructor注入各種不同型態的資料)

* **不帶參數建構子**
* 
# 37 Spring動態網站開發

---

一定要有不帶參數建構子

不要大寫

# 38 Spring動態網站開發

---

# 39 Spring動態網站開發

---

# 40 Spring動態網站開發

---

# 41 Spring動態網站開發

---

# 42 Spring動態網站開發


[Date api](https://docs.oracle.com/javase/8/docs/api/)
## DateDao

```clike=
package tw.leonchen.model;
import java.util.Date;
public class DateDao {
private Date date;
public DateDao(Date date) {
this.date = date;
}
public Date getDate() {
return date;
}
}
```
## bean
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<!-- alt+/ -->
<constructor-arg ref="myDate"/>
</bean>
</beans>
```
## DemoDateAction

```clike=
package tw.leonchen.action;
import java.util.Date;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.DateDao;
public class DemoDateAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
DateDao dDao = context.getBean("dateDao", DateDao.class);
Date date = dDao.getDate();
System.out.println("date:" + date);
context.close();
}
}
```
# 42 Spring動態網站開發

* set 不重複 沒順序
* list 按照順序 可重複
* **LinkedHashSet**
* 不可重複 按照順序放入
* LinkedList 按照順序 可重複
# 43 Spring動態網站開發

---

## DateDao


```clike=
package tw.leonchen.model;
import java.util.Date;
import java.util.Map;
public class DateDao {
private Date date;
private Map<String, String> map;
public DateDao(Date date) {
this.date = date;
}
public DateDao(Map<String, String> map) {
this.map = map;
}
public Date getDate() {
return date;
}
public Map<String, String> getMap() {
return map;
}
}
```
## bean
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
</beans>
```
## DemoDateAction
```clike=
package tw.leonchen.action;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.DateDao;
public class DemoDateAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
DateDao dDao = context.getBean("dateDao", DateDao.class);
Date date = dDao.getDate();
System.out.println("date:" + date);
DateDao dDaoMap =context.getBean("dateDaoMap", DateDao.class);
Map<String, String> map = dDaoMap.getMap();
Set<String> keys = map.keySet();
Collection<String> values = map.values();
Set<Entry<String, String>> mapping = map.entrySet();
System.out.println("keys:" + keys);
System.out.println("values:" + values);
System.out.println("mapping:" + mapping);
System.out.println("fruit1 Value:" + map.get("fruit1"));
Iterator<String> i1 = keys.iterator();
while(i1.hasNext()) {
String key = i1.next();
String value = map.get(key);
System.out.println(key + ":" + value);
}
context.close();
}
}
```
# 44 Spring動態網站開發

---



# 45 Spring動態網站開發

## DateDao
```clike=
package tw.leonchen.model;
import java.util.Date;
import java.util.Map;
public class DateDao {
private Date date;
private Map<String, String> map;
private String msg;
public DateDao() {
}
public DateDao(Date date) {
this.date = date;
}
public DateDao(Map<String, String> map) {
this.map = map;
}
public Date getDate() {
return date;
}
public Map<String, String> getMap() {
return map;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
```
## beans
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
</beans>
```
## DemoDateAction
```clike=
package tw.leonchen.action;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.DateDao;
public class DemoDateAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
DateDao dDao = context.getBean("dateDao", DateDao.class);
Date date = dDao.getDate();
System.out.println("date:" + date);
DateDao dDaoMap =context.getBean("dateDaoMap", DateDao.class);
Map<String, String> map = dDaoMap.getMap();
Set<String> keys = map.keySet();
Collection<String> values = map.values();
Set<Entry<String, String>> mapping = map.entrySet();
System.out.println("keys:" + keys);
System.out.println("values:" + values);
System.out.println("mapping:" + mapping);
System.out.println("fruit1 Value:" + map.get("fruit1"));
Iterator<String> i1 = keys.iterator();
while(i1.hasNext()) {
String key = i1.next();
String value = map.get(key);
System.out.println(key + ":" + value);
}
DateDao dDaoMsg1 = context.getBean("dateDaoMsg1", DateDao.class);
String msg1 = dDaoMsg1.getMsg();
System.out.println("msg1:" + msg1);
DateDao dDaoMsg2 = context.getBean("dateDaoMsg2", DateDao.class);
String msg2 = dDaoMsg2.getMsg();
System.out.println("msg2:" + msg2);
context.close();
}
}
```
# 46 Spring動態網站開發(Spring產生物件的方式)

---

# 47 Spring動態網站開發

## TruckBean

```clike=
package tw.leonchen.model;
public class TruckBean {
private int id;
private String brand;
public TruckBean() {
}
public TruckBean(int id, String brand) {
this.id = id;
this.brand = brand;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
```
## beans
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
</beans>
```
## DemoTruckBeanAction

```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.TruckBean;
public class DemoTruckBeanAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
TruckBean tBean1 = context.getBean("truckBean", TruckBean.class);
int id = tBean1.getId();
String brand = tBean1.getBrand();
System.out.println("id:" + id);
System.out.println("brand:" + brand);
context.close();
}
}
```
# 47 Spring動態網站開發

## TruckBeanStaticFactory

```clike=
package tw.leonchen.model;
import java.util.HashMap;
public class TruckBeanStaticFactory {
private static HashMap<Integer, TruckBean> map = new HashMap<Integer, TruckBean>();
static {
map.put(1, new TruckBean(3, "ford"));
map.put(2, new TruckBean(4,"tesla"));
}
public static TruckBean getTruckBean(int id) {
return map.get(id);
}
}
```
# 47.48 Spring動態網站開發


## beans
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
<bean id="fordTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="teslaTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
</beans>
```
# 48 Spring動態網站開發

## DemoTruckBeanAction
```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.TruckBean;
public class DemoTruckBeanAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
TruckBean tBean1 = context.getBean("truckBean", TruckBean.class);
int id = tBean1.getId();
String brand = tBean1.getBrand();
System.out.println("id:" + id);
System.out.println("brand:" + brand);
TruckBean fordTruck = context.getBean("fordTruck", TruckBean.class);
System.out.println("ID:" + fordTruck.getId());
System.out.println("Brand:" + fordTruck.getBrand());
TruckBean teslaTruck = context.getBean("teslaTruck", TruckBean.class);
System.out.println("ID:" + teslaTruck.getId());
System.out.println("Brand:" + teslaTruck.getBrand());
context.close();
}
}
```
# 49 Spring動態網站開發


## TruckBeanFactory

```clike=
package tw.leonchen.model;
import java.util.HashMap;
public class TruckBeanFactory {
private HashMap<Integer, TruckBean> map = new HashMap<Integer, TruckBean>();
public void setMap(HashMap<Integer, TruckBean> map) {
this.map = map;
}
public TruckBean getTruckBean(int id) {
return map.get(id);
}
}
```
## beans
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
<bean id="fordTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="teslaTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
<bean id="truckBeanFactory" class="tw.leonchen.model.TruckBeanFactory">
<property name="map">
<map>
<entry key="1">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="5"/>
<property name="brand" value="bmw"/>
</bean>
</entry>
<entry key="2">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="6"/>
<property name="brand" value="benz"/>
</bean>
</entry>
</map>
</property>
</bean>
</beans>
```
# 50 Spring動態網站開發

## beans
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
<bean id="fordTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="teslaTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
<bean id="truckBeanFactory" class="tw.leonchen.model.TruckBeanFactory">
<property name="map">
<map>
<entry key="1">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="5"/>
<property name="brand" value="bmw"/>
</bean>
</entry>
<entry key="2">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="6"/>
<property name="brand" value="benz"/>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="bmwTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="benzTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
</beans>
```
## DemoTruckBeanAction
```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.TruckBean;
public class DemoTruckBeanAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
TruckBean tBean1 = context.getBean("truckBean", TruckBean.class);
int id = tBean1.getId();
String brand = tBean1.getBrand();
System.out.println("id:" + id);
System.out.println("brand:" + brand);
TruckBean fordTruck = context.getBean("fordTruck", TruckBean.class);
System.out.println("ID:" + fordTruck.getId());
System.out.println("Brand:" + fordTruck.getBrand());
TruckBean teslaTruck = context.getBean("teslaTruck", TruckBean.class);
System.out.println("ID:" + teslaTruck.getId());
System.out.println("Brand:" + teslaTruck.getBrand());
TruckBean benzTruck = context.getBean("benzTruck", TruckBean.class);
System.out.println("ID:" + benzTruck.getId());
System.out.println("Brand:" + benzTruck.getBrand());
context.close();
}
}
```
# 51 Spring動態網站開發(使用註釋(Annotation)說明Bean的組裝資訊)

# 52 Spring動態網站開發


## beans


```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="tw.leonchen"/>
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
<bean id="fordTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="teslaTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
<bean id="truckBeanFactory" class="tw.leonchen.model.TruckBeanFactory">
<property name="map">
<map>
<entry key="1">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="5"/>
<property name="brand" value="bmw"/>
</bean>
</entry>
<entry key="2">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="6"/>
<property name="brand" value="benz"/>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="bmwTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="benzTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
</beans>
```
# 53 Spring動態網站開發

---

* @Component:我
*
# 54 Spring動態網站開發

---

# 55 Spring動態網站開發

---

# 56 Spring動態網站開發

## Animal

```clike=
package tw.leonchen.model;
import org.springframework.stereotype.Component;
@Component(value="animal")
public class Animal {
private int id;
private String name;
private String continent;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
}
```
## DemoAnimalAction

```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.Animal;
public class DemoAnimalAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
Animal myAnimal = context.getBean("animal", Animal.class);
myAnimal.setId(1000);
myAnimal.setName("Tiger");
myAnimal.setContinent("ASIA");
System.out.println("ID:" + myAnimal.getId());
System.out.println("Name:" + myAnimal.getName());
System.out.println("Continent:" + myAnimal.getContinent());
context.close();
}
}
```
# 57 Spring動態網站開發(@Autowired)

---

# 58 Spring動態網站開發

---

# 59 Spring動態網站開發

## AnimalDao
* @AutoWired:讓程式自己找

```clike=
package tw.leonchen.model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository(value = "animalDao")
public class AnimalDao {
@Autowired
private Animal animal;
public void showDetails() {
System.out.println("id:" + animal.getId());
System.out.println("name:" + animal.getName());
System.out.println("continent:" + animal.getContinent());
}
}
```
## DemoAnimalAction
```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.Animal;
import tw.leonchen.model.AnimalDao;
public class DemoAnimalAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
Animal myAnimal = context.getBean("animal", Animal.class);
myAnimal.setId(1000);
myAnimal.setName("Tiger");
myAnimal.setContinent("ASIA");
System.out.println("ID1:" + myAnimal.getId());
System.out.println("Name1:" + myAnimal.getName());
System.out.println("Continent1:" + myAnimal.getContinent());
AnimalDao aDao = context.getBean("animalDao", AnimalDao.class);
aDao.showDetails();
context.close();
}
}
```
## beans
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="tw.leonchen"/>
<bean id="animal1" class="tw.leonchen.model.Animal">
<property name="id" value="1001"/>
<property name="name" value="camel"/>
<property name="continent" value="africa"/>
</bean>
<bean id="animal2" class="tw.leonchen.model.Animal">
<property name="id" value="1002"/>
<property name="name" value="panda"/>
<property name="continent" value="asia"/>
</bean>
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
<bean id="fordTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="teslaTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
<bean id="truckBeanFactory" class="tw.leonchen.model.TruckBeanFactory">
<property name="map">
<map>
<entry key="1">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="5"/>
<property name="brand" value="bmw"/>
</bean>
</entry>
<entry key="2">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="6"/>
<property name="brand" value="benz"/>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="bmwTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="benzTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
</beans>
```
## AnimalDao
```clike=
package tw.leonchen.model;
import org.springframework.beans.factory.annotation.Autowired;
import package tw.leonchen.model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
@Repository(value="animalDao")
public class AnimalDao {
@Autowired @Qualifier(value="animal2")//beans id
private Animal animal;
public void showDetails() {
System.out.println("id:"+animal.getId());
System.out.println("name:"+animal.getName());
System.out.println("Continent:"+animal.getContinent());
}
}
```
# 59 Spring動態網站開發
set

# 60 Spring動態網站開發
Constructor

# 61 Spring動態網站開發

# 62 Spring動態網站開發(系統常數檔案化)

## tree.properties


```clike=
name=MapleTree
age=18
```
# 63 Spring動態網站開發

## beans

[spring api](https://docs.spring.io/spring-framework/docs/current/javadoc-api/)
```clike=
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="tw.leonchen"/>
<bean id="animal1" class="tw.leonchen.model.Animal">
<property name="id" value="1001"/>
<property name="name" value="camel"/>
<property name="continent" value="africa"/>
</bean>
<bean id="animal2" class="tw.leonchen.model.Animal">
<property name="id" value="1002"/>
<property name="name" value="panda"/>
<property name="continent" value="asia"/>
</bean>
<!-- LogProvider logProvider = new LogProvider(); -->
<bean id="logProvider" class="tw.leonchen.util.LogProvider"/>
<bean id="string0" name="string1, string2" class="java.lang.String">
<constructor-arg value="Test Information"/>
</bean>
<!-- LoginDao loginDao = new LoginDao(); -->
<bean id="loginDao" class="tw.leonchen.model.LoginDao"/>
<!-- LoginService loginService2 = new LoginService(loginDao); -->
<bean id="loginService2" class="tw.leonchen.model.LoginService">
<constructor-arg ref="loginDao"/>
</bean>
<!--
LoginService loginService1 = new LoginService();
loginService1.setLoginDao(loginDao);
-->
<bean id="loginService1" class="tw.leonchen.model.LoginService">
<property name="loginDao" ref="loginDao"/>
</bean>
<bean id="myDate" class="java.util.Date"/>
<bean id="dateDao" class="tw.leonchen.model.DateDao">
<constructor-arg ref="myDate"/>
</bean>
<bean id="dateDaoMap" class="tw.leonchen.model.DateDao">
<constructor-arg>
<map>
<entry key="fruit1" value="banana"/>
<entry key="fruit2" value="apple"/>
</map>
</constructor-arg>
</bean>
<bean id="dateDaoMsg1" class="tw.leonchen.model.DateDao">
<property name="msg" value="good day"/>
</bean>
<bean id="dateDaoMsg2" class="tw.leonchen.model.DateDao" p:msg="have a good time"/>
<bean id="truckBean" class="tw.leonchen.model.TruckBean">
<constructor-arg name="id" value="100"/>
<constructor-arg name="brand" value="toyota"/>
</bean>
<bean id="fordTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
<bean id="teslaTruck" class="tw.leonchen.model.TruckBeanStaticFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
<bean id="truckBeanFactory" class="tw.leonchen.model.TruckBeanFactory">
<property name="map">
<map>
<entry key="1">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="5"/>
<property name="brand" value="bmw"/>
</bean>
</entry>
<entry key="2">
<bean class="tw.leonchen.model.TruckBean">
<property name="id" value="6"/>
<property name="brand" value="benz"/>
</bean>
</entry>
</map>
</property>
</bean>
<bean id="bmwTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="1"/>
</bean>
1. <bean id="benzTruck" factory-bean="truckBeanFactory" factory-method="getTruckBean">
<constructor-arg value="2"/>
</bean>
<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>classpath:tree.properties</value>
</property>
</bean>
</beans>
```
# 63 Spring動態網站開發


## TreeBean

```clike=
package tw.leonchen.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("tree")
@Scope("singleton")
public class TreeBean {
@Value("#{props.name}")
private String name;
@Value("#{props.age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
## DemoTreeBeanAction
```clike=
package tw.leonchen.action;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import tw.leonchen.model.TreeBean;
public class DemoTreeBeanAction {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
TreeBean tBean = context.getBean("tree", TreeBean.class);
System.out.println("Name:" + tBean.getName());
System.out.println("Age:" + tBean.getAge());
context.close();
}
}
```
## TreeBean
```clike=
package tw.leonchen.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("tree")
@Scope("singleton")
@PropertySource("classpath:tree.properties")
public class TreeBean {
//@Value("#{props.name}")
@Value("${name}")
private String name;
//@Value("#{props.age}")
@Value("${age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```