传统的spring项目如何集成nacos呢?

 

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;
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值