Spring2.0以后,基于XML的配置采用了Schema的格式,让不同类型的配置拥有了自己的命名空间,使得配置文件更具扩展性。
Spring4.2的Schema文件用途的说明:
目录 | 说明 | 命名空间 | Schema文件 |
---|---|---|---|
spring-aop-4.2.xsd | AOP配置文件定义的Schema | http://www.springframework.org/schema/aop | http://www.springframework.org/schema/aop/spring-aop-4.2.xsd |
spring-beans-4.2.xsd | Spring4.2最主要的Schema,用于配置Bean | http://www.springframework.org/schema/beans | http://www.springframework.org/schema/beans/spring-beans-4.2.xsd |
spring-cache-4.2.xsd | 缓存配置文件定义的Schema | http://www.springframework.org/schema/cache | http://www.springframework.org/schema/cache/spring-cache-4.2.xsd |
spring-context-4.2.xsd | 应用程序上下文配置文件定义的Schema | http://www.springframework.org/schema/context | http://www.springframework.org/schema/context/spring-context-4.2.xsd |
spring-jdbc-4.2.xsd | 数据库访问配置文件定义的Schema | http://www.springframework.org/schema/jdbc | http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd |
spring-jee-4.2.xsd | 为简化JavaEE中的EJB,JNDI等功能配置而提供的Schema | http://www.springframework.org/schema/jee | http://www.springframework.org/schema/jee/spring-jee-4.2.xsd |
spring-jms-4.2.xsd | JMS配置的Schema | http://www.springframework.org/schema/jms | http://www.springframework.org/schema/jms/spring-jms-4.2.xsd |
spring-lang-4.2.xsd | 增加了对JRuby和Grooy等动态语言的支持,为集成动态语言而定义的 | http://www.springframework.org/schema/lang | http://www.springframework.org/schema/lang/spring-lang-4.2.xsd |
spring-mvc-4.2.xsd | mvc配置的Schema | http://www.springframework.org/schema/mvc | http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd |
spring-oxm-4.2.xsd | 配置对象映射XML的Schema,是Spring3.0新增的 | http://www.springframework.org/schema/oxm | http://www.springframework.org/schema/oxm/spring-oxm-4.2.xsd |
spring-task-4.2.xsd | 任务调度的Schema | http://www.springframework.org/schema/task | http://www.springframework.org/schema/task/spring-task-4.2.xsd |
spring-tool-4.2.xsd | 为集成Spring一些有用工具定义的Schema | http://www.springframework.org/schema/tool | http://www.springframework.org/schema/tool/spring-tool-4.2.xsd |
spring-tx-4.2.xsd | 声明式事务事务配置定义的Schema | http://www.springframework.org/schema/tx | http://www.springframework.org/schema/tx/spring-tx-4.2.xsd |
spring-util-4.2.xsd | 简化某些复杂的标准配置提供的Schema | http://www.springframework.org/schema/util | http://www.springframework.org/schema/util/spring-util-4.2.xsd |
spring-websocket-4.2.xsd | websocket提供了一个在web应用中的高效、双向的通讯,需要考虑到客户端(浏览器)和服务器之间的高频和低延时消息交换提供的Schema,Spring4.0新增的 | http://www.springframework.org/schema/websocket | http://www.springframework.org/schema/websocket/spring-websocket-4.2.xsd |
参考博客: 三种实例化Bean的方式链接点我!!!
属性注入即通过setXxx()方法注入Bean的属性值或者是依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,属性注入方式是最常用的依赖注入方式。属性注入方式要求Bean提供一个默认的构造函数,为需要注入的属性提供Setter方法。Spring调用Bean的默认构造函数实例化Bean对象,然后通过反射的方式Setter方法注入的属性值。
自定义Bean IDCardGenerator 说明:
IDCardGenerator类中主要包含了:
属性: Long和String以及Date类型的数据类型,其中Date类型的数据类型需要手动处理
getset方法:Spring依赖注入时通过set方法给属性赋值
默认无参数的构造器:用于创建Bean的实例
带三个参数的构造器:Spring依赖注入时通过构造器给属性复制
toString()方法:用于获取Bean信息
package com.timeline.javaweb.spring.bean;
import com.timeline.javaweb.spring.utils.lang.DateFormatConstants;
import com.timeline.javaweb.spring.utils.lang.DateUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/** * IDCard生成器,用于SpringIOC测试数据 * * @author tony * @create 2016-03-13-8:18 * @since JDK7.0u80 */
public class IDCardGenerator {
private String prefix; //身份证前缀
private Date birthday; //生日
private Long number; //编号
/*get set方法*/
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Long getNumber() {
return number;
}
public void setNumber(Long number) {
this.number = number;
}
/** * 带参数的构造器 * @param prefix * @param year * @param month * @param day * @param number */
public IDCardGenerator(String prefix,int year,int month,int day, Long number) {
this.prefix = prefix;
Calendar instance =new GregorianCalendar(year,month,day);
this.birthday =instance.getTime();
this.number = number;
}
/** * 默认的构造器 */
public IDCardGenerator(){}
/** * 身份证的描述 * @return */
@Override
public String toString() {
return "IDCardGenerator{" +
"prefix='" + prefix + '\'' +
", birthday=" + DateUtils.date2String(birthday, DateFormatConstants.YYYY_MM_DD)+
", number=" + number +
'}';
}
}
appContext-ioc.xml配置文件的配置信息
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd ">
<!--使用get set方法注入一个IDCardGenerator对象 -->
<bean id="idCardGeneratorWithSetterInject" class="com.timeline.javaweb.spring.bean.IDCardGenerator">
<!-- 给指定的属性赋值-->
<property name="prefix">
<value>421024</value>
</property>
<property name="birthday">
<!-- 引用日期格式转换工厂-->
<bean factory-bean="dateFormat" factory-method="parse">
<constructor-arg value="1988-12-12" ></constructor-arg>
</bean>
</property>
<property name="number" value="8210"/>
</bean>
<!-- 定义一个日期格式转换对象-->
<bean id="dateFormat" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd"/>
</bean>
</beans>
属性注入测试用例
public void testSetterInject() throws Exception {
//获取
IDCardGenerator idCardGeneratorWithSetterInject = SpringUtils.getSeviceBean("idCardGeneratorWithSetterInject", IDCardGenerator.class);
logger.info("Get Object From Spring Context With Setter Inject " + idCardGeneratorWithSetterInject);
}
程序运行结果
2016-04-10 14:41:12.741 INFO [main][TestSequenceGenerator.java:74] - Get Object From Spring Context With Setter Inject IDCardGenerator{prefix='421024', birthday=1988-12-12, number=8210}
appContext-ioc.xml配置文件的配置信息
<!-- 采用Spring容器依赖注入的构造器方式注入Bean-->
<bean id="idCardGeneratorWithConstructor" class="com.timeline.javaweb.spring.bean.IDCardGenerator">
<!-- 设置构造器对应的参数的值-->
<constructor-arg index="0" type="java.lang.String" value="421023"/>
<constructor-arg index="1" value="1988"/>
<constructor-arg index="2" value="1"/>
<constructor-arg index="3" value="24"/>
<constructor-arg index="4" value="8510"/>
</bean>
构造函数注入测试用例
@Test //构造方法注入
public void testConstructorInject() {
IDCardGenerator idCardGenerator = SpringUtils.getSeviceBean("idCardGeneratorWithConstructor", IDCardGenerator.class);
logger.info("Get Object From Spring Context With Constructor Inject " + idCardGenerator);
}
程序运行结果
2016-04-10 14:48:59.526 INFO [main][TestSequenceGenerator.java:84] - Get Object From Spring Context With Constructor Inject IDCardGenerator{prefix='421023', birthday=1988-02-24, number=8510}