spring bean三种创建方式
空参构造
<bean name="user" class="leitao.zhang.entities.User"></bean>
静态工厂
<bean name="staticuser" class="leitao.zhang.spring.UserFactory" factory-method="createUser"></bean> public class UserFactory { public static User createUser() { System.out.println("静态工厂"); return new User(); } }
实例工厂
<bean name="UserFactory" class="leitao.zhang.spring.UserFactory"></bean> <bean name="insuser" factory-bean="UserFactory" factory-method="createUser2"></bean> public class UserFactory { public User createUser2() { System.out.println("实例工厂"); return new User(); } }
bean 之 scope
<bean name="user" class="leitao.zhang.entities.User" scope="singleton"></bean>
singleton 单利模式(默认) 容器中只会存在一个 绝大多数要用默认
prototype 多例 每次都new 整合struts2时要配置为改值 ActionContext 每次访问都要创建
request:web环境下,对象和request生命周期一直
session:web环境下,对像与session周期一样
bean之生命周期
<bean name="user" class="leitao.zhang.entities.User" init-method="init" destroy-method="destory"></bean>
init-method=”init”:配置一个方法作为生命周期的初始化方法,spring会在对象创建之后立即调用
destory-method=”destory” :配置一个方法作为生命周期销毁方法,spring容器会在关闭并销毁所有中期中的对象时调用
分模块配置
导入其它配置文件
<beans> <import resource="leitao/zhang/entities/springBean.xml"/> </beans>
spring属性注入
set方式注入
<bean name="order" class="leitao.zhang.entities.Order"> <!-- 值类型 --> <property name="id" value="d123456"></property> <property name="price" value="123"></property> <!-- 引用类型 --> <property name="user" ref="user"></property> </bean> <bean name="user" class="leitao.zhang.entities.User"> <property name="name" value="张三"></property> <property name="age" value="12"></property> </bean>
构造注入
index 指定参数索引位置
type 指定参数类型(完全限定类名)
value 值类型的值
ref引用类型bean
<bean name="order1" class="leitao.zhang.entities.Order"> <constructor-arg name="id" index="0" type="java.lang.String" value="d456987"></constructor-arg> <constructor-arg name="price" index="1" type="java.lang.Double" value="65423"></constructor-arg> <constructor-arg name="user" index="2" type="leitao.zhang.entities.User" ref="user"></constructor-arg> </bean>
p命名空间注入
在跟元素引入 命名空间 xmlns:p=”http://www.springframework.org/schema/p”
<bean name="order2" class="leitao.zhang.entities.Order" p:id="d65478" p:price="365.65" p:user-ref="user"></bean>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > <bean name="order" class="leitao.zhang.entities.Order"> <!-- 八大基本数据类型 --> <property name="id" value="d123456"></property> <property name="price" value="123"></property> <!-- 引用类型 --> <property name="user" ref="user"></property> </bean> <bean name="user" class="leitao.zhang.entities.User"> <property name="name" value="张三"></property> <property name="age" value="12"></property> </bean> <bean name="order1" class="leitao.zhang.entities.Order"> <constructor-arg name="id" index="0" type="java.lang.String" value="d456987"></constructor-arg> <constructor-arg name="price" index="1" type="java.lang.Double" value="65423"></constructor-arg> <constructor-arg name="user" index="2" type="leitao.zhang.entities.User" ref="user"></constructor-arg> </bean> <bean name="order2" class="leitao.zhang.entities.Order" p:id="d65478" p:price="365.65" p:user-ref="user"></bean> </beans>
spel注入 spring express language
格式:#{beanName.beanProperty}
<bean name="order3" class="leitao.zhang.entities.Order"> <property name="id" value="#{order.id}"></property> <property name="price" value="456"></property> <!-- 引用类型 --> <property name="user" ref="user"></property> </bean>
属性复杂类型注入
bean:
package leitao.zhang.entities; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionBean { private Object[] arr; private Object[] arr2; private List list; private Map map; private Set set; private Properties properties; public CollectionBean() { super(); } public Object[] getArr() { return arr; } public void setArr(Object[] arr) { this.arr = arr; } public Object[] getArr2() { return arr2; } public void setArr2(Object[] arr2) { this.arr2 = arr2; } public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "CollectionBean [arr=" + Arrays.toString(arr) + ", arr2=" + Arrays.toString(arr2) + ", list=" + list + ", map=" + map + ", set=" + set + ", properties=" + properties + "]"; } }
xml:
<bean name="cb" class="leitao.zhang.entities.CollectionBean"> <!-- 数组只有一个值 --> <property name="arr" value="4568"></property> <!-- 数组多个值 --> <property name="arr2"> <array> <value>6589</value> <value>#{order2}</value> <ref bean="order3"/> </array> </property> <!-- list和数组一样 --> <property name="list"> <list> <value>6584</value> <ref bean="user"/> <value>#{user.name}</value> </list> </property> <!-- map --> <property name="map"> <map> <entry key="#{user.name}" value="战斗"></entry> <entry key="ade" value-ref="order"></entry> <entry key-ref="user" value-ref="order"></entry> </map> </property> <property name="set"> <set> <value>#{order}</value> <value>#{user.name}</value> <ref bean="order1"/> <array> <value>98</value> </array> </set> </property> <!-- Properties --> <property name="properties"> <props> <prop key="driverClass">com.jdbc.mysql.Driver</prop> <prop key="userName">root</prop> <prop key="password">root</prop> </props> </property> </bean>
java:
package leitao.zhang.spring; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import leitao.zhang.entities.CollectionBean; import leitao.zhang.entities.Order; import leitao.zhang.entities.User; public class Demo1 { // /** // * bean entitis 空参构造 // */ // @Test // public void fun1() // { // // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // User bean = (User) ac.getBean("user"); // System.out.println(bean); // } // /** // * 静态工厂 // */ // @Test // public void fun2() // { // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // User bean = (User) ac.getBean("staticuser"); // System.out.println(bean); // // } // /** // * 实例工厂 // */ // @Test // public void fun3() // { // ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // User bean = (User) ac.getBean("insuser"); // System.out.println(bean); // // } // // // // /** // * 生命周期==初始化销毁方法 // */ // @Test // public void fun4() // { // ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // User bean = (User) ac.getBean("user"); // ac.close(); // System.out.println(bean); // // } /** * set注入 */ @Test public void fun5() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Order bean = (Order) ac.getBean("order"); ac.close(); System.out.println(bean); } /** * 构造注入 */ @Test public void fun6() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Order bean = (Order) ac.getBean("order1"); ac.close(); System.out.println(bean); } /** * p命名空间注入 */ @Test public void fun7() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Order bean = (Order) ac.getBean("order2"); ac.close(); System.out.println(bean); } /** * spel spring expresess language 注入 */ @Test public void fun8() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Order bean = (Order) ac.getBean("order3"); ac.close(); System.out.println(bean); } /** * 属性复杂类型注入 */ @Test public void fun9() { ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); CollectionBean bean = (CollectionBean) ac.getBean("cb"); ac.close(); System.out.println(bean); } }
Spring,struts2整合到web
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在 struts2 Action中获取ApplicationContext对象
WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext()); Order bean = (Order) context.getBean("order1"); ActionContext.getContext().put("order", bean);
未经允许不得转载:开心乐窝-乐在其中 » spring 对象创建 bean