Spring学习(二)

来源:互联网 发布:2017软件设计师 编辑:程序博客网 时间:2024/06/05 18:13
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 "/>
singleton
单例 默认值,通过spring容器每次获取到的都是同一个的对象
prototype
原型 通过spring容器每次获取到的都是不同的bean对象
request
一次请求有效( java web)
session
当一个用户和服务器连接未中断,所获取到的都是同一个对象( java web)
global session
 
示例
<beanid="user"class="com.ioc.test.User"scope="prototype">
<propertyname="userId"value="101"/>
<propertyname="userName"value="admin"/>
</bean>
注意:springstruts2整合的时候,把struts2action设置为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标签中设置<beansdefualt-autorwire=""/>表示所有的bean默认都会根据id进行自动装配,如果在beans和bean标签都没有设置自动装配,则默认不进行自动装配。
<beanid=""class=""autowire="no|byName|byType|constructor|autodetect|defualt">
no
不自动装配,这是autowrite的默认值
byName
在Spring中寻找类型和属性能够匹配的bean。如果没有找到,则该属性不进行复制;如果有且仅有一个类型匹配的bean,则将这个赋值给该属性;如果找到不止一个类型匹配的bean则抛出异常
byType
在Spring容器中寻找id等于当前属性名的bean。如果找到了id和当前属性名一致的bean,则会将这个bean赋值给该属性,但是如果这个bean的类型和该属性不一致,则会抛出异常。
constructor
查找和bean的构造参数一致的一个或多个bean,若找不到或找到多个,抛异常。按照参数的类型装配
autodetect
constructor和byType之间选一个方式。不确定性的处理与constructor和byType一致。
defualt
这个需要在<beans defualt-autorwire=“指定” />当你在<beans >指定了 default-atuowrite后, 所有的bean的 默认的autowire就是 指定的装配方法;
处理自动装配的不确定性
使用byType和constructor自动装配时,若找到多个符合条件的bean,会报异常,因此最好的方式是不用自动组装。
混合使用手动和自动组装
<beanid="bar"class="...Bar"autowire="byName">
<propertyname="cousedao">
<refbean="somebean"/>
</property>
</bean>
示例(以 byName为例):
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="包名.类名.方法名"属性来为所有的bean指定默认的初始化和销毁方法
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();
    }
}
 
 

原创粉丝点击