---
# System prepended metadata

title: Spring 2/24上課內容
tags: [Spring]

---

# Spring 2/24上課內容
###### tags: `Spring`


# 32 Spring動態網站開發
![](https://i.imgur.com/BRYsiET.png)
```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動態網站開發
![](https://i.imgur.com/rnIN8ge.png)
```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>

```
![](https://i.imgur.com/WMNPN7D.png)

---
![](https://i.imgur.com/DObGXuB.png)
* **FullyQualifiedName**
     *    ![](https://i.imgur.com/I5raGMl.png)

# 34 Spring動態網站開發
![](https://i.imgur.com/agLpCGA.png)

---
* property 參考bean id or value
* ![](https://i.imgur.com/q2ZZQxL.png)

# 補昨天進度 4-1
![](https://i.imgur.com/YdEvscO.png)
![](https://i.imgur.com/0kUbQwJ.png)
```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;
    }
}
```
代建構子
不代參數建構子
![](https://i.imgur.com/8gHy3uF.png)
* **建構子**
    * 定義:任何class一定有建構子

![](https://i.imgur.com/yR7qC8P.png)
```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動態網站開發
![](https://i.imgur.com/j6uRkBo.png)
```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>
```
---
![](https://i.imgur.com/NyrcHxN.png)
![](https://i.imgur.com/NPKV9Eb.png)
```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();
	}

}

```
![](https://i.imgur.com/45VqxnU.png)
```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();
	}

}

```
![](https://i.imgur.com/qJqYTOU.png)

![](https://i.imgur.com/jqSxiIp.png)
* 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注入各種不同型態的資料)
![](https://i.imgur.com/Oblfn1a.png)
* **不帶參數建構子**
    * ![](https://i.imgur.com/n6L7Cmz.png)

# 37 Spring動態網站開發
![](https://i.imgur.com/bhEEYm0.png)

---
![](https://i.imgur.com/MWhD8jr.png)
一定要有不帶參數建構子
![](https://i.imgur.com/YUax2pJ.png)
不要大寫
![](https://i.imgur.com/mIg8GGY.png)

# 38 Spring動態網站開發 
![](https://i.imgur.com/uYbvLRU.png)

---
![](https://i.imgur.com/jOBIjri.png)

# 39 Spring動態網站開發 
![](https://i.imgur.com/PQngmhx.png)

---
![](https://i.imgur.com/BOo9C2E.png)

# 40 Spring動態網站開發 
![](https://i.imgur.com/qk6LIWP.png)

--- 
![](https://i.imgur.com/XglGrMA.png)

# 41  Spring動態網站開發 
![](https://i.imgur.com/U1AI5Wz.png)

---
![](https://i.imgur.com/BS1fVHW.png)

# 42 Spring動態網站開發 
![](https://i.imgur.com/cdq2zQ9.png)
![](https://i.imgur.com/WxyuHf5.png)
[Date api](https://docs.oracle.com/javase/8/docs/api/)

## DateDao
![](https://i.imgur.com/QEk7L4R.png)
```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
![](https://i.imgur.com/TEUzIU8.png)
```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動態網站開發 
![](https://i.imgur.com/U3FUjvv.png)
* set 不重複 沒順序
* list 按照順序 可重複
* **LinkedHashSet**
    * 不可重複 按照順序放入
* LinkedList 按照順序 可重複

# 43 Spring動態網站開發 
![](https://i.imgur.com/0eIi6W5.png)

---
![](https://i.imgur.com/a0jSOYs.png)

## DateDao
![](https://i.imgur.com/zFfSbT5.png)
![](https://i.imgur.com/tHaGet3.png)
```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動態網站開發 
![](https://i.imgur.com/sGIa5nL.png)

---
![](https://i.imgur.com/M5pCDla.png)
![](https://i.imgur.com/0h1RlKO.png)
![](https://i.imgur.com/eDmgDo8.png)

# 45 Spring動態網站開發 
![](https://i.imgur.com/IkXcd9F.png)
## 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產生物件的方式)
![](https://i.imgur.com/UtAnKSX.png)

---
![](https://i.imgur.com/bqNFfzR.png)

# 47 Spring動態網站開發
![](https://i.imgur.com/dOkVYzY.png)

## TruckBean
![](https://i.imgur.com/oHDmPgF.png)
```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
![](https://i.imgur.com/PYpi360.png)
```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動態網站開發 
![](https://i.imgur.com/HzoJqVg.png)

## TruckBeanStaticFactory
![](https://i.imgur.com/GhV6VQK.png)
```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動態網站開發 
![](https://i.imgur.com/L00k5Un.png)
![](https://i.imgur.com/YFkYUUV.png)

## 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動態網站開發
![](https://i.imgur.com/ayF3IX3.png)

## 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動態網站開發
![](https://i.imgur.com/pZ3MPZD.png)
![](https://i.imgur.com/rM1UZJp.png)

## TruckBeanFactory
![](https://i.imgur.com/wdIbeSf.png)
```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動態網站開發
![](https://i.imgur.com/HzoOS7U.png)

## 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的組裝資訊)
![](https://i.imgur.com/pxARUxJ.png)

# 52 Spring動態網站開發
![](https://i.imgur.com/ytgY7qr.png)
![](https://i.imgur.com/vWshrKz.png)

## beans
![](https://i.imgur.com/chaduX4.png)
![](https://i.imgur.com/qiDFTgY.png)
```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動態網站開發
![](https://i.imgur.com/yRrQQiH.png)

---
![](https://i.imgur.com/hAvoF11.png)
* @Component:我
* 
# 54 Spring動態網站開發
![](https://i.imgur.com/QXSinAe.png)

---
![](https://i.imgur.com/s6SGwMW.png)

# 55 Spring動態網站開發
![](https://i.imgur.com/S8ieLzj.png)

---
![](https://i.imgur.com/XTiMXY7.png)

# 56 Spring動態網站開發
![](https://i.imgur.com/VAqrrBQ.png)

## Animal
![](https://i.imgur.com/zkDnSQo.png)
```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
![](https://i.imgur.com/HPGOaYM.png)
```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)
![](https://i.imgur.com/fFVUL8s.png)

---
![](https://i.imgur.com/mVrKhCc.png)

# 58 Spring動態網站開發
![](https://i.imgur.com/u2N2kXf.png)

---
![](https://i.imgur.com/zfUWV9P.png)

# 59 Spring動態網站開發
![](https://i.imgur.com/n4geQ9o.png)

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

![](https://i.imgur.com/bMZzhul.png)
```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
![](https://i.imgur.com/Fgs6Rud.png)

# 60 Spring動態網站開發
Constructor
![](https://i.imgur.com/nGW9xyA.png)

# 61 Spring動態網站開發
![](https://i.imgur.com/3MMppCK.png)

# 62 Spring動態網站開發(系統常數檔案化)
![](https://i.imgur.com/NR2dgfM.png)

## tree.properties
![](https://i.imgur.com/PWZxxQ6.png)
![](https://i.imgur.com/6yG1eSR.png)
```clike=
name=MapleTree
age=18
```

# 63 Spring動態網站開發
![](https://i.imgur.com/TtQAdgW.png)


## beans
![](https://i.imgur.com/QLHEF0n.png)
[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動態網站開發
![](https://i.imgur.com/NZpYt9V.png)
![](https://i.imgur.com/oezb1Ai.png)

## TreeBean
![](https://i.imgur.com/TAHOgfW.png)
```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;
	}

}

```