Spring学习(二)
7.给bean的属性赋值
<1>简单bean配置
配置bean的简单属性,
基本数据类型和string。
直接在
property
标签中添加
value
属性,在
value
中给出值
<beanid="course"class="com.softeem.pojos.Course">
<propertyname="courseId"value="1"/>
<propertyname="courseName">
<value>Java软件开发</value>
</property>
</bean>
给
bean
属性赋值
(
第二种方式
)
:
在当前
property
标签添加子标签
bean
完成对象创建
<beanid="stu"class="com.softeem.pojos.Student">
<propertyname="birthday">
<beanclass="java.util.Date"/>
</property>
</bean>
<2>引用其它bean
<beanid="date"class="java.util.Date"/>
<beanid="stu"class="com.softeem.pojos.Student">
<propertyname="birthday"ref="date"/>
</bean>
若bean的属性是 集合类型 ,按如下处理:
<3>装配List和数组:
<beanid="course"class="com.softeem.pojos.Course">
<propertyname="courseId"value="1"/>
<propertyname="courseName"value="Java软件开发"/>
</bean>
<beanid="stu"class="com.softeem.pojos.Student">
<propertyname="coursesNames1">
<list>
<!--基本数据类型 -->
<value>线性代数</value>
<!--引用其他bean -->
<refbean="course"/>
</list>
</property>
</bean>
<4>装配set:
<beanid="course"class="com.softeem.pojos.Course">
<propertyname="courseId"value="1"/>
<propertyname="courseName"value="Java软件开发"/>
</bean>
<beanid="stu"class="com.softeem.pojos.Student">
<propertyname="coursesNames2">
<set>
<!--基本数据类型 -->
<value>线性代数</value>
<!--引用其他bean -->
<refbean="course"/>
</set>
</property>
</bean>
set使用方法和list一样,不同的是对象被装配到set中,而list是装配到List或数组中装配 给 set 注入值 set 不能有相同的对象,会被覆盖掉。
<5>装配map:
<propertyname="map">
<map>
<entrykey="key1"value="bar1"/>
<entrykey="key2"value-ref="xxx"/>
</map>
</property>
<6>装配Properties:
<propertyname="prop">
<props>
<propkey="key1">value1</prop>
<propkey="key2">value2</prop>
</props>
</property>
<7>设置null:
<beanid="stu"class="com.softeem.pojos.Student">
<propertyname="birthday">
<null />
</property>
</bean>
示例:
Employee.java
package com.softeem.pojos;
publicclass Employee {
private String name;
privateintid;
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
}
Department.java
package com.softeem.pojos;
import java.util.List;
import java.util.Map;
import java.util.Set;
publicclass Department {
private String name;
private String[] empName;
private List<Employee> empList;
private Set<Employee> empsets;
private Map<String, Employee> empMaps;
public Set<Employee> getEmpsets() {
returnempsets;
}
publicvoid setEmpsets(Set<Employee> empsets) {
this.empsets = empsets;
}
public String[] getEmpName() {
returnempName;
}
publicvoid setEmpName(String[] empName) {
this.empName = empName;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public List<Employee> getEmpList() {
returnempList;
}
publicvoid setEmpList(List<Employee> empList) {
this.empList = empList;
}
public Map<String, Employee> getEmpMaps() {
returnempMaps;
}
publicvoid setEmpMaps(Map<String, Employee> empMaps) {
this.empMaps = empMaps;
}
}
applicationContext.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<beanid="department"class="com.hsp.collection.Department">
<propertyname="name"value="财务部"/>
<!--给数组注入值 -->
<propertyname="empName">
<list>
<value>小明</value>
<value>小明小明</value>
<value>小明小明小明小明</value>
</list>
</property>
<!--给list注入值 list 中可以有相当的对象 -->
<propertyname="empList">
<list>
<refbean="emp2"/>
<refbean="emp1"/>
<refbean="emp1"/>
<refbean="emp1"/>
<refbean="emp1"/>
<refbean="emp1"/>
<refbean="emp1"/>
</list>
</property>
<!--给set注入值 set不能有相同的对象 -->
<propertyname="empsets">
<set>
<refbean="emp1"/>
<refbean="emp2"/>
</set>
</property>
<!--给map注入值 map只有key不一样,就可以装配value -->
<propertyname="empMaps">
<map>
<entrykey="11"value-ref="emp1"/>
<entrykey="22"value-ref="emp2"/>
<entrykey="33"value-ref="emp1"/>
</map>
</property>
<!--给属性集合配置【点http协议 referer 】 -->
<propertyname="pp">
<props>
<propkey="pp1">abcd</prop>
<propkey="pp2">hello</prop>
</props>
</property>
</bean>
<beanid="emp1"class="com.hsp.collection.Employee">
<propertyname="name"value="北京"/>
<propertyname="id"value="1"/>
</bean>
<beanid="emp2"class="com.hsp.collection.Employee">
<propertyname="name"value="天津"/>
<propertyname="id"value="2"/>
</bean>
</beans>
8.bean 标签中的id属性和name属性的区别
我们在bean标签中可以使用id属性作为对象的唯一表示,同样也可以使用name属性;id和name的唯一区别就是:如果使用name属性做为key,允许使用特殊符号。例如:
使用id:
<beanid="user"class="com.ioc.test.User"/>
使用name:
<beanname="#user@"class="com.ioc.test.User"/>
9.bean的作用域
在
bean
标签中添加
scope
属性,可以设置当前
bean
对象的作用域
scope
的取值:
singleton/prototype/request/session/global session
<beanid=""class=""scope="singleton|prototype|request|session|global session "/>
示例
<beanid="user"class="com.ioc.test.User"scope="prototype">
<propertyname="userId"value="101"/>
<propertyname="userName"value="admin"/>
</bean>
注意
:
在
spring
是
struts2
整合的时候,把
struts2
的
action
设置为
protptype
<beanid="userAction"class="com....UserAction"scope="prototype"/>
10.继承配置
在bean标签中通过
parent
=
""
属性来指明父类
Student.java
publicclass Student implements Serializable {
private String name;
privateintage;
}
Grdate.java
publicclass Grdate extends Student {
private String degree;
public String getDegree() {
returndegree;
}
publicvoid setDegree(String degree) {
this.degree = degree;
}
}
applicationContext.xml
<beanid="student"class="com.pojos.Student">
<propertyname="name"value="旺财"></property>
<propertyname="age"value="30"></property>
</bean>
<beanid="grdate"parent="student"class="com.pojos.Grdate">
<propertyname="degree"value="学士"></property>
<!--如果自己配置name age会覆盖父类继承的属性 -->
</bean>
11.自动装配
自动装配:无需再bean标签中指定属性的值或者引用,就可以自动地为某些属性赋值。
自动:根据Spring所管理的对象的类型(byType)或者id(byName)值来进行匹配。
在beans标签中设置
<
beans
defualt-autorwire
=
""
/>
表示所有的bean默认都会根据id进行自动装配,如果在beans和bean标签都没有设置自动装配,则默认不进行自动装配。
<beanid=""class=""autowire="no|byName|byType|constructor|autodetect|defualt">
处理自动装配的不确定性
使用byType和constructor自动装配时,若找到多个符合条件的bean,会报异常,因此最好的方式是不用自动组装。
混合使用手动和自动组装
示例(以 byName为例):
<beanid="bar"class="...Bar"autowire="byName">
<propertyname="cousedao">
<refbean="somebean"/>
</property>
</bean>
Dog.java
publicclass Dog {
private String name;
privateintage;
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
publicint getAge() {
returnage;
}
publicvoid setAge(int age) {
this.age = age;
}
}
Mester.java
publicclass Mester {
private String name;
private Dog dog;
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
public Dog getDog() {
returndog;
}
publicvoid setDog(Dog dog) {
this.dog = dog;
}
}
applicationContext.xml
<beanid="dog"class="com.pojos.Dog">
<propertyname="name"value="旺财"/>
<propertyname="age"value="10"></property>
</bean>
<beanid="mester"class="com.pojos.Mester"autowire="byName">
<propertyname="name"value="张三"></property>
<!--传统方法 <property name="dog" ref="dog"></property> -->
</bean>
12.bean的生命周期
bean的生命周期:bean的实例由创建到销毁的过程。
可以在bean标签通过
init-method
=
"
方法名
"
和
destroy-method
=
"
方法名
"
属性来指定当前bean的初始化方法和销毁方法。
init-method
:
指定当前
bean
的初始化方法,此方法在创建
bean
的时候会被调用
destroy-method
:指定当前
bean
的销毁方法,此方法在销毁当前
bean
对象时被调用
我们也可以在beans根标签中通过
default-init-method
=
"
包名
.
类名
.
方法名
"
和
default-destroy-method
=
"
包名
.
类名
.
方法名
"
属性来为所有的b
ean指定默认的初始化和销毁方法
bean的生命周期:
①,
如果在bean标签中配置了
scope
=
"singleton"
或者没有配置
scope
(默认值为
singleton
),在加载配置文件时,会完成bean对象的创建并调用指定的init方法;但是调用getBean方法时就不会再创建该实例了。
②,
如果在bean标签中配置了
scope
=
"prototype"
,那么在加载配置文件时候不会创建该实例,而当我们调用getBean方法获取这个bean的实例的时候才会完成bean的创建,并调用init方法,并且在以后每次调用getBean方法都会创建该对象。
package com.softeem.pojos;
publicclass User {
private StringuserName;
private StringuserPass;
publicvoid init(){
System.out.println("----恭喜你,来到这个世界----");
}
publicvoid destory(){
System.out.println("-----你的生命到此结束-----");
}
publicString getUserName() {
returnuserName;
}
publicvoid setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
returnuserPass;
}
publicvoid setUserPass(String userPass) {
this.userPass = userPass;
}
}
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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-2.5.xsd"
default-autowire="byName">
<!--在scope="singleton"的时候bean的生命周期:
当没有配置lazy-init="true":在加载配置文件时,就会创建当前bean的实例,在以后通过getBean访问这个实例的时候不会再次创建,直到释放配置信息的时候销毁该实例;
当配置了lazy-init="true":在加载配置文件时,不会创建bean的实例,当第一次通过getBean访问的时候,才会创建实例,再次使用时则不会重复创建,直到释放配置信息的时候销毁该实例。
-->
<beanid="user1"class="com.softeem.pojos.User"lazy-init="true"init-method="init"destroy-method="destory">
<propertyname="userName"value="admin"></property>
<propertyname="userPass"value="123456"></property>
</bean>
</beans>
package com.softeem.pojos;
import org.springframework.context.support.ClassPathXmlApplicationContext;
publicclass Test {
publicstaticvoid main(String[] args) {
ClassPathXmlApplicationContext c =new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("---------------");
Useru = (User) c.getBean("user1");
System.out.println("...");
Useru1 = (User) c.getBean("user1");
System.out.println("===============");
c.close();
}
}