spring注解 配置属性 Environment@PropertySource 配置数据源

来源:互联网 发布:vue.js 2.0 radio 编辑:程序博客网 时间:2024/05/29 00:30
http://blog.csdn.net/u011179993/article/details/51511364
http://www.importnew.com/1099.html

Environment

 环境,比如JDK环境,Servlet环境,Spring环境等等;每个环境都有自己的配置数据,如System.getProperties()、System.getenv()等可以拿到JDK环境数据;ServletContext.getInitParameter()可以拿到Servlet环境配置数据等等;也就是说Spring抽象了一个Environment来表示环境配置。

查看复制到剪贴板打印
  1. public interface Environment extends PropertyResolver {//继承PropertyResolver  
  2.   
  3.         //得到当前明确激活的剖面  
  4.     String[] getActiveProfiles();  
  5.   
  6.         //得到默认激活的剖面,而不是明确设置激活的  
  7.     String[] getDefaultProfiles();  
  8.    
  9.         //是否接受某些剖面  
  10.     boolean acceptsProfiles(String... profiles);  
  11.   
  12. }  

从API上可以看出,除了可以解析相应的属性信息外,还提供了剖面相关的API,目的是: 可以根据剖面有选择的进行注册组件/配置。比如对于不同的环境注册不同的组件/配置(正式机、测试机、开发机等的数据源配置)。它的主要几个实现如下所示:

 

MockEnvironment:模拟的环境,用于测试时使用;

StandardEnvironment:标准环境,普通Java应用时使用,会自动注册System.getProperties() 和 System.getenv()到环境;

StandardServletEnvironment:标准Servlet环境,其继承了StandardEnvironment,Web应用时使用,除了StandardEnvironment外,会自动注册ServletConfig(DispatcherServlet)、ServletContext及JNDI实例到环境;

  一般在配置数据源是都会使用xml的方式注入,key-value在properties中管理;spring4.X已有着比较完善的注解来替换xml的配置方式。

使用xml配置数据源

通常我们使用xml配置数据源,使用SpEL获取properties中的配置。 
applicationContext.xml 中配置 dataSource 及 PreferencesPlaceholderConfigurer,使用 PropertyPlaceholderConfigurer进行Bean属性替换

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>                <value>classpath:/jdbc.properties</value>            </list>        </property>        <property name="fileEncoding" value="utf-8"/>    </bean><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">    <property name="properties" ref="configProperties" /></bean><!-- 使用proxool连接池的数据源, --><bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">    <!-- 数据源的别名 -->    <property name="alias" value="${proxool.alias}" />     <!-- 驱动 -->    <property name="driver" value="${proxool.driver}" />     <!-- 链接URL  -->    <property name="driverUrl" value="${proxool.driverUrl}" />     <!-- 用户名-->    <property name="user" value="${proxool.user}" />    <!-- 密码 -->    <property name="password" value="${proxool.password}" />     <!-- 最大链接数-->    <property name="maximumConnectionCount" value="${proxool.maximumConnectionCount}" />     <!-- 最小链接数 -->    <property name="minimumConnectionCount" value="${proxool.minimumConnectionCount}" />     <!-- ...(略) --></bean> 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

jdbc.properties

proxool.alias=mySqlproxool.driver=com.mysql.jdbc.Driverproxool.driverUrl=jdbc:mysql://localhost:3306/test?characterEncoding=utf8proxool.user=rootproxool.password=rootproxool.maximumActiveTime=1200proxool.maximumConnectionCount=50#...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

使用javaBean配置数据源

DataSourceConfiguration类是数据源的javaBean配置方式,@Configuratio注解当前类, 
spring启动时会扫描被@Configuratio注解的类,注入当前类中配置的方法bean; 
当然别忘了启用注解扫描:

<context:annotation-config/>  <context:component-scan base-package="com.XXX.test.dateSource"></context:component-scan>
  • 1
  • 2
  • 1
  • 2

@value注解读取配置

@value中可以直接使用SpEL,获取properties配置,成员变量也不需要getter、setter,不过还是有一个前提,需要配置xml:

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>                <value>classpath:/jdbc.properties</value>            </list>        </property>        <property name="fileEncoding" value="utf-8"/>    </bean><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">    <property name="properties" ref="configProperties" /></bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

@Bean注解:spring扫面当前类时,注入每个有@Bean注解的方法的返回值Bean, name属性默认为返回值类类名首字母小写,这里自己设置name。

package com.XXX.test.dateSource;import org.logicalcobwebs.proxool.ProxoolDataSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configuratiopublic class DataSourceConfiguration{    @Value("${proxool.alias}")    private String alias;    @Value("${proxool.driver}")    private String driver;    @Value("${proxool.driverUrl}")    private String driverUrl;    @Value("${proxool.user}")    private String user;    @Value("${proxool.password}")    private String password;    //...    @Bean(name="dataSource")    public ProxoolDataSource dataSource(){         ProxoolDataSource proxoolDataSource = new ProxoolDataSource();         proxoolDataSource.setDriver(driver);         proxoolDataSource.setDriverUrl(driverUrl);         proxoolDataSource.setUser(user);         proxoolDataSource.setPassword(password);         //...         return proxoolDataSource;     } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

这时dataSource已被注入,使用时可注解注入,如下:

    @Autowired    private ProxoolDataSource dataSource;
  • 1
  • 2
  • 1
  • 2

@PropertySource注解读取配置

@PropertySource注解当前类,参数为对应的配置文件路径,这种方式加载配置文件,可不用在xml中配置PropertiesFactoryBean引入jdbc.properties,使用时方便得多,DataSourceConfiguration不再需要成员变量,取而代之的是需要注入一个Environment环境配置,使用env.getProperty(key)获取数据:

package com.XXX.test.dateSource;import org.logicalcobwebs.proxool.ProxoolDataSource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;@Configuratio@PropertySource("classpath:/jdbc.properties")public class DataSourceConfiguration{    @Autowired    private Environment env;    @Bean(name="dataSource")    public ProxoolDataSource dataSource(){         ProxoolDataSource proxoolDataSource = new ProxoolDataSource();         proxoolDataSource.setDriver(env.getProperty("proxool.alias"));         proxoolDataSource.setDriverUrl(env.getProperty("proxool.driver"));         proxoolDataSource.setUser(env.getProperty("proxool.user"));         proxoolDataSource.setPassword(env.getProperty("proxool.password"));         //...         return proxoolDataSource;     } }
原创粉丝点击