Skip to content

Commit 0126b26

Browse files
authored
Merge pull request #229 from mercyblitz/master
1.0.0
2 parents 8265710 + c6dba51 commit 0126b26

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

nacos-spring-context/src/main/java/com/alibaba/nacos/spring/context/annotation/config/NacosValueAnnotationBeanPostProcessor.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,25 @@
3939
import org.springframework.context.ApplicationListener;
4040
import org.springframework.context.EnvironmentAware;
4141
import org.springframework.core.MethodParameter;
42+
import org.springframework.core.annotation.AnnotationAttributes;
4243
import org.springframework.core.env.Environment;
4344
import org.springframework.util.ReflectionUtils;
4445

4546
import com.alibaba.nacos.api.config.annotation.NacosValue;
4647
import com.alibaba.nacos.common.utils.MD5Utils;
4748
import com.alibaba.nacos.spring.context.event.config.NacosConfigReceivedEvent;
48-
import com.alibaba.spring.beans.factory.annotation.AnnotationInjectedBeanPostProcessor;
49+
import com.alibaba.spring.beans.factory.annotation.AbstractAnnotationBeanPostProcessor;
4950

5051
/**
52+
* Injected {@link NacosValue}
5153
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation.
5254
*
5355
* @author <a href="mailto:[email protected]">hxy1991</a>
5456
* @see NacosValue
5557
* @since 0.1.0
5658
*/
57-
public class NacosValueAnnotationBeanPostProcessor extends
58-
AnnotationInjectedBeanPostProcessor<NacosValue> implements BeanFactoryAware,
59+
public class NacosValueAnnotationBeanPostProcessor
60+
extends AbstractAnnotationBeanPostProcessor implements BeanFactoryAware,
5961
EnvironmentAware, ApplicationListener<NacosConfigReceivedEvent> {
6062

6163
/**
@@ -80,11 +82,29 @@ public class NacosValueAnnotationBeanPostProcessor extends
8082

8183
private Environment environment;
8284

85+
public NacosValueAnnotationBeanPostProcessor() {
86+
super(NacosValue.class);
87+
}
88+
89+
@Override
90+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
91+
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
92+
throw new IllegalArgumentException(
93+
"NacosValueAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory");
94+
}
95+
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
96+
}
97+
98+
@Override
99+
public void setEnvironment(Environment environment) {
100+
this.environment = environment;
101+
}
102+
83103
@Override
84-
protected Object doGetInjectedBean(NacosValue annotation, Object bean,
104+
protected Object doGetInjectedBean(AnnotationAttributes attributes, Object bean,
85105
String beanName, Class<?> injectedType,
86-
InjectionMetadata.InjectedElement injectedElement) {
87-
String annotationValue = annotation.value();
106+
InjectionMetadata.InjectedElement injectedElement) throws Exception {
107+
String annotationValue = attributes.getString("value");
88108
String value = beanFactory.resolveEmbeddedValue(annotationValue);
89109

90110
Member member = injectedElement.getMember();
@@ -100,24 +120,10 @@ protected Object doGetInjectedBean(NacosValue annotation, Object bean,
100120
}
101121

102122
@Override
103-
protected String buildInjectedObjectCacheKey(NacosValue annotation, Object bean,
104-
String beanName, Class<?> injectedType,
123+
protected String buildInjectedObjectCacheKey(AnnotationAttributes attributes,
124+
Object bean, String beanName, Class<?> injectedType,
105125
InjectionMetadata.InjectedElement injectedElement) {
106-
return bean.getClass().getName() + annotation;
107-
}
108-
109-
@Override
110-
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
111-
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
112-
throw new IllegalArgumentException(
113-
"NacosValueAnnotationBeanPostProcessor requires a ConfigurableListableBeanFactory");
114-
}
115-
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
116-
}
117-
118-
@Override
119-
public void setEnvironment(Environment environment) {
120-
this.environment = environment;
126+
return bean.getClass().getName() + attributes;
121127
}
122128

123129
@Override

nacos-spring-context/src/main/java/com/alibaba/nacos/spring/context/event/config/EventPublishingConfigService.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Properties;
2121
import java.util.concurrent.Executor;
2222

23+
import org.springframework.beans.factory.DisposableBean;
2324
import org.springframework.context.ApplicationEventPublisher;
2425
import org.springframework.context.ConfigurableApplicationContext;
2526

@@ -35,7 +36,8 @@
3536
* @author <a href="mailto:[email protected]">Mercy</a>
3637
* @since 0.1.0
3738
*/
38-
public class EventPublishingConfigService implements ConfigService, NacosServiceMetaData {
39+
public class EventPublishingConfigService
40+
implements ConfigService, NacosServiceMetaData, DisposableBean {
3941

4042
private final ConfigService configService;
4143

@@ -144,4 +146,14 @@ private void publishEvent(NacosConfigEvent nacosConfigEvent) {
144146
public Properties getProperties() {
145147
return properties;
146148
}
149+
150+
/**
151+
* Destroy lifecycle method to invoke {@link #shutDown()}
152+
* @throws Exception
153+
* @since 1.0.0
154+
*/
155+
@Override
156+
public void destroy() throws Exception {
157+
shutDown();
158+
}
147159
}

nacos-spring-context/src/main/java/com/alibaba/nacos/spring/factory/DelegatingNamingService.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121
import java.util.Properties;
2222

23+
import org.springframework.beans.factory.DisposableBean;
24+
2325
import com.alibaba.nacos.api.exception.NacosException;
2426
import com.alibaba.nacos.api.naming.NamingService;
2527
import com.alibaba.nacos.api.naming.listener.EventListener;
@@ -37,7 +39,8 @@
3739
* @see NacosServiceMetaData
3840
* @since 0.1.0
3941
*/
40-
class DelegatingNamingService implements NamingService, NacosServiceMetaData {
42+
class DelegatingNamingService
43+
implements NamingService, NacosServiceMetaData, DisposableBean {
4144

4245
private final NamingService delegate;
4346

@@ -356,4 +359,14 @@ public void shutDown() throws NacosException {
356359
public Properties getProperties() {
357360
return properties;
358361
}
362+
363+
/**
364+
* Destroy lifecycle method to invoke {@link #shutDown()}
365+
* @throws Exception
366+
* @since 1.0.0
367+
*/
368+
@Override
369+
public void destroy() throws Exception {
370+
shutDown();
371+
}
359372
}

0 commit comments

Comments
 (0)