一、XML和注解组合使用
前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的properties文件。
关键配置信息如下:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd" >
</beans>
特别说明一下:
在Spring框架的配置文件中,xmlns代表xml的命名空间,xmlns:xsi代表当前配置xml所要遵循的标准,简而言之,下面2条是Spring配置文件的最核心也是最基本的配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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-4.2.xsd " >
</beans>
当然,Spring配置文件中能够使用的标签有很多,这需要我们手动引入命名空间,然后给出xsd文件的位置。比如下面:
引入:上下文标签的命名空间
xmlns:context="http://www.springframework.org/schema/context"
引入:面向切面aop相关标签的命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
引入:事务管理相关标签的命名空间
http://www.springframework.org/schema/tx
引入:定时任务相关标签的命名空间
xmlns:task="http://www.springframework.org/schema/task"
引入:加载SpringMVC框架相关标签的命名空间
xmlns:mvc="http://www.springframework.org/schema/mvc"
引入:一些工具类先关标签的命名空间
xmlns:util="http://www.springframework.org/schema/util"
等等..............
二、测试案例
创建一个测试配置文件:applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd" >
<!--扫描加载指定相对路径下的所有properties文件 -->
<context:property-placeholder location="classpath:com/xfwl/spring/profile/*.properties" />
<!--将针对注解的处理器配置好 -->
<context:annotation-config/>
<!-- 自动扫描指定包中的Bean:通配符统一批量设置 -->
<context:component-scan base-package="com.xfwl.spring.context"/>
<!-- 依赖注入 -->
<bean id="user" class="com.xfwl.spring.profile.UserBean">
<property name="uname" value="${uname}"/>
<property name="upwd" value="${upwd}"/>
</bean>
</beans>
创建一个Properties属性文件:user.properties
#简单测试一下:用properties文件存放用户数据
uname=xfwl
upwd=123456
创建一个POJO类:UserBean.java
package com.xfwl.spring.context; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component; public class UserBean {
private String uname;
private String upwd;
public UserBean(){}
public UserBean(String uname,String upwd){
this.uname=uname;
this.upwd=upwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
@Override
public String toString() {
return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";
}
}
创建一个管理类:Manager.java
package com.xfwl.spring.context; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component("manager")
public class Manager{
@Autowired
private UserBean user;
/****setter******/
public void setUser(UserBean user) {
this.user = user;
}
public void manage(){
System.out.println(this.user.toString());
}
}
创建一个测试类:TestBean.java
package com.xfwl.spring.context;
import java.sql.SQLException; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
//项目相对路径:ClassPathXmlApplicationContext/ApplicationContext
private static final String xmlRelPath="com/xfwl/spring/profile/applicationContext.xml";
public static void main(String[] args) throws SQLException {
//拿到解析对象
ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext(xmlRelPath);
//开始测试
Manager manager=(Manager)ctx.getBean("manager");
manager.manage();
}
}
测试结果:(XML+注解同时被正常使用)
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
UserBean [uname=xfwl, upwd=123456]
最后补充一点:除了在XML中配置加载properties文件,也可以使用注解加载properties文件,比如下面代码:
package com.xfwl.spring.profile; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration
@PropertySource(value={"classpath:com/xfwl/spring/profile/*.properties"},
ignoreResourceNotFound=true)
public class UserBean {
@Value("${tom.uname}")
private String uname;
@Value("${tom.upwd}")
private String upwd;
public UserBean(){}
public UserBean(String uname,String upwd){
this.uname=uname;
this.upwd=upwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
@Override
public String toString() {
return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";
}
}