一、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 + "]";
}
}

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

  1. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(10):通过注解(annotation)装配Bean之(@Configguration、@Component、@Value、@ComponentScan、@Autowired、@Primary、@Qualifier、@Bean)

    一.通过注解(annotation)装配Bean 通过之前的学习,我们已经知道如何使用XML装配Bean,但是更多的时候已经不再推荐使用XML的方式去装配Bean,更多的时候会考虑注解(annotat ...

  2. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(8):装配SpringBean概述(如何合理使用装配级别)

    一. 装配Bean概述  关于如何将自己开发的Bean配置到Spring IoC容器中,大部分场景下,我们都会使用ApplicationContext的具体实现类,因为对应的Spring IoC容器功 ...

  3. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(7):装配SpringBean·依赖注入装配

    一.依赖注入的三种方式      在实际环境中实现IoC容器的方式主要分为两大类,一类是依赖查找,依赖查找是通过资源定位,把对应的资源查找回来.另一类则是依赖注入.一般而言,依赖注入可分为3中方式: ...

  4. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(6):Spring IOC容器学习(概念、作用、Bean生命周期)

    一.IOC控制反转概念 控制反转(IOC)是一种通过描述(在Java中可以是XML或者是注解)并通过第三方去生产或获取特定对象的方式. 主动创建模式,责任在于开发者,而在被动模式下,责任归于Ioc容器 ...

  5. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(2):SSM+Redis概念理解

    一.SSM+Redis的结构图 在Java互联网中,以Spring+SpringMVC+MyBatis(SSM)作为主流框架,SSM+Redis的结构图如下: 二.下面介绍它们各自承担的功能: 1.S ...

  6. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(1):Mybatis和Hibernate概念理解

    一.关键字说明: oop:面向对象 aop:面向切面 ioc:控制反转 orm:对象关系映射 pojo:数据库表映射的java实体类 二.常识说明:1.hibernate和mybatis都属于持久层. ...

  7. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(11):XML和Annotation装配Bean的混合使用(@ImportResource)

    一.XML和Annotation装配Bean如何合理使用 引入第三方资源包中类的时候,建议使用XML配置,而使用自己编写的Java类的时候,推荐使用Annotation注解配置Bean. 二.关于注解 ...

  8. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(9):通过XML装配Bean

    一.通过XML装配Bean 装配简易值 装配集合 命名空间装配(暂不测试) 二.测试例子 创建一个用户类:UserBean.java package com.xfwl.spring.assem; /* ...

  9. JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(5):责任链模式、观察者模式

    一.责任链模式.观察者模式 1.责任链模式:当一个对象在一条链上被多个拦截器处理(烂机器也可以选择不拦截处理它)时,我们把这样的设计模式称为责任链模式,它用于一个对象在多个角色中传递的场景.   2. ...

随机推荐

  1. Centos下Apache+Tomcat集群--搭建记录

    一.目的 利用apache的mod_jk模块,实现tomcat集群服务器的负载均衡以及会话复制,这里用到了<Cluster>. 二.环境 1.基础:3台主机,系统Centos6.5,4G内 ...

  2. thymeleaf layout

      摘自:https://tomoya92.github.io/2017/03/09/thymeleaf-layout/   thymeleaf的layout常用的有两种方式用法 第一种将页面里的每个 ...

  3. GO 功能注释

    文章转载于 Original 2017-06-12 liuhui 生信百科 相似的基因在不同物种中,其功能往往保守的.显然,需要一个统一的术语用于描述这些跨物种的同源基因及其基因产物的功能,否则,不同 ...

  4. java 红包规则

    java 红包规则 拼手气红包: 规则:最大金额:全部金额/个数*倍数 最小金额:0.01 最后一个红包是全部金额-领取金额 随机分配 package com.utils; import java.m ...

  5. DNS_PROBE_FINISHED_NXDOMAIN

    DNS_PROBE_FINISHED_NXDOMAIN 用如下链接清除dns即可 chrome://net-internals/#dns dns不稳定 手动绑定host即可

  6. LR11直接对数据库访问操作方法在性能测试中的应用总结

    项目背景概述 某测试项目,该项目的接口测试需要大量的订单,并且需要订单的状态是已确认客户的订单,大量的订单可以通过下单接口直接造订单数据,但下的订单要人工在后台页面处理到已确认客户的状态才可以使用这些 ...

  7. Program Size: data=9.0 xdata=0 code=47

    data=47.0 编译器编译后,程序总共需要占用47字节的片内RAM空间.注意这个大小仅仅是累加而已,并未考虑各个块之间的空隙,也就是说实际占用的RAM空间可能多于此数值.xdata=0 程序未使用 ...

  8. Python之with语句原理

    我们看一个with处理文件操作的实例: with open('/etc/passwd') as f: for line in f: print(line) 这段代码的作用:打开一个文件,如果一切正常, ...

  9. Spring 中的 LocalSessionFactoryBean和LocalContainerEntityManagerFactoryBean

    Spring和Hibernate整合的时候我们经常会有如下的配置代码 1,非JPA支持的配置 <!-- 配置 Hibernate 的 SessionFactory 实例: 通过 Spring 提 ...

  10. angular的service与factory

      angular里的service是一个单例对象,在应用生命周期结束的时候(关闭浏览器)才会被清除.而controllers在不需要的时候就会被销毁了. factory是angular里的一种ser ...