Giter Club home page Giter Club logo

nacos-spring-project's Issues

Add @NacosService for auto-wiring ConfigService

@NacosService Programming Model is like below :

public class ConfigServiceTest {

    @NacosService(serverAddr = "${serverAddr}")
    private ConfigService configService;

    @Test
    public void testConfigService() throws NacosException {
        String dataId = "testDataId";
        String group = "testGroupId";
        Properties properties = new Properties();
        configService.publishConfig(dataId, group, "Hello,World");
        // Actively get the configuration.
        String content = configService.getConfig(dataId, group, 5000);
        System.out.println(content);
    }
}

Different "Properties" are returning the same "ConfigService" instance.

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Properties;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {CacheableEventPublishingNacosServiceFactory.class})
public class CacheableNacosServiceFactoryTest {

    @Autowired
    private NacosServiceFactory nacosServiceFactory;

    private Properties properties = new Properties();

    private Properties properties2 = new Properties();

    @Before
    public void init() {
        nacosServiceFactory = new CacheableEventPublishingNacosServiceFactory();

        properties.setProperty(PropertyKeyConst.NAMESPACE, "nc");
        properties.setProperty(PropertyKeyConst.SERVER_ADDR, "127.0.0.1");
        properties.setProperty(PropertyKeyConst.ACCESS_KEY, "a");
        properties.setProperty(PropertyKeyConst.SECRET_KEY, "s");

        properties2.setProperty(PropertyKeyConst.CLUSTER_NAME, "nc");
        properties2.setProperty(PropertyKeyConst.SERVER_ADDR, "127.0.0.1");
        properties2.setProperty(PropertyKeyConst.ACCESS_KEY, "a");
        properties2.setProperty(PropertyKeyConst.SECRET_KEY, "s");
    }

    @Test
    public void testCreateConfigService() throws NacosException {
        ConfigService configService = nacosServiceFactory.createConfigService(properties);
        ConfigService configService2 = nacosServiceFactory.createConfigService(properties2);
        // throws java.lang.AssertionError
        Assert.assertTrue(configService != configService2);
    }

    @Test
    public void testCreateNamingService() throws NacosException {
        NamingService namingService = nacosServiceFactory.createNamingService(properties);
        NamingService namingService2 = nacosServiceFactory.createNamingService(properties2);
        // throws java.lang.AssertionError
        Assert.assertTrue(namingService != namingService2);
    }
}

When using @EnableNacos and @EnableNacosConfig together, exception will be thrown

java.lang.IllegalStateException: Could not register object [java.util.concurrent.ThreadPoolExecutor@e383572[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]] under bean name 'nacosConfigListenerExecutor': there is already object [java.util.concurrent.ThreadPoolExecutor@5ddf0d24[Running, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]] bound
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:120) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerSingleton(DefaultListableBeanFactory.java:932) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerSingleton(NacosBeanUtils.java:115) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosConfigListenerExecutor(NacosBeanUtils.java:269) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at com.alibaba.nacos.spring.util.NacosBeanUtils.registerNacosConfigBeans(NacosBeanUtils.java:327) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at com.alibaba.nacos.spring.context.annotation.NacosConfigBeanDefinitionRegistrar.registerBeanDefinitions(NacosConfigBeanDefinitionRegistrar.java:53) ~[nacos-spring-context-0.1.0-SNAPSHOT.jar:na]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:358) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_141]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:357) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:145) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:328) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:233) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar:2.0.3.RELEASE]
at com.alibaba.boot.nacos.sample.ConfigApplication.main(ConfigApplication.java:65) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_141]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_141]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_141]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_141]
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]

Catch java.lang.ClassCastException in NacosPropertySourceBeanDefinitionParser

public static void main(String[] args) {
    new ClassPathXmlApplicationContext("classpath:/test.xml");
}

test.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:nacos="http://nacos.io/schema/nacos"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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:global-properties server-addr="127.0.0.1" />

    <nacos:property-source data-id="user" auto-refreshed="true" />

</beans>

The following exception is thrown at runtime

Caused by: java.lang.ClassCastException: org.springframework.beans.factory.support.DefaultListableBeanFactory cannot be cast to org.springframework.context.ApplicationEventPublisher
	at com.alibaba.nacos.spring.context.annotation.NacosPropertySourceBeanDefinitionParser.addPropertySourceAttribute(NacosPropertySourceBeanDefinitionParser.java:94)
	at com.alibaba.nacos.spring.context.annotation.NacosPropertySourceBeanDefinitionParser.parse(NacosPropertySourceBeanDefinitionParser.java:76)
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1432)
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1422)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:147)
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:101)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:495)
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)

Some test cases will affect each other

com.alibaba.nacos.spring.config.NacosNamespaceHandlerTest
com.alibaba.nacos.spring.context.config.xml.NacosPropertySourceBeanDefinitionParserTest

By default, once loaded, the configured ApplicationContext is reused for each test. Thus the setup cost is incurred only once per test suite, and subsequent test execution is much faster. In this context, the term test suite means all tests run in the same JVM — for example, all tests run from an Ant, Maven, or Gradle build for a given project or module. In the unlikely case that a test corrupts the application context and requires reloading — for example, by modifying a bean definition or the state of an application object — the TestContext framework can be configured to reload the configuration and rebuild the application context before executing the next test.

Context management and caching

DirtiesContext

Change the style of property placeholder to be Kebab case in @NacosProperties's default value

Before

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NacosProperties {

    String endpoint() default "${nacos.endpoint:}";

    String namespace() default "${nacos.namespace:}";

    String accessKey() default "${nacos.accessKey:}";

    String secretKey() default "${nacos.secretKey:}";

    String serverAddr() default "${nacos.serverAddr:}";

    String contextPath() default "${nacos.contextPath:}";

    String clusterName() default "${nacos.clusterName:}";

    String encode() default "${nacos.encode:" + ENCODE + "}";

}

After

@Target(ElementType.ANNOTATION_TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NacosProperties {

    String endpoint() default "${nacos.endpoint:}";

    String namespace() default "${nacos.namespace:}";

    String accessKey() default "${nacos.access-key:}";

    String secretKey() default "${nacos.secret-key:}";

    String serverAddr() default "${nacos.server-addr:}";

    String contextPath() default "${nacos.context-path:}";

    String clusterName() default "${nacos.cluster-name:}";

    String encode() default "${nacos.encode:" + ENCODE + "}";

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.