Bean的应用范围
AbstractApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDao personDao1=(PersonDao)ctx.getBean("personDao");
PersonDao personDao2=(PersonDao)ctx.getBean("personDao");
所上述演示中,取两次相同的key, 返回的结果为true.说明PersonDaoImpl这个类在spring容器中是一个单例模式。
在bean标签中scope属性的值默认为singleton,意义单例。
如果希望两次从容器中取出的都是一个新的对象。可以设置scope为prototype
对于scope的值还有三个:
Request
Session
Request和session应用于web环境。
Global-session 分布式环境中有效。
Bean中的各种属性的注入方式
spring实例化bean有两种方式:
1. 调用无参构造:
<bean id="personDao" class="cn.offcn.dao.impl.PersonDaoImpl"></bean>
<bean id="personDao" class="cn.offcn.dao.impl.PersonDaoImpl">
<property name="id" value="200"></property>
<property name="name" value="张三"></property>
</bean>
当personDaoImpl被实例化后,给property标签所指定的属性进行赋值,赋值也称注入。
2. 有参构造:
<bean id="personDao" class="cn.offcn.dao.impl.PersonDaoImpl">
<constructor-arg name="id" value="300"></constructor-arg>
<constructor-arg name="name" value="李四"></constructor-arg>
</bean>
调用有参构造方法,第一个参是int类型,第二参数String的构造方法进行初始化。
非基本类型和String类型属性注入:
ref 注入的是对象。Ref的值是spring容器中的key
<bean id="myDate" class="java.util.Date"></bean>
<bean id="personDao" class="cn.offcn.dao.impl.PersonDaoImpl">
<property name="id" value="200"></property>
<property name="name" value="张三"></property>
<property name="birthday" ref="myDate"></property>
<property name="books">
<list>
<bean class="cn.offcn.dao.impl.Book">
<property name="bookName" value="三个男人和一个女人的故事"></property>
<property name="bookNo" value="ISBN:8859-100"></property>
</bean>
<bean class="cn.offcn.dao.impl.Book">
<property name="bookName" value="学会理解"></property>
<property name="bookNo" value="ISBN:8859-101"></property>
</bean>
<bean class="cn.offcn.dao.impl.Book">
<property name="bookName" value="红楼梦"></property>
<property name="bookNo" value="ISBN:8859-102"></property>
</bean>
</list>
</property>
<property name="names">
<set>
<value>叶文龙</value>
<value>陈白</value>
<value>赵青</value>
</set>
</property>
<property name="persons">
<map>
<entry key="personName" value="赵正日"></entry>
<entry key="personAge" value="22"></entry>
<entry key="personAddress" value="北京市朝阳区王八大街8号"></entry>
</map>
</property>
<property name="pp">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql:///aaa</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
<property name="addresses">
<array>
<value>北京</value>
<value>天津</value>
<value>上海</value>
</array>
</property>
</bean>
测试:
@Test
public void testBean(){
//实例化spring容器
AbstractApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
PersonDaoImpl personDao1=(PersonDaoImpl)ctx.getBean("personDao");
System.out.println(personDao1.getId());
System.out.println(personDao1.getName());
System.out.println(personDao1.getBirthday());
System.out.println("---------List集合-------------");
List<Book> bookList = personDao1.getBooks();
for (Book book : bookList) {
System.out.println(book);
}
System.out.println("---------Set集合-------------");
Set<String> names = personDao1.getNames();
for (String name : names) {
System.out.println(name);
}
System.out.println("---------Map集合-------------");
Map<String,String> persons= personDao1.getPersons();
Set<Map.Entry<String,String>> sets= persons.entrySet();
for(Map.Entry<String,String> me : sets){
System.out.println(me.getKey()+"="+me.getValue());
}
System.out.println("---------Properties集合-------------");
Properties pp=personDao1.getPp();
Set<Map.Entry<Object,Object>> ppSets= pp.entrySet();
for(Map.Entry<Object,Object> me : ppSets){
System.out.println(me.getKey()+"="+me.getValue());
}
System.out.println("---------数组-------------");
String[] addresses=personDao1.getAddresses();
for(String s : addresses){
System.out.println(s);
}
ctx.close();
}