Java Spring xml文件

2 篇文章 0 订阅


<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
   xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
   xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
   xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd  
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd  
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd  
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
--------------------------------------------------------------------------------------
我的理解就是,这上面一大段,是Spring的版本。  
-----------------------------------------------------------------------------------------
   
   <context:property-placeholder location="file:/opt/icim_amoled/conf/icim.properties"  ignore-unresolvable="true"/>


 
 
-------------------------------------------------------------------------------------------------------------------

<context:property-placeholder>标签提供了一种优雅的外在化参数配置的方式,不过该标签在Spring配置文件中只能存在一份!!!

众所周知,Spring容器是采用反射扫描的发现机制,通过标签的命名空间实例化实例,当Spring探测到容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描,即只能存在一个实例!

<context:property-placeholder   
        location=""  
        file-encoding=""  
        ignore-resource-not-found=""  
        ignore-unresolvable=""  
        properties-ref=""  
        local-override=""  
        system-properties-mode=""  
        order=""  
/> 

location:表示属性文件位置,多个之间通过如逗号/分号等分隔;
file-encoding:文件编码;
ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常
ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常
properties-ref:本地java.util.Properties配置
local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性
system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE
ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer
OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment
NEVER:只查找properties-ref、location;
order:当配置多个<context:property-placeholder/>时的查找顺序

使用注意:
1.location中的加载文件的顺序
如果location中有多个文件:
classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties
将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值
举例来说:
default.properties文件中有个属性名userId,其对应的值为-1
default2.properties文件中也有一个属性名userId,其对应的值为-2
default3.properties文件中特有一个属性名userId,其对于那个的值为-3

default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2
所以需要避免不同属性文件中的属性名称重名

2.ignore-resource-not-found和ignore-unresolvable两个属性是类似的作用,推荐配对使用
如果location中的文件指向了一个不存在的文件,那么也极有可能意味着有属性无法解析(虽然存在其他属性文件中存在重名,但是这个是应该避免的)
所以当ignore-resource-not-found设为true时,ignore-unresolvable也必须设为true,其实当ignore-unresolvable设为true时,ignore-resource-not-found的值true或false,并不影响异常的抛出
如果设置为ture,后属性值无法解析成功,将赋值为${属性名}
不推荐将ignore-resource-not-found和ignore-unresolvable的值设置为ture,默认为false,可以有效避免程序运行异常

3.properties-ref属性
引入其他方式引入的属性文件

<bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>classpath:default2.properties</value>
        </list>
    </property>
</bean>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

该属性需要local-override配合使用,只有当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)

4.local-override属性
当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)

5.sytem-properties-mode属性
不同的sytem-properties-mode属性定义了不同的查找顺序
Environment环境:包括JDK环境,系统环境变量,Sevlet环境,Spring环境等,是Spring在3.1之后抽象的一个表示环境配置

在local-override属性值为false,sytem-properties-mode属性值为ENVIRONMENT或OVRRIDE时,查找顺序是location,然后是environment或者System.getProperty(),System.getenv()(Spring 3.1 之前)
即现加载location指向的属性文件,再加之environment指向的环境,当environment环境中存在和location指向的属性文件中同名的属性,则该属性的值将被修改,取决于environment环境中的值
如果sytem-properties-mode属性值为NEVER,则只查询location指向的属性文件

当local-override属性值为true时,最后将加载properties-ref指向的文件,如遇到同名的,该同名属性值将取决于properties-ref指向的文件中的值

所以,最终程序中获取的值将是一个综合作用后的值,一般情况下建议sytem-properties-mode属性值为NEVER避免ENVIRONMENT环境中的不可控

------------------------------------------------------------------------------------------

 <context:annotation-config />
-------------------------------------------------------------------------------------------

<context:annotation-config/>的作用是向Spring容器注册以下四个BeanPostProcessor:

  • AutowiredAnnotationBeanPostProcessor
  • CommonAnnotationBeanPostProcessor
  • PersistenceAnnotationBeanPostProcessor
  • RequiredAnnotationBeanPostProcessor
那么,为什么要注册这四个BeanPostProcessor呢?

是为了让系统能够识别相应的注解。

例如:

1、如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

传统声明方式如下:

<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 

2、如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor  Bean。

3、如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

4、如果想使用@Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。


以上这些注解是很常用的,如果按照传统的方式进行配置将会非常繁琐,所以Spring给我们提供了一个简便的方式:<context:annotation-config/>,使用该元素可以自动声明以上注解。

注:由于<context:component-scan base-package=”xx.xx”/>也包含了自动注入上述Bean的功能,所以<context:annotation-config/> 可以省略。如果两者都进行了配置,则只有前者有效。

注:

<context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解。

<context:component-scan>除了具有<context:annotation-config>的功能之外,<context:component-scan>还可以在指定的package下扫描以及注册javabean 。

-----------------------------------------------------------------------------------------------------------------------------------

 <aop:aspectj-autoproxy /> <context:component-scan base-package="com.dx.mes.web" /> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="web_opi_zh_CN" /> <!-- <property name="useCodeAsDefaultMessage" value="true" /> --> </bean></beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值