JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件

时间:2021-05-27 20:57:57

一、XML和注解组合使用

  前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的properties文件。

关键配置信息如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:aop="http://www.springframework.org/schema/aop"
 8     xmlns:util="http://www.springframework.org/schema/util" 
 9     xmlns:task="http://www.springframework.org/schema/task"
10     xsi:schemaLocation="
11         http://www.springframework.org/schema/beans 
12         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd   
15         http://www.springframework.org/schema/task  
16         http://www.springframework.org/schema/task/spring-task-4.2.xsd 
17         http://www.springframework.org/schema/context 
18         http://www.springframework.org/schema/context/spring-context-4.2.xsd" >
19 </beans>

  特别说明一下:

  在Spring框架的配置文件中,xmlns代表xml的命名空间,xmlns:xsi代表当前配置xml所要遵循的标准,简而言之,下面2条是Spring配置文件的最核心也是最基本的配置。 

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
4     xsi:schemaLocation="
5         http://www.springframework.org/schema/beans 
6         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd " >
7 </beans>

        当然,Spring配置文件中能够使用的标签有很多,这需要我们手动引入命名空间,然后给出xsd文件的位置。比如下面:

引入:上下文标签的命名空间

1 xmlns:context="http://www.springframework.org/schema/context"

 引入:面向切面aop相关标签的命名空间

1 xmlns:aop="http://www.springframework.org/schema/aop"

 引入:事务管理相关标签的命名空间

1 http://www.springframework.org/schema/tx

 引入:定时任务相关标签的命名空间

1 xmlns:task="http://www.springframework.org/schema/task"

 引入:加载SpringMVC框架相关标签的命名空间

1 xmlns:mvc="http://www.springframework.org/schema/mvc"

 引入:一些工具类先关标签的命名空间

1 xmlns:util="http://www.springframework.org/schema/util"

 等等..............

二、测试案例

创建一个测试配置文件:applicationContext.xml 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:aop="http://www.springframework.org/schema/aop"
 8     xmlns:util="http://www.springframework.org/schema/util" 
 9     xmlns:task="http://www.springframework.org/schema/task"
10     xsi:schemaLocation="
11         http://www.springframework.org/schema/beans 
12         http://www.springframework.org/schema/beans/spring-beans-4.2.xsd   
13         http://www.springframework.org/schema/aop 
14         http://www.springframework.org/schema/aop/spring-aop-4.2.xsd   
15         http://www.springframework.org/schema/task  
16         http://www.springframework.org/schema/task/spring-task-4.2.xsd 
17         http://www.springframework.org/schema/context 
18         http://www.springframework.org/schema/context/spring-context-4.2.xsd" >
19     <!--扫描加载指定相对路径下的所有properties文件 -->
20     <context:property-placeholder location="classpath:com/xfwl/spring/profile/*.properties" />
21     <!--将针对注解的处理器配置好 -->
22     <context:annotation-config/>
23     <!-- 自动扫描指定包中的Bean:通配符统一批量设置 -->
24     <context:component-scan base-package="com.xfwl.spring.context"/>
25     <!-- 依赖注入 -->
26     <bean id="user" class="com.xfwl.spring.profile.UserBean">
27         <property name="uname" value="${uname}"/>
28         <property name="upwd" value="${upwd}"/>
29     </bean>
30 </beans>

 创建一个Properties属性文件:user.properties 

1 #简单测试一下:用properties文件存放用户数据
2 uname=xfwl
3 upwd=123456

 创建一个POJO类:UserBean.java 

 1 package com.xfwl.spring.context;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.stereotype.Component;
 6 
 7 public class UserBean {
 8     private String uname;
 9     private String upwd;
10     public UserBean(){}
11     public UserBean(String uname,String upwd){
12         this.uname=uname;
13         this.upwd=upwd;
14     }
15     public String getUname() {
16         return uname;
17     }
18     public void setUname(String uname) {
19         this.uname = uname;
20     }
21     public String getUpwd() {
22         return upwd;
23     }
24     public void setUpwd(String upwd) {
25         this.upwd = upwd;
26     }
27     @Override
28     public String toString() {
29         return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";
30     }
31 }    

  创建一个管理类:Manager.java

 1 package com.xfwl.spring.context;
 2 
 3 import org.springframework.beans.factory.BeanNameAware;
 4 import org.springframework.beans.factory.DisposableBean;
 5 import org.springframework.beans.factory.InitializingBean;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.beans.factory.annotation.Qualifier;
 8 import org.springframework.context.annotation.Bean;
 9 import org.springframework.context.annotation.ComponentScan;
10 import org.springframework.context.annotation.Configuration;
11 import org.springframework.stereotype.Component;
12 @Component("manager")
13 public class Manager{
14     @Autowired
15     private UserBean user;
16     /****setter******/
17     public void setUser(UserBean user) {
18         this.user = user;
19     }
20     public void manage(){
21         System.out.println(this.user.toString());    
22     }
23 }

  创建一个测试类:TestBean.java

 1 package com.xfwl.spring.context;
 2 import java.sql.SQLException;
 3 
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 public class TestBean {
 6     //项目相对路径:ClassPathXmlApplicationContext/ApplicationContext
 7     private static final String xmlRelPath="com/xfwl/spring/profile/applicationContext.xml";
 8     public static void main(String[] args) throws SQLException {
 9         //拿到解析对象
10         ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext(xmlRelPath);
11         //开始测试
12         Manager manager=(Manager)ctx.getBean("manager");
13         manager.manage();
14     }
15 }

  测试结果:(XML+注解同时被正常使用

1 log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
2 log4j:WARN Please initialize the log4j system properly.
3 log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
4 UserBean [uname=xfwl, upwd=123456]

 最后补充一点:除了在XML中配置加载properties文件,也可以使用注解加载properties文件,比如下面代码:

 1 package com.xfwl.spring.profile;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.context.annotation.ComponentScan;
 5 import org.springframework.context.annotation.PropertySource;
 6 import org.springframework.stereotype.Component;
 7 import org.springframework.test.context.ContextConfiguration;
 8 @ContextConfiguration  9 @PropertySource(value={"classpath:com/xfwl/spring/profile/*.properties"}, 10                 ignoreResourceNotFound=true) 11 public class UserBean {
12     @Value("${tom.uname}") 13     private String uname;
14     @Value("${tom.upwd}") 15     private String upwd;
16     public UserBean(){}
17     public UserBean(String uname,String upwd){
18         this.uname=uname;
19         this.upwd=upwd;
20     }
21     public String getUname() {
22         return uname;
23     }
24     public void setUname(String uname) {
25         this.uname = uname;
26     }
27     public String getUpwd() {
28         return upwd;
29     }
30     public void setUpwd(String upwd) {
31         this.upwd = upwd;
32     }
33     @Override
34     public String toString() {
35         return "UserBean [uname=" + uname + ", upwd=" + upwd + "]";
36     }
37 }