pom.xml包依赖
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-spring-context</artifactId>
<version>0.1.0-RC1</version>
</dependency>
</dependencies>
yml配置
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<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="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Spring MVC Annotation-Driven -->
<mvc:annotation-driven/>
<!-- Spring Context Annotation-Driven -->
<context:annotation-config/>
<context:component-scan base-package="com.wolf.nacos.example.spring"/>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="true" version="3.0">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
代码编写
启动类代码
@Configuration
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
public class PromotionConfiguration {
@Bean
public Promotion promotion() {
return new Promotion();
}
}
#注解
@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
这个在springboot或者springcloud中就相当于通过配置文件进行设置
Promotion配置文件
import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties;
import com.alibaba.nacos.api.config.annotation.NacosIgnore;
import com.alibaba.nacos.api.config.annotation.NacosProperty;
/**
* @author hexu.hxy
* @date 2019/1/6
*
* Nacos 控制台添加配置:
* Data ID:promotion.properties
* Group:spring-example
* 配置内容:
* sku=1
* price=99.9
* amount=1000
* desc=desc
* description=description
* sold=99
*
*/
@NacosConfigurationProperties(dataId = "promotion.properties", groupId = "spring-example", autoRefreshed = true)
public class Promotion {
private long sku;
private double price;
private int amount;
@NacosProperty(value = "desc")//属性转化,意思将desc配置的属性设置为description
private String description;
@NacosIgnore//忽略设置的属性
private int sold;
//忽略get/set方法
}
@Controller
@RequestMapping("promotion")
public class PromotionController {
@Autowired
private Promotion promotion;
@RequestMapping(method = GET)
@ResponseBody
public Promotion get() {
return promotion;
}
}