当我们把nacos服务端启动起来,项目中也集成好之后,兴高采烈的启动项目准备试一下,发现在nacos中修改配置之后发现项目中的配置竟然没有刷新,然后开始怀疑是不是自己那里配置的不对、那个注解没有写、nacos版本是不是和spring不兼容,然后一通修改,最后发现还是不行,最后开始怀疑人生
本次使用nacos 2.0
spring版本
<spring.version>4.3.16.RELEASE</spring.version>
<jackson.version>2.7.0</jackson.version>
<struts2.version>2.5.8</struts2.version>
<mybatis.version>3.0.5</mybatis.version>
<mysql.version>5.1.20</mysql.version>
<nacos.version>1.1.1</nacos.version>
<!-- naocs -->
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>${nacos.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<!-- -->
<exclusion>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
一、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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
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-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"
default-autowire="byName" default-init-method="initCheck">
<context:annotation-config/>
<context:component-scan base-package="com.*.*"/>
<import resource="classpath*:applicationContext-nacos.xml" />
<import resource="classpath*:dataSource.xml" />
<import resource="classpath*:conf/spring/*_*.xml"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.password}" />
<property name="poolConfig" ref="jedisPoolConfig" />
</bean>
<bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="${redis.maxInactiveIntervalInSeconds}" />
</bean>
</beans>
二、applicationContext-nacos.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:nacos="http://nacos.io/schema/nacos"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://nacos.io/schema/nacos
http://nacos.io/schema/nacos.xsd">
<!--开启注解-->
<nacos:annotation-driven/>
<!--指定nacos配置地址-->
<nacos:global-properties server-addr="ip:8848" namespace="64339721-542e-46b8-bd22-b1f9a9b19c44"/>
<!--指定dataId,group-id, 是否是自动刷新-->
<nacos:property-source data-id="****.properties" auto-refreshed="true"/>
</beans>
三、dataSource.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="minIdle" value="${jdbc.minIdle}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
<property name="removeAbandoned" value="${jdbc.removeAbandoned}"></property>
<property name="removeAbandonedTimeout" value="${connection.removeAbandonedTimeout}"></property>
<property name="logAbandoned" value="${jdbc.logAbandoned}"></property>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"></property>
<property name="testWhileIdle" value="${jdbc.testWhileIdle}"></property>
<property name="validationQuery" value="${jdbc.validationQuery}"></property>
</bean>
<bean id="sqlSessionFactory" class="*.dao.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"/>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true"/>
<tx:method name="list*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED" rollback-for="Exception,RunTimeException"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.*..*.*.service..*.*(..))" advice-ref="txAdvice"/>
</aop:config>
</beans>
配置完成以后直接使用 @Value,如果这里使用 @NacosValue 将不会实时刷新,原因我也没找到,有可能是老框架的原因吧
@Value("${properties}")
private String userSmsUrl;
//如果是静态方法怎么获取参数呢
private static String imageUrl;
public static String getImageUrl() {
return imageUrl;
}
@Value("${image.url}")
public void setImageUrl(String imageUrl) {
this.imageUrl= imageUrl;
}
//然后在get获取就行了