Container的職責在於創建、配置與組裝bean,昨天我們學到了
今日將接續昨天的屬性賦值,再探討bean的重用與bean scope
<?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">
<bean id="userInfo" class="com.swj.UserInfo">
<property name="title" value="manager"></property>
<property name="dept" value="risk management"></property>
</bean>
<bean id="user08" class="com.swj.User">
<property name="userInfo" ref="userInfo"></property>
<property name="userInfo.dept" value="財務部"></property>
</bean>
</beans>
@Test
public void testDay08(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
User user= ioc.getBean("user08", User.class);
System.out.println(user);
}
Result
有的時候我麼可能值需要對現有的bean修改一點點東西,就可以透過bean標籤中的parent屬性達成
<?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">
<bean id="userInfo" class="com.swj.UserInfo">
<property name="title" value="manager"></property>
<property name="dept" value="risk management"></property>
</bean>
<bean id="user08" class="com.swj.User">
<property name="userInfo" ref="userInfo"></property>
<property name="userInfo.dept" value="財務部"></property>
</bean>
<bean id="user08_reuse" class="com.swj.User" parent="user08">
<property name="name" value="james"></property>
<property name="userInfo" ref="userInfo"></property>
<property name="userInfo.dept" value="財務部"></property>
</bean>
</beans>
@Test
public void testDay08(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
User user= ioc.getBean("user08", User.class);
User userReuse= ioc.getBean("user08_reuse", User.class);
System.out.println(user);
System.out.println(userReuse);
}
Result
透過bean標籤中的abstract屬性可以指定該bean只是個模板,無法取用
<?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">
<bean id="userInfo" class="com.swj.UserInfo">
<property name="title" value="manager"></property>
<property name="dept" value="risk management"></property>
</bean>
<!--設定abstract屬性為true -->
<bean id="user08" class="com.swj.User" abstract="true">
<property name="userInfo" ref="userInfo"></property>
<property name="userInfo.dept" value="財務部"></property>
</bean>
<bean id="user08_reuse" class="com.swj.User" parent="user08">
<property name="name" value="james"></property>
<property name="userInfo" ref="userInfo"></property>
<property name="userInfo.dept" value="財務部"></property>
</bean>
</beans>
@Test
public void testDay08(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
User userReuse= ioc.getBean("user08_reuse", User.class);
System.out.println(userReuse);
User user= ioc.getBean("user08", User.class);
System.out.println(user);
}
Result
設定測試類別constructor
public class User {
public User() {
System.out.println("user被創建...");
}
//其他略
}
<?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">
<bean id="user08" class="com.swj.User">
<property name="name" value="james"></property>
<property name="gender" value="male"></property>
</bean>
</beans>
@Test
public void testDay08(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
System.out.println("容器啟動完成....");
}
Result
<?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">
<!-- scope設為prototype -->
<bean id="user08" class="com.swj.User" scope="prototype">
<property name="name" value="james"></property>
<property name="gender" value="male"></property>
</bean>
</beans>
@Test
public void testDay08(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean08.xml");
System.out.println("容器啟動完成....");
User user1= ioc.getBean("user08", User.class);
User user2= ioc.getBean("user08", User.class);
System.out.println(user1 == user2);
}
Result