用@Value注解直接注入properties中的值

有两种方式可以实现@Value注解直接将properties中的值注入变量,一种是@Value("${key}"),一种是@Value("#{beanName[key]}"),他们本质上是实现了不同的类

@Value("${key}"),实现PropertyPlaceholderConfigurer

demo:https://blog.csdn.net/lyz_112233/article/details/83105165

https://blog.csdn.net/lyz_112233/article/details/83105165

https://blog.csdn.net/qing_mei_xiu/article/details/53537409

1配置properties配置文件(在applicationContext中):

  • 使用<context:property-placeholder >标签,要配置多个properties文件则使用多个标签
<context:property-placeholder location="classpath:xxx.properties"
    ignore-unresolvable="true" />
  • 使用PropertyPlaceholderConfigurer配置,这样可以在list中配置多个properties文件
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   <property name="locations">

      <list>

        <value>classpath:jdbc.properties</value>

      </list>

   </property>

</bean>

 

2、Client类中Annotation的使用:

  • 注释变量,对变量值进行注入:
@Value("${server.ip}")
private String ip;
  • 注释方法,对输入值进行注入(set方法不能是static的):
@Value("${getToken}")

private void setTokenUrl(String tokenUrl) {

this.tokenUrl = tokenUrl;

}

 

二、@Value("#{beanName[key]}"),实现PropertiesFactoryBean

demo:https://blog.csdn.net/w605283073/article/details/49203141

1配置properties配置文件:可以将配置整体赋给Properties类型的类变量,也可以取出其中的一项赋值给String类型的类变量

  • <util:properties/> 标签

<util:properties/> 标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签

  • 使用PropertiesFactoryBean配置

<!-- <util:properties/> 标签的实现类是PropertiesFactoryBean,  

 直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的 -->  

 <bean id="settings"   

   class="org.springframework.beans.factory.config.PropertiesFactoryBean">  

   <property name="locations">  

  <list>  

    <value>file:/opt/rms/config/rms-mq.properties</value>  

    <value>file:/opt/rms/config/rms-env.properties</value>  

  </list>  

   </property>  

 </bean>  

</beans> 

 

2Client类中Annotation的使用,有三种方式:

  • 使用@Value("#{remoteSettings['remote.ip']}")  来注释一个string类型对象
  • 使用@Value("#{remoteSettings}")  来注释一个Properties对象
  • 使用第二种配置方式还可以使用@Autowired对Properties对象进行注入

 

  • 如果class的静态变量值(static)是获取不到值的

*注意,不管是哪种注入方式,变量都不能被static和final修饰,否则会出现变量获取不到值的情况

如果变量必须是static的,那么可以通过非static的setter方法来进行注入,此时@Value必须修饰在方法上,且set方法不能有static :

private static String CLUSTER_NAME;

@Value("${ES.CLUSTER_NAME}")

public void setClusterName(String clusterName) {

CLUSTER_NAME = clusterName;

}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:oscache="http://www.springmodules.org/schema/oscache" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- 对web包的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 mvc:annotation-driven --> <mvc:annotation-driven/> <!-- 扫描包 --> <context:annotation-config/> <context:component-scan base-package="com.org.*" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置jdbc --> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations"> <value>classpath:properties/jdbc.properties</value> </property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池启动时的初始 --> <property name="initialSize" value="1"/> <property name="maxActive" value="500"/> <property name="maxIdle" value="2"/> <property name="minIdle" value="1"/> </bean> <!-- 配置sessionFactory 注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.org.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <!-- 配置hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- Spring AOP config配置切点 --> <aop:config> <aop:pointcut expression="execution(public * com.org.service.*.*(..))" id="bussinessService" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> <!-- 配置那个类那个方法用到事务处理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 这个映射配置主要是用来进行静态资源的访问 --> <mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/> <mvc:resources mapping="/resource/**" location="/resource/" /> <mvc:resources mapping="/jsp/**" location="/jsp/" /> </beans>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值