|
16 | 16 | */ |
17 | 17 | package com.alibaba.nacos.spring.beans.factory.annotation; |
18 | 18 |
|
| 19 | +import java.util.Map; |
| 20 | +import java.util.Properties; |
| 21 | + |
19 | 22 | import com.alibaba.nacos.api.annotation.NacosProperties; |
20 | 23 | import com.alibaba.nacos.api.exception.NacosException; |
21 | 24 | import com.alibaba.nacos.spring.factory.NacosServiceFactory; |
22 | 25 | import com.alibaba.nacos.spring.util.GlobalNacosPropertiesSource; |
23 | 26 | import com.alibaba.nacos.spring.util.NacosUtils; |
| 27 | + |
24 | 28 | import org.springframework.beans.factory.BeanCreationException; |
25 | 29 | import org.springframework.beans.factory.BeanFactory; |
26 | 30 | import org.springframework.beans.factory.BeanFactoryAware; |
27 | 31 | import org.springframework.context.EnvironmentAware; |
28 | 32 | import org.springframework.core.env.Environment; |
29 | 33 |
|
30 | | -import java.util.Map; |
31 | | -import java.util.Properties; |
32 | | - |
33 | 34 | import static com.alibaba.nacos.spring.util.NacosBeanUtils.getNacosServiceFactoryBean; |
34 | 35 | import static com.alibaba.spring.util.ClassUtils.resolveGenericType; |
35 | 36 | import static java.lang.String.format; |
|
42 | 43 | * @author <a href="mailto:[email protected]">Mercy</a> |
43 | 44 | * @since 0.1.0 |
44 | 45 | */ |
45 | | -public abstract class AbstractNacosServiceBeanBuilder<S> implements BeanFactoryAware, EnvironmentAware { |
46 | | - |
47 | | - private BeanFactory beanFactory; |
48 | | - |
49 | | - private Environment environment; |
50 | | - |
51 | | - private final Class<?> type; |
52 | | - |
53 | | - private final GlobalNacosPropertiesSource source; |
54 | | - |
55 | | - protected AbstractNacosServiceBeanBuilder(GlobalNacosPropertiesSource source) { |
56 | | - type = resolveGenericType(getClass()); |
57 | | - this.source = source; |
58 | | - } |
59 | | - |
60 | | - public S build(NacosProperties nacosProperties) { |
61 | | - return build(getAnnotationAttributes(nacosProperties)); |
62 | | - } |
63 | | - |
64 | | - public S build(Map<String, Object> nacosPropertiesAttributes) { |
65 | | - |
66 | | - NacosServiceFactory nacosServiceFactory = getNacosServiceFactoryBean(beanFactory); |
67 | | - Properties properties = resolveProperties(nacosPropertiesAttributes); |
68 | | - |
69 | | - if (properties.isEmpty()) { |
70 | | - throw new BeanCreationException( |
71 | | - format("The @%s attributes must be configured", |
72 | | - NacosProperties.class.getSimpleName())); |
73 | | - } |
74 | | - |
75 | | - try { |
76 | | - return createService(nacosServiceFactory, properties); |
77 | | - } catch (NacosException e) { |
78 | | - throw new BeanCreationException(e.getErrMsg(), e); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - /** |
83 | | - * Subtype would implement this method to create target Nacos Service |
84 | | - * |
85 | | - * @param nacosServiceFactory {@link NacosServiceFactory} |
86 | | - * @param properties {@link Properties} |
87 | | - * @return target Nacos Service instance |
88 | | - * @throws NacosException When Nacos Service creation is failed |
89 | | - */ |
90 | | - protected abstract S createService(NacosServiceFactory nacosServiceFactory, Properties properties) throws NacosException; |
91 | | - |
92 | | - /** |
93 | | - * Resolve Nacos {@link Properties} from {@link NacosProperties @NacosProperties} |
94 | | - * |
95 | | - * @param nacosProperties {@link NacosProperties @NacosProperties} |
96 | | - * @return non-null |
97 | | - */ |
98 | | - public final Properties resolveProperties(NacosProperties nacosProperties) { |
99 | | - Properties globalNacosProperties = resolveGlobalNacosProperties(); |
100 | | - return NacosUtils.resolveProperties(nacosProperties, environment, globalNacosProperties); |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * Resolve Nacos {@link Properties} from {@link NacosProperties @NacosProperties} |
105 | | - * |
106 | | - * @param nacosPropertiesAttributes {@link NacosProperties Nacos Properties}'s attributes |
107 | | - * @return non-null |
108 | | - */ |
109 | | - public final Properties resolveProperties(Map<String, Object> nacosPropertiesAttributes) { |
110 | | - Properties globalNacosProperties = resolveGlobalNacosProperties(); |
111 | | - return NacosUtils.resolveProperties(nacosPropertiesAttributes, environment, globalNacosProperties); |
112 | | - } |
113 | | - |
114 | | - private Properties resolveGlobalNacosProperties() { |
115 | | - return source.getMergedGlobalProperties(beanFactory); |
116 | | - } |
117 | | - |
118 | | - |
119 | | - final Class<?> getType() { |
120 | | - return type; |
121 | | - } |
122 | | - |
123 | | - @Override |
124 | | - public void setBeanFactory(BeanFactory beanFactory) { |
125 | | - this.beanFactory = beanFactory; |
126 | | - } |
127 | | - |
128 | | - @Override |
129 | | - public void setEnvironment(Environment environment) { |
130 | | - this.environment = environment; |
131 | | - } |
| 46 | +public abstract class AbstractNacosServiceBeanBuilder<S> |
| 47 | + implements BeanFactoryAware, EnvironmentAware { |
| 48 | + |
| 49 | + private BeanFactory beanFactory; |
| 50 | + |
| 51 | + private Environment environment; |
| 52 | + |
| 53 | + private final Class<?> type; |
| 54 | + |
| 55 | + private final GlobalNacosPropertiesSource source; |
| 56 | + |
| 57 | + protected AbstractNacosServiceBeanBuilder(GlobalNacosPropertiesSource source) { |
| 58 | + type = resolveGenericType(getClass()); |
| 59 | + this.source = source; |
| 60 | + } |
| 61 | + |
| 62 | + public S build(NacosProperties nacosProperties) { |
| 63 | + return build(getAnnotationAttributes(nacosProperties)); |
| 64 | + } |
| 65 | + |
| 66 | + public S build(Map<String, Object> nacosPropertiesAttributes) { |
| 67 | + |
| 68 | + NacosServiceFactory nacosServiceFactory = getNacosServiceFactoryBean(beanFactory); |
| 69 | + Properties properties = resolveProperties(nacosPropertiesAttributes); |
| 70 | + |
| 71 | + if (properties.isEmpty()) { |
| 72 | + throw new BeanCreationException( |
| 73 | + format("The @%s attributes must be configured", |
| 74 | + NacosProperties.class.getSimpleName())); |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + return createService(nacosServiceFactory, properties); |
| 79 | + } |
| 80 | + catch (NacosException e) { |
| 81 | + throw new BeanCreationException(e.getErrMsg(), e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Subtype would implement this method to create target Nacos Service |
| 87 | + * |
| 88 | + * @param nacosServiceFactory {@link NacosServiceFactory} |
| 89 | + * @param properties {@link Properties} |
| 90 | + * @return target Nacos Service instance |
| 91 | + * @throws NacosException When Nacos Service creation is failed |
| 92 | + */ |
| 93 | + protected abstract S createService(NacosServiceFactory nacosServiceFactory, |
| 94 | + Properties properties) throws NacosException; |
| 95 | + |
| 96 | + /** |
| 97 | + * Resolve Nacos {@link Properties} from {@link NacosProperties @NacosProperties} |
| 98 | + * |
| 99 | + * @param nacosProperties {@link NacosProperties @NacosProperties} |
| 100 | + * @return non-null |
| 101 | + */ |
| 102 | + public final Properties resolveProperties(NacosProperties nacosProperties) { |
| 103 | + Properties globalNacosProperties = resolveGlobalNacosProperties(); |
| 104 | + return NacosUtils.resolveProperties(nacosProperties, environment, |
| 105 | + globalNacosProperties); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Resolve Nacos {@link Properties} from {@link NacosProperties @NacosProperties} |
| 110 | + * |
| 111 | + * @param nacosPropertiesAttributes {@link NacosProperties Nacos Properties}'s |
| 112 | + * attributes |
| 113 | + * @return non-null |
| 114 | + */ |
| 115 | + public final Properties resolveProperties( |
| 116 | + Map<String, Object> nacosPropertiesAttributes) { |
| 117 | + Properties globalNacosProperties = resolveGlobalNacosProperties(); |
| 118 | + return NacosUtils.resolveProperties(nacosPropertiesAttributes, environment, |
| 119 | + globalNacosProperties); |
| 120 | + } |
| 121 | + |
| 122 | + private Properties resolveGlobalNacosProperties() { |
| 123 | + return source.getMergedGlobalProperties(beanFactory); |
| 124 | + } |
| 125 | + |
| 126 | + final Class<?> getType() { |
| 127 | + return type; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void setBeanFactory(BeanFactory beanFactory) { |
| 132 | + this.beanFactory = beanFactory; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void setEnvironment(Environment environment) { |
| 137 | + this.environment = environment; |
| 138 | + } |
132 | 139 | } |
0 commit comments