spring-配置详解
1. Bean元素
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
<!-- 将ComboList对象交给spring容器管理 -->
<!-- Bean元素:使用该元素描述需要spring容器管理的对象
class属性:被管理对象的完整类名.
name属性:给被管理的对象起个名字.获得对象时根据该名称获得对象.
可以重复.可以使用特殊字符.
id属性: 与name属性一模一样.
名称不可重复.不能使用特殊字符.
结论: 尽量使用name属性.
-->
<bean name="comboList" class="com.ccblogs.spring.ComboList" ></bean>
</beans>
2. 创建对象方式
- ComboList.java类
public class ComboList { public ComboList() { System.out.println("无参构造方法创建对象"); } }
- ComboListFactory.java类
public class ComboListFactory { public ComboListFactory() { // TODO Auto-generated constructor stub } public static ComboList createComboList() { System.out.println("静态工厂创建"); return new ComboList(); } public ComboList CreateComboList2() { System.out.println("实例工厂创建"); return new ComboList(); } }
- applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <!-- 无参构造函数创建对象 --> <bean name="comboList" class="com.ccblogs.spring.ComboList"></bean> <!-- 静态工厂创建对象 --> <bean name="comboList2" class="com.ccblogs.spring.ComboListFactory" factory-method="createComboList"></bean> <!-- 实例工厂创建对象 --> <bean name="comboList3" factory-bean="createComboList2" factory-method="CreateComboList2"></bean> <bean name="createComboList2" class="com.ccblogs.spring.ComboListFactory"></bean> </beans>
- ComboListTest.java类
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); Object obj2 = cont.getBean("comboList2"); System.out.println(obj2); Object obj3 = cont.getBean("comboList3"); System.out.println(obj3); } }
3. scope属性
- singleton,默认值,单例对象。在spring容器中只会存在一个实例。使用方式:
<bean name="comboList" class="com.ccblogs.spring.ComboList" scope="singleton"></bean>
- prototype,多例的对象。每次再获得都会创建新的对象,spring管理Struts2时,ActionBean必须配置为多例的。
<bean name="comboList" class="com.ccblogs.spring.ComboList" scope="prototype"></bean>
- request,web环境下,对象与request生命周期一致。
- session,web环境下,对象与session生命周期一致。
4. 初始化和销毁方法
有错误!(cvc-complex-type.3.2.2: Attribute ‘destory-method’ is not allowed to appear in element ‘bean’.)
- ComboList.java
public class ComboList { public ComboList() { System.out.println("无参构造方法创建对象"); } public void getInit() { System.out.println("初始化方法"); } public void getDestory() { System.out.println("销毁方法"); } }
- 使用init-method属性标注初始化方法,使用destory-method属性标注销毁方法,applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList" init-method="getInit" destory-method="getDestory"></bean> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @Test public static void main(String[] args) { ClassPathXmlApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); cont.close(); } }
5. 模块化配置
- ComboList.java
public class ComboList { public ComboList() { System.out.println("无参构造方法创建对象"); } public void getInit() { System.out.println("初始化方法"); } }
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList" init-method="getInit"></bean> </beans>
- 使用import标签导入其他配置文件,applicationContext1.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <import resource="applicationContext.xml"></import> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @Test public static void main(String[] args) { ClassPathXmlApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext1.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); cont.close(); } }
6. 属性注入(set方式)
- ComboList.java
public class ComboList { private String name; private int age; private Like like; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Like getLike() { return like; } public void setLike(Like like) { this.like = like; } @Override public String toString() { return "ComboList [name=" + name + ", age=" + age + ", like=" + like + "]"; } }
- Like.java
public class Like { private String like1; private String like2; public Like() { // TODO Auto-generated constructor stub } public String getLike1() { return like1; } public void setLike1(String like1) { this.like1 = like1; } public String getLike2() { return like2; } public void setLike2(String like2) { this.like2 = like2; } @Override public String toString() { return "Like [like1=" + like1 + ", like2=" + like2 + "]"; } }
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList"> <property name="name" value="ccblogs"></property> <property name="age" value="18"></property> <property name="like" ref="like"></property> </bean> <bean name="like" class="com.ccblogs.spring.Like"> <property name="like1" value="like11"></property> <property name="like2" value="like22"></property> </bean> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); } }
7. 属性注入(构造函数)
- ComboList.java
public class ComboList { public ComboList(String name, Integer age) { System.out.println(String.format("name=%s,age=%d", name, age)); } public ComboList(Integer name, String age) { System.out.println(String.format("name=%d,age=%s", name, age)); } }
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList"> <constructor-arg name="name" value="18" index="0" type="java.lang.Integer"></constructor-arg> <constructor-arg name="age" value="ccblogs" index="1" type="java.lang.String"></constructor-arg> </bean> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); } }
8. 属性注入(p名称空间&SPEL表达式)
- p名称空间
- ComboList.java
public class ComboList { private String name; private int age; private Like like; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Like getLike() { return like; } public void setLike(Like like) { this.like = like; } @Override public String toString() { return "ComboList [name=" + name + ", age=" + age + ", like=" + like + "]"; } }
- Like.java
public class Like { private String like1; private String like2; public Like() { // TODO Auto-generated constructor stub } public String getLike1() { return like1; } public void setLike1(String like1) { this.like1 = like1; } public String getLike2() { return like2; } public void setLike2(String like2) { this.like2 = like2; } @Override public String toString() { return "Like [like1=" + like1 + ", like2=" + like2 + "]"; } }
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList" p:name="ccblogs" p:age="18" p:like-ref="like"></bean> <bean name="like" class="com.ccblogs.spring.Like"> <property name="like1" value="like11"></property> <property name="like2" value="like22"></property> </bean> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); } }
- ComboList.java
- SPEL表达式
- ComboList.java
public class ComboList { private String name; private int age; private Like like; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Like getLike() { return like; } public void setLike(Like like) { this.like = like; } @Override public String toString() { return "ComboList [name=" + name + ", age=" + age + ", like=" + like + "]"; } }
- Like.java
public class Like { private String like1; private String like2; public Like() { // TODO Auto-generated constructor stub } public String getLike1() { return like1; } public void setLike1(String like1) { this.like1 = like1; } public String getLike2() { return like2; } public void setLike2(String like2) { this.like2 = like2; } @Override public String toString() { return "Like [like1=" + like1 + ", like2=" + like2 + "]"; } }
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList"> <property name="name" value="#{comboList1.name}"></property> <property name="age" value="#{comboList2.age}"></property> <property name="like" ref="like"></property> </bean> <bean name="comboList1" class="com.ccblogs.spring.ComboList" p:name="ccblogs1" p:age="181" p:like-ref="like"> </bean> <bean name="comboList2" class="com.ccblogs.spring.ComboList"> <property name="name" value="ccblogs2"></property> <property name="age" value="182"></property> <property name="like" ref="like"></property> </bean> <bean name="like" class="com.ccblogs.spring.Like"> <property name="like1" value="like11"></property> <property name="like2" value="like22"></property> </bean> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); } }
- ComboList.java
9. 复杂类型注入
- ComboList.java
import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; public class ComboList { private Object[] strs; private List lst; private Map map; private Properties pro; public Object[] getStrs() { return strs; } public void setStrs(Object[] strs) { this.strs = strs; } public List getLst() { return lst; } public void setLst(List lst) { this.lst = lst; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getPro() { return pro; } public void setPro(Properties pro) { this.pro = pro; } @Override public String toString() { return "ComboList [strs=" + Arrays.toString(strs) + ", lst=" + lst + ", map=" + map + ", pro=" + pro + "]"; } }
- Like.java
public class Like { private String like1; private String like2; public Like() { // TODO Auto-generated constructor stub } public String getLike1() { return like1; } public void setLike1(String like1) { this.like1 = like1; } public String getLike2() { return like2; } public void setLike2(String like2) { this.like2 = like2; } @Override public String toString() { return "Like [like1=" + like1 + ", like2=" + like2 + "]"; } }
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd "> <bean name="comboList" class="com.ccblogs.spring.ComboList"> <property name="strs"> <array> <value>ccblogs</value> <value>ccblogs1</value> <ref bean="like" /> </array> </property> <property name="lst"> <list> <value>ccblogs</value> <value>ccblogs1</value> <ref bean="like" /> </list> </property> <property name="map"> <map> <entry key="key1" value="ccblogs1"> </entry> <entry key="key1" value="ccblogs2"> </entry> <entry key-ref="like" value-ref="like"> </entry> </map> </property> <property name="pro"> <props> <prop key="key1">ccblogs1</prop> <prop key="key2">ccblogs2</prop> </props> </property> </bean> <bean name="like" class="com.ccblogs.spring.Like"> <property name="like1" value="like11"></property> <property name="like2" value="like22"></property> </bean> </beans>
- ComboListTest.java
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); Object obj = cont.getBean("comboList"); System.out.println(obj); } }