目录:
1、bean的注入方式
2、bean的范围(singleton/scope)
3、生命集合的
4、自动装配(byName,byType)
5、bean的生命周期
内容:
1、bean的注入方式
当在一个类中要使用其他类的对象时,如何引用到这个自己的,类中呢?
(1)setter方法
<bean id="aDAO" class="com.dao.impl.ADAO"/>
<bean id="aService" class="com.service.impl.AService">
<property ref="aDAO">
</bean>
(2)带参构造函数constructor
<bean id="aDAO" class="com.dao.impl.ADAO"/>
<bean id="aService" class="com.service.impl.AService">
<constructor-arg>
<ref="aDAO"/>
</constructor-arg>
</bean>
在AService中:
class AService{
private ADAO aDAO;
AService(ADAO aDAO){
this.aDAO = aDAO;
}
}
2、bean的范围(singleton/prototype)
<bean id="aService" class="com.service.impl.AService" scope="prototype">//默认方式singleton
3、生命集合的
(1)list的注入
- <bean id="list" class="java.util.ArrayList">
- <constructor-arg>
- <list>
- <value>value1</value>
- <value>value2</value>
- </list>
- </constructor-arg>
- </bean>
(2) map 的注入
- <bean id="map" class="java.util.HashMap">
- <constructor-arg>
- <map>
- <entry key="key1" value="value1"/>
- <entry key="key2" value="value2"/>
- </map>
- </constructor-arg>
- </bean>
- <bean id="set" class="java.util.HashSet">
- <constructor-arg>
- <set>
- <value>value1</value>
- <value>value2</value>
- </set>
- </constructor-arg>
- </bean>
<bean id="aService" class="com.service.impl.AService" autowire="byName"/>
<bean id="aService" class="com.service.impl.AService" autowire="byType"/>
使用事例:
bean.xml中:
<beans>
<bean id="category" class="java.util.TreeMap">
<constructor-arg>
<map>
<entry key="1001" value="餐饮美食"></entry>
<entry key="1002" value="休闲娱乐"/>
</map>
</constructor-arg>
</bean>
<!--上面这个就是使用了带参数的构造函数,初始化category这个对象的。
<bean id="testapp" class="com.nulltest.Test">
</bean>
<bean id="spring1" class="com.spring.app.Spring1" autowire="byName">
<!-- <property name="category" ref="category"></property> -->
<!-- <property name="testapp" ref="testapp"/> -->
</bean>
</beans>
java代码中:
public class Spring1 {
private com.nulltest.Test testapp;
private TreeMap<String , String> category;
public com.nulltest.Test getTestapp() {
return testapp;
}
public void setTestapp(com.nulltest.Test testapp) {
this.testapp = testapp;
}
public TreeMap<String, String> getCategory() {
return category;
}
public void setCategory(TreeMap<String, String> category) {
this.category = category;
}
@SuppressWarnings("unchecked")
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
TreeMap<String, String> cate = (TreeMap<String, String>) context.getBean("category");
Test testa = (Test) context.getBean("testapp");
System.out.println(cate.get("1001"));
Test.test(new SnowRobot("snow.."));
testa.displaySnow();
System.out.println("******************************************");
Spring1 spring1 = (Spring1) context.getBean("spring1");
System.out.println(spring1.getCategory().get("1002"));
spring1.getTestapp().displaySnow();
Spring1 spring2 = (Spring1) context.getBean("spring1");
System.out.println(spring1.hashCode());
System.out.println(spring2.hashCode());
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
其中的get、set方法是必须的。
5、bean的生命周期
<bean id="aService" class="com.service.impl.AService" init-method="init" destroy-method="destroy"/>
含义就是在初始化aService这个对象时,先执行AService类中的init()方法,在销毁这个对象时,执行AService类中的destroy()这个方法。