这一节我们介绍一下Spring框架的相关常用配置:
- Spring依赖注入的两种方式(构造方法注入和setter方式注入)
- p-namespace方式配置
- properties属性文件配置方式
- 集合对象配置方式
- Bean scopes作用域(单例作用域和原生作用域)
1. Spring依赖注入方式
- 构造方法注入,它相当于在Spring初始化对象的时候调用构造方法将其对象之间的依赖关系给注入到对象中
- 先在类中定义好依赖对象
- 再去定义构造方法,通过在构造方法的参数中设置对象的依赖关系
- 最后在Spring配置文件中使用<constructor-arg>标签搞定对象的依赖注入/<constructor-arg>
<code>package com.zlt.spring.day02;
public class PetServiceImpl {
private PetDaoImpl petDao; //依赖对象
public PetServiceImpl(PetDaoImpl petDao) { //构造方法的DI
this.petDao = petDao;
}
public void selectPet() {
petDao.selectPet();
}
}/<code>
<code>package com.zlt.spring.day02;
public class PetDaoImpl {
public void selectPet() {
/**
* 完成宠物数据查询
*/
System.out.println("==宠物数据查询==");
}
}/<code>
<code>
<beans>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean>
<constructor-arg>
/<bean>
<bean>
/<beans>/<code>
- 设值注入,它通过给依赖对象添加setter方法来完成对象的DI
- 先定义好依赖对象
- 再给依赖对象添加setter方法
- 最后在配置文件中使用<property...>标签就OK了
<code>package com.zlt.spring.day01;
public class PetServiceImpl {
private PetDaoImpl petDao; //依赖对象
private ItemDaoImpl itemDao; //依赖对象
public void setPetDao(PetDaoImpl petDao) {
this.petDao = petDao;
}
public void setItemDao(ItemDaoImpl itemDao) {
this.itemDao = itemDao;
}
public void selectPet() {
petDao.selectPet();
itemDao.selectItem();
}
}/<code>
<code>package com.zlt.spring.day01;
public class PetDaoImpl {
public void selectPet() {
/**
* 完成宠物数据查询
*/
System.out.println("==宠物数据查询==");
}
}/<code>
<code>package com.zlt.spring.day01;
public class ItemDaoImpl {
public void selectItem() {
/**
* 完成宠物分类数据查询
*/
System.out.println("==宠物分类的数据查询==");
}
}/<code>
<code>
<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.xsd">
<bean>
<bean>
<property>
<property>
/<bean>
<bean>
<bean>
/<beans>/<code>
2. p-namespace配置方式
- 主要简化<property>标签的配置/<property>
- 要使用p-namespace需要在整个配置文件声明部分加入p-namespace的XMLSchema定义
<code>
<beans>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean>
<constructor-arg>
/<bean>
<bean>
<bean>
/<beans>/<code>
3. properties属性文件配置
此配置可以利用Spring框架帮我们解析Java中的属性文件。下面我们介绍两种配置方法来解析Java中的属性文件
- <bean>来解析Java中属性文件/<bean>
<code>
<beans>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean>
<property>
/<bean>
<bean> p:username="${mysql.username}"
p:password="${mysql.password}"
p:driver="${mysql.driver}">/<bean>
/<beans>/<code>
- <property-placeholder>来解析Java中属性文件。这个需要在Spring配置文件的XMLSchema导入xmlns:context/<property-placeholder>
<code>
<beans>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<property-placeholder>
<bean> p:username="${mysql.username}"
p:password="${mysql.password}"
p:driver="${mysql.driver}">/<bean>
/<beans>/<code>
4. 集合对象的配置
- 在Spring框架配置文件中采用<list>,/<list>
<code>
<beans>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean>
<property>
<list>
<value>Hello World/<value>
/<list>
/<property>
<property>
/<property>
<property>
<value>Hello World/<value>
/<property>
<property>
<props>
<prop>Hello World/<prop>
<prop>liuyang/<prop>
/<props>
/<property>
/<bean>
/<beans>/<code>
<code>package com.zlt.spring.day02;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionsSpring {
private List> list;
private Map<string> map;
private Set> set;
private Properties props;
public void setList(List> list) {
this.list = list;
}
public void setMap(Map<string> map) {
this.map = map;
}
public void setSet(Set> set) {
this.set = set;
}
public void setProps(Properties props) {
this.props = props;
}
public void showList() {
for (int i = 0; i <list.size> System.out.println(list.get(i));
}
}
public void showMap() {
System.out.println(map);
}
public void showSet() {
Iterator> iterator = set.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
public void showProps() {
Enumeration> enumeration = props.propertyNames();
while(enumeration.hasMoreElements()) {
System.out.println(props.getProperty(enumeration.nextElement().toString()));
}
}
}/<list.size>/<string>/<string>/<code>
5. Bean Scopes作用域
- Singleton:单例作用域,Spring容器初始化对象只有唯一个(默认)
- Prototype:原生作用域,每次调用Spring容器的getBean方法都会重新产生一个新的对象
- Request
- Session
- Global Session
这里重点还是谈一下单例作用域和原生作用域。在配置单例作用域和原生作用域需要使用scope属性
<code>
<beans>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean>
<property>
<list>
<value>Hello World/<value>
/<list>
/<property>
<property>
/<property>
<property>
<value>Hello World/<value>
/<property>
<property>
<props>
<prop>Hello World/<prop>
<prop>liuyang/<prop>
/<props>
/<property>
/<bean>
/<beans>/<code>
<code>package com.zlt.spring.test02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zlt.spring.day02.CollectionsSpring;
import com.zlt.spring.day02.DBConnection;
import com.zlt.spring.day02.Person;
import com.zlt.spring.day02.PetServiceImpl;
public class Test01 {
@Test
public void m05() {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
CollectionsSpring collectionsSpring01 = context.getBean("collectionsSpring", CollectionsSpring.class);
CollectionsSpring collectionsSpring02 = context.getBean("collectionsSpring", CollectionsSpring.class);
System.out.println(collectionsSpring01.hashCode());
System.out.println(collectionsSpring02.hashCode());
}
}/<code>
閱讀更多 知了堂 的文章