Przeglądaj źródła

refactor(apollo-biz): Use constructor injection instead of field injection (#4826)

* add tech-support-qq-4.png

* Update README.md

* Enhance the user experience in the scenario of submitting duplicate keys

* Modify the key-value conflict exception prompt, adjust the code style

* refactor(apollo-biz): Use constructor injection instead of field injection

* [Clean code] Remove unused imports

* Correct the test semantics of the test case in ItemSetControllerTest (#4823)

* add tech-support-qq-4.png

* Update README.md

* Enhance the user experience in the scenario of submitting duplicate keys

* Modify the key-value conflict exception prompt, adjust the code style

* test(apollo-biz): Correct the test semantics of the test case in ItemSetControllerTest

* test(apollo-biz): Optimize the test case for ItemSetControllerTest

* test(apollo-biz): Optimize the test case for ItemSetControllerTest

* test(apollo-biz): Optimize the test case for ItemSetControllerTest

---------

Co-authored-by: Jason Song <nobodyiam@gmail.com>

* Refactor the code related to ReleaseMessage (#4822)

* add tech-support-qq-4.png

* Update README.md

* Enhance the user experience in the scenario of submitting duplicate keys

* Modify the key-value conflict exception prompt, adjust the code style

* refactor(apollo-biz): Refactor the code related to ReleaseMessage

* refactor(apollo-biz): Refactor the code related to ReleaseMessage

* test(apollo-admin): Optimize the test case

* test(apollo-admin): Optimize the test case

---------

Co-authored-by: Jason Song <nobodyiam@gmail.com>

* refactor(apollo-biz): Use constructor injection instead of field injection

* chore(apollo-portal): Adjusting the code style

---------

Co-authored-by: Jason Song <nobodyiam@gmail.com>
Co-authored-by: ZhangJian He <shoothzj@gmail.com>
kl 2 lat temu
rodzic
commit
2800a00e92
25 zmienionych plików z 188 dodań i 118 usunięć
  1. 6 6
      apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/grayReleaseRule/GrayReleaseRulesHolder.java
  2. 6 6
      apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/message/ReleaseMessageScanner.java
  3. 8 4
      apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/BizDBPropertySource.java
  4. 4 1
      apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/config/BizConfigTest.java
  5. 1 6
      apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/grayReleaseRule/GrayReleaseRulesHolderTest.java
  6. 1 5
      apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/message/ReleaseMessageScannerTest.java
  7. 1 3
      apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/BizDBPropertySourceTest.java
  8. 29 8
      apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/ConfigServiceAutoConfiguration.java
  9. 0 1
      apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2.java
  10. 2 2
      apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/AccessKeyServiceWithCache.java
  11. 6 3
      apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/config/AbstractConfigService.java
  12. 9 7
      apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/config/ConfigServiceWithCache.java
  13. 10 4
      apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/config/DefaultConfigService.java
  14. 5 5
      apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/config/ConfigServiceWithCacheTest.java
  15. 1 4
      apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/config/DefaultConfigServiceTest.java
  16. 0 2
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/PermissionValidator.java
  17. 8 5
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/RestTemplateFactory.java
  18. 0 1
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/PermissionController.java
  19. 8 5
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/PortalDBPropertySource.java
  20. 0 2
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/SystemRoleManagerService.java
  21. 6 2
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/AuthConfiguration.java
  22. 37 2
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/RoleConfiguration.java
  23. 11 7
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRoleInitializationService.java
  24. 24 15
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java
  25. 5 12
      apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/ldap/LdapUserService.java

+ 6 - 6
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/grayReleaseRule/GrayReleaseRulesHolder.java

@@ -43,7 +43,6 @@ import com.google.common.collect.TreeMultimap;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
 
 import java.util.List;
@@ -60,10 +59,8 @@ public class GrayReleaseRulesHolder implements ReleaseMessageListener, Initializ
   private static final Logger logger = LoggerFactory.getLogger(GrayReleaseRulesHolder.class);
   private static final Joiner STRING_JOINER = Joiner.on(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR);
 
-  @Autowired
-  private GrayReleaseRuleRepository grayReleaseRuleRepository;
-  @Autowired
-  private BizConfig bizConfig;
+  private final GrayReleaseRuleRepository grayReleaseRuleRepository;
+  private final BizConfig bizConfig;
 
   private int databaseScanInterval;
   private ScheduledExecutorService executorService;
@@ -74,7 +71,10 @@ public class GrayReleaseRulesHolder implements ReleaseMessageListener, Initializ
   //an auto increment version to indicate the age of rules
   private AtomicLong loadVersion;
 
-  public GrayReleaseRulesHolder() {
+  public GrayReleaseRulesHolder(final GrayReleaseRuleRepository grayReleaseRuleRepository,
+      final BizConfig bizConfig) {
+    this.grayReleaseRuleRepository = grayReleaseRuleRepository;
+    this.bizConfig = bizConfig;
     loadVersion = new AtomicLong();
     grayReleaseRuleCache = Multimaps.synchronizedSetMultimap(
         TreeMultimap.create(String.CASE_INSENSITIVE_ORDER, Ordering.natural()));

+ 6 - 6
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/message/ReleaseMessageScanner.java

@@ -29,7 +29,6 @@ import java.util.concurrent.TimeUnit;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
 
 import com.ctrip.framework.apollo.biz.config.BizConfig;
@@ -46,17 +45,18 @@ import com.google.common.collect.Lists;
 public class ReleaseMessageScanner implements InitializingBean {
   private static final Logger logger = LoggerFactory.getLogger(ReleaseMessageScanner.class);
   private static final int missingReleaseMessageMaxAge = 10; // hardcoded to 10, could be configured via BizConfig if necessary
-  @Autowired
-  private BizConfig bizConfig;
-  @Autowired
-  private ReleaseMessageRepository releaseMessageRepository;
+  private final BizConfig bizConfig;
+  private final ReleaseMessageRepository releaseMessageRepository;
   private int databaseScanInterval;
   private final List<ReleaseMessageListener> listeners;
   private final ScheduledExecutorService executorService;
   private final Map<Long, Integer> missingReleaseMessages; // missing release message id => age counter
   private long maxIdScanned;
 
-  public ReleaseMessageScanner() {
+  public ReleaseMessageScanner(final BizConfig bizConfig,
+      final ReleaseMessageRepository releaseMessageRepository) {
+    this.bizConfig = bizConfig;
+    this.releaseMessageRepository = releaseMessageRepository;
     listeners = Lists.newCopyOnWriteArrayList();
     executorService = Executors.newScheduledThreadPool(1, ApolloThreadFactory
         .create("ReleaseMessageScanner", true));

+ 8 - 4
apollo-biz/src/main/java/com/ctrip/framework/apollo/biz/service/BizDBPropertySource.java

@@ -41,15 +41,19 @@ public class BizDBPropertySource extends RefreshablePropertySource {
 
   private static final Logger logger = LoggerFactory.getLogger(BizDBPropertySource.class);
 
-  @Autowired
-  private ServerConfigRepository serverConfigRepository;
+  private final ServerConfigRepository serverConfigRepository;
 
-  public BizDBPropertySource(String name, Map<String, Object> source) {
+  public BizDBPropertySource(final String name,
+      final Map<String, Object> source,
+      final ServerConfigRepository serverConfigRepository) {
     super(name, source);
+    this.serverConfigRepository = serverConfigRepository;
   }
 
-  public BizDBPropertySource() {
+  @Autowired
+  public BizDBPropertySource(final ServerConfigRepository serverConfigRepository) {
     super("DBConfig", Maps.newConcurrentMap());
+    this.serverConfigRepository = serverConfigRepository;
   }
 
   String getCurrentDataCenter() {

+ 4 - 1
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/config/BizConfigTest.java

@@ -16,6 +16,7 @@
  */
 package com.ctrip.framework.apollo.biz.config;
 
+import com.ctrip.framework.apollo.biz.repository.ServerConfigRepository;
 import com.ctrip.framework.apollo.biz.service.BizDBPropertySource;
 import org.junit.Before;
 import org.junit.Test;
@@ -36,12 +37,14 @@ public class BizConfigTest {
 
   @Mock
   private ConfigurableEnvironment environment;
+  @Mock
+  private ServerConfigRepository serverConfigRepository;
 
   private BizConfig bizConfig;
 
   @Before
   public void setUp() throws Exception {
-    bizConfig = new BizConfig(new BizDBPropertySource());
+    bizConfig = new BizConfig(new BizDBPropertySource(serverConfigRepository));
     ReflectionTestUtils.setField(bizConfig, "environment", environment);
   }
 

+ 1 - 6
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/grayReleaseRule/GrayReleaseRulesHolderTest.java

@@ -35,7 +35,6 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
-import org.springframework.test.util.ReflectionTestUtils;
 
 import java.util.List;
 import java.util.Set;
@@ -64,11 +63,7 @@ public class GrayReleaseRulesHolderTest {
 
   @Before
   public void setUp() throws Exception {
-    grayReleaseRulesHolder = spy(new GrayReleaseRulesHolder());
-    ReflectionTestUtils.setField(grayReleaseRulesHolder, "bizConfig",
-                                 bizConfig);
-    ReflectionTestUtils.setField(grayReleaseRulesHolder, "grayReleaseRuleRepository",
-        grayReleaseRuleRepository);
+    grayReleaseRulesHolder = spy(new GrayReleaseRulesHolder(grayReleaseRuleRepository, bizConfig));
     idCounter = new AtomicLong();
   }
 

+ 1 - 5
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/message/ReleaseMessageScannerTest.java

@@ -30,7 +30,6 @@ import org.awaitility.Awaitility;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.springframework.test.util.ReflectionTestUtils;
 
 import java.util.concurrent.TimeUnit;
 
@@ -52,10 +51,7 @@ public class ReleaseMessageScannerTest extends AbstractUnitTest {
 
   @Before
   public void setUp() throws Exception {
-    releaseMessageScanner = new ReleaseMessageScanner();
-    ReflectionTestUtils
-        .setField(releaseMessageScanner, "releaseMessageRepository", releaseMessageRepository);
-    ReflectionTestUtils.setField(releaseMessageScanner, "bizConfig", bizConfig);
+    releaseMessageScanner = new ReleaseMessageScanner(bizConfig, releaseMessageRepository);
     databaseScanInterval = 100; //100 ms
     when(bizConfig.releaseMessageScanIntervalInMilli()).thenReturn(databaseScanInterval);
     releaseMessageScanner.afterPropertiesSet();

+ 1 - 3
apollo-biz/src/test/java/com/ctrip/framework/apollo/biz/service/BizDBPropertySourceTest.java

@@ -28,7 +28,6 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mock;
-import org.springframework.test.util.ReflectionTestUtils;
 
 import java.util.List;
 
@@ -55,8 +54,7 @@ public class BizDBPropertySourceTest extends AbstractUnitTest {
 
   @Before
   public void initTestData() {
-    propertySource = spy(new BizDBPropertySource());
-    ReflectionTestUtils.setField(propertySource, "serverConfigRepository", serverConfigRepository);
+    propertySource = spy(new BizDBPropertySource(serverConfigRepository));
 
     List<ServerConfig> configs = Lists.newLinkedList();
 

+ 29 - 8
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/ConfigServiceAutoConfiguration.java

@@ -19,6 +19,10 @@ package com.ctrip.framework.apollo.configservice;
 import com.ctrip.framework.apollo.biz.config.BizConfig;
 import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
 import com.ctrip.framework.apollo.biz.message.ReleaseMessageScanner;
+import com.ctrip.framework.apollo.biz.repository.GrayReleaseRuleRepository;
+import com.ctrip.framework.apollo.biz.repository.ReleaseMessageRepository;
+import com.ctrip.framework.apollo.biz.service.ReleaseMessageService;
+import com.ctrip.framework.apollo.biz.service.ReleaseService;
 import com.ctrip.framework.apollo.configservice.controller.ConfigFileController;
 import com.ctrip.framework.apollo.configservice.controller.NotificationController;
 import com.ctrip.framework.apollo.configservice.controller.NotificationControllerV2;
@@ -40,22 +44,32 @@ import org.springframework.security.crypto.password.NoOpPasswordEncoder;
 public class ConfigServiceAutoConfiguration {
 
   private final BizConfig bizConfig;
+  private final ReleaseService releaseService;
+  private final ReleaseMessageService releaseMessageService;
+  private final GrayReleaseRuleRepository grayReleaseRuleRepository;
 
-  public ConfigServiceAutoConfiguration(final BizConfig bizConfig) {
+  public ConfigServiceAutoConfiguration(final BizConfig bizConfig,
+      final ReleaseService releaseService,
+      final ReleaseMessageService releaseMessageService,
+      final GrayReleaseRuleRepository grayReleaseRuleRepository) {
     this.bizConfig = bizConfig;
+    this.releaseService = releaseService;
+    this.releaseMessageService = releaseMessageService;
+    this.grayReleaseRuleRepository = grayReleaseRuleRepository;
   }
 
   @Bean
   public GrayReleaseRulesHolder grayReleaseRulesHolder() {
-    return new GrayReleaseRulesHolder();
+    return new GrayReleaseRulesHolder(grayReleaseRuleRepository, bizConfig);
   }
 
   @Bean
   public ConfigService configService() {
     if (bizConfig.isConfigServiceCacheEnabled()) {
-      return new ConfigServiceWithCache();
+      return new ConfigServiceWithCache(releaseService, releaseMessageService,
+          grayReleaseRulesHolder());
     }
-    return new DefaultConfigService();
+    return new DefaultConfigService(releaseService, grayReleaseRulesHolder());
   }
 
   @Bean
@@ -64,8 +78,8 @@ public class ConfigServiceAutoConfiguration {
   }
 
   @Bean
-  public FilterRegistrationBean clientAuthenticationFilter(AccessKeyUtil accessKeyUtil) {
-    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
+  public FilterRegistrationBean<ClientAuthenticationFilter> clientAuthenticationFilter(AccessKeyUtil accessKeyUtil) {
+    FilterRegistrationBean<ClientAuthenticationFilter> filterRegistrationBean = new FilterRegistrationBean<>();
 
     filterRegistrationBean.setFilter(new ClientAuthenticationFilter(bizConfig, accessKeyUtil));
     filterRegistrationBean.addUrlPatterns("/configs/*");
@@ -83,6 +97,8 @@ public class ConfigServiceAutoConfiguration {
     private final GrayReleaseRulesHolder grayReleaseRulesHolder;
     private final ReleaseMessageServiceWithCache releaseMessageServiceWithCache;
     private final ConfigService configService;
+    private final BizConfig bizConfig;
+    private final ReleaseMessageRepository releaseMessageRepository;
 
     public MessageScannerConfiguration(
         final NotificationController notificationController,
@@ -90,18 +106,23 @@ public class ConfigServiceAutoConfiguration {
         final NotificationControllerV2 notificationControllerV2,
         final GrayReleaseRulesHolder grayReleaseRulesHolder,
         final ReleaseMessageServiceWithCache releaseMessageServiceWithCache,
-        final ConfigService configService) {
+        final ConfigService configService,
+        final BizConfig bizConfig,
+        final ReleaseMessageRepository releaseMessageRepository) {
       this.notificationController = notificationController;
       this.configFileController = configFileController;
       this.notificationControllerV2 = notificationControllerV2;
       this.grayReleaseRulesHolder = grayReleaseRulesHolder;
       this.releaseMessageServiceWithCache = releaseMessageServiceWithCache;
       this.configService = configService;
+      this.bizConfig = bizConfig;
+      this.releaseMessageRepository = releaseMessageRepository;
     }
 
     @Bean
     public ReleaseMessageScanner releaseMessageScanner() {
-      ReleaseMessageScanner releaseMessageScanner = new ReleaseMessageScanner();
+      ReleaseMessageScanner releaseMessageScanner = new ReleaseMessageScanner(bizConfig,
+          releaseMessageRepository);
       //0. handle release message cache
       releaseMessageScanner.addMessageListener(releaseMessageServiceWithCache);
       //1. handle gray release rule

+ 0 - 1
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/NotificationControllerV2.java

@@ -86,7 +86,6 @@ public class NotificationControllerV2 implements ReleaseMessageListener {
   private final Gson gson;
   private final BizConfig bizConfig;
 
-  @Autowired
   public NotificationControllerV2(
       final WatchKeysUtil watchKeysUtil,
       final ReleaseMessageServiceWithCache releaseMessageService,

+ 2 - 2
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/AccessKeyServiceWithCache.java

@@ -66,8 +66,8 @@ public class AccessKeyServiceWithCache implements InitializingBean {
   private ListMultimap<String, AccessKey> accessKeyCache;
   private ConcurrentMap<Long, AccessKey> accessKeyIdCache;
 
-  @Autowired
-  public AccessKeyServiceWithCache(AccessKeyRepository accessKeyRepository, BizConfig bizConfig) {
+  public AccessKeyServiceWithCache(final AccessKeyRepository accessKeyRepository,
+      final BizConfig bizConfig) {
     this.accessKeyRepository = accessKeyRepository;
     this.bizConfig = bizConfig;
 

+ 6 - 3
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/config/AbstractConfigService.java

@@ -24,14 +24,17 @@ import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
 import com.google.common.base.Strings;
 
 import java.util.Objects;
-import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * @author Jason Song(song_s@ctrip.com)
  */
 public abstract class AbstractConfigService implements ConfigService {
-  @Autowired
-  private GrayReleaseRulesHolder grayReleaseRulesHolder;
+
+  private final GrayReleaseRulesHolder grayReleaseRulesHolder;
+
+  protected AbstractConfigService(final GrayReleaseRulesHolder grayReleaseRulesHolder) {
+    this.grayReleaseRulesHolder = grayReleaseRulesHolder;
+  }
 
   @Override
   public Release loadConfig(String clientAppId, String clientIp, String clientLabel, String configAppId, String configClusterName,

+ 9 - 7
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/config/ConfigServiceWithCache.java

@@ -16,6 +16,7 @@
  */
 package com.ctrip.framework.apollo.configservice.service.config;
 
+import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
 import com.google.common.base.Strings;
 import com.google.common.cache.CacheBuilder;
 import com.google.common.cache.CacheLoader;
@@ -37,7 +38,6 @@ import java.util.Optional;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.List;
 import java.util.concurrent.TimeUnit;
@@ -59,11 +59,8 @@ public class ConfigServiceWithCache extends AbstractConfigService {
   private static final String TRACER_EVENT_CACHE_GET = "ConfigCache.Get";
   private static final String TRACER_EVENT_CACHE_GET_ID = "ConfigCache.GetById";
 
-  @Autowired
-  private ReleaseService releaseService;
-
-  @Autowired
-  private ReleaseMessageService releaseMessageService;
+  private final ReleaseService releaseService;
+  private final ReleaseMessageService releaseMessageService;
 
   private LoadingCache<String, ConfigCacheEntry> configCache;
 
@@ -71,7 +68,12 @@ public class ConfigServiceWithCache extends AbstractConfigService {
 
   private ConfigCacheEntry nullConfigCacheEntry;
 
-  public ConfigServiceWithCache() {
+  public ConfigServiceWithCache(final ReleaseService releaseService,
+      final ReleaseMessageService releaseMessageService,
+      final GrayReleaseRulesHolder grayReleaseRulesHolder) {
+    super(grayReleaseRulesHolder);
+    this.releaseService = releaseService;
+    this.releaseMessageService = releaseMessageService;
     nullConfigCacheEntry = new ConfigCacheEntry(ConfigConsts.NOTIFICATION_ID_PLACEHOLDER, null);
   }
 

+ 10 - 4
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/service/config/DefaultConfigService.java

@@ -18,11 +18,10 @@ package com.ctrip.framework.apollo.configservice.service.config;
 
 import com.ctrip.framework.apollo.biz.entity.Release;
 import com.ctrip.framework.apollo.biz.entity.ReleaseMessage;
+import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
 import com.ctrip.framework.apollo.biz.service.ReleaseService;
 import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
 
-import org.springframework.beans.factory.annotation.Autowired;
-
 /**
  * config service with no cache
  *
@@ -30,8 +29,15 @@ import org.springframework.beans.factory.annotation.Autowired;
  */
 public class DefaultConfigService extends AbstractConfigService {
 
-  @Autowired
-  private ReleaseService releaseService;
+  private final ReleaseService releaseService;
+  private final GrayReleaseRulesHolder grayReleaseRulesHolder;
+
+  public DefaultConfigService(final ReleaseService releaseService,
+      final GrayReleaseRulesHolder grayReleaseRulesHolder) {
+    super(grayReleaseRulesHolder);
+    this.releaseService = releaseService;
+    this.grayReleaseRulesHolder = grayReleaseRulesHolder;
+  }
 
   @Override
   protected Release findActiveOne(long id, ApolloNotificationMessages clientMessages) {

+ 5 - 5
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/config/ConfigServiceWithCacheTest.java

@@ -16,6 +16,7 @@
  */
 package com.ctrip.framework.apollo.configservice.service.config;
 
+import com.ctrip.framework.apollo.biz.grayReleaseRule.GrayReleaseRulesHolder;
 import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
 import com.google.common.collect.Lists;
 
@@ -31,7 +32,6 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
-import org.springframework.test.util.ReflectionTestUtils;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
@@ -55,6 +55,8 @@ public class ConfigServiceWithCacheTest {
   private Release someRelease;
   @Mock
   private ReleaseMessage someReleaseMessage;
+  @Mock
+  private GrayReleaseRulesHolder grayReleaseRulesHolder;
 
   private String someAppId;
   private String someClusterName;
@@ -65,10 +67,8 @@ public class ConfigServiceWithCacheTest {
 
   @Before
   public void setUp() throws Exception {
-    configServiceWithCache = new ConfigServiceWithCache();
-    ReflectionTestUtils.setField(configServiceWithCache, "releaseService", releaseService);
-    ReflectionTestUtils.setField(configServiceWithCache, "releaseMessageService", releaseMessageService);
-
+    configServiceWithCache = new ConfigServiceWithCache(releaseService, releaseMessageService,
+        grayReleaseRulesHolder);
     configServiceWithCache.initialize();
 
     someAppId = "someAppId";

+ 1 - 4
apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/service/config/DefaultConfigServiceTest.java

@@ -34,7 +34,6 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
-import org.springframework.test.util.ReflectionTestUtils;
 
 /**
  * @author Jason Song(song_s@ctrip.com)
@@ -62,9 +61,7 @@ public class DefaultConfigServiceTest {
 
   @Before
   public void setUp() throws Exception {
-    configService = new DefaultConfigService();
-    ReflectionTestUtils.setField(configService, "releaseService", releaseService);
-    ReflectionTestUtils.setField(configService, "grayReleaseRulesHolder", grayReleaseRulesHolder);
+    configService = new DefaultConfigService(releaseService, grayReleaseRulesHolder);
 
     someClientAppId = "1234";
     someConfigAppId = "1";

+ 0 - 2
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/PermissionValidator.java

@@ -24,7 +24,6 @@ import com.ctrip.framework.apollo.portal.service.RolePermissionService;
 import com.ctrip.framework.apollo.portal.service.SystemRoleManagerService;
 import com.ctrip.framework.apollo.portal.spi.UserInfoHolder;
 import com.ctrip.framework.apollo.portal.util.RoleUtils;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 @Component("permissionValidator")
@@ -36,7 +35,6 @@ public class PermissionValidator {
   private final AppNamespaceService appNamespaceService;
   private final SystemRoleManagerService systemRoleManagerService;
 
-  @Autowired
   public PermissionValidator(
           final UserInfoHolder userInfoHolder,
           final RolePermissionService rolePermissionService,

+ 8 - 5
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/component/RestTemplateFactory.java

@@ -21,7 +21,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClientBuilder;
 import org.springframework.beans.factory.FactoryBean;
 import org.springframework.beans.factory.InitializingBean;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
 import org.springframework.stereotype.Component;
@@ -32,13 +31,17 @@ import java.io.UnsupportedEncodingException;
 @Component
 public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
 
-  @Autowired
-  private HttpMessageConverters httpMessageConverters;
-  @Autowired
-  private PortalConfig portalConfig;
+  private final HttpMessageConverters httpMessageConverters;
+  private final PortalConfig portalConfig;
 
   private RestTemplate restTemplate;
 
+  public RestTemplateFactory(final HttpMessageConverters httpMessageConverters,
+      final PortalConfig portalConfig) {
+    this.httpMessageConverters = httpMessageConverters;
+    this.portalConfig = portalConfig;
+  }
+
   public RestTemplate getObject() {
     return restTemplate;
   }

+ 0 - 1
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/controller/PermissionController.java

@@ -57,7 +57,6 @@ public class PermissionController {
   private final SystemRoleManagerService systemRoleManagerService;
   private final PermissionValidator permissionValidator;
 
-  @Autowired
   public PermissionController(
           final UserInfoHolder userInfoHolder,
           final RolePermissionService rolePermissionService,

+ 8 - 5
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/PortalDBPropertySource.java

@@ -38,15 +38,18 @@ import java.util.Objects;
 public class PortalDBPropertySource extends RefreshablePropertySource {
   private static final Logger logger = LoggerFactory.getLogger(PortalDBPropertySource.class);
 
-  @Autowired
-  private ServerConfigRepository serverConfigRepository;
+  private final ServerConfigRepository serverConfigRepository;
 
-  public PortalDBPropertySource(String name, Map<String, Object> source) {
+  public PortalDBPropertySource(final String name,
+      final Map<String, Object> source,
+      final ServerConfigRepository serverConfigRepository) {
     super(name, source);
+    this.serverConfigRepository = serverConfigRepository;
   }
-
-  public PortalDBPropertySource() {
+  @Autowired
+  public PortalDBPropertySource(final ServerConfigRepository serverConfigRepository) {
     super("DBConfig", Maps.newConcurrentMap());
+    this.serverConfigRepository = serverConfigRepository;
   }
 
   @Override

+ 0 - 2
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/SystemRoleManagerService.java

@@ -22,7 +22,6 @@ import com.ctrip.framework.apollo.portal.util.RoleUtils;
 import javax.annotation.PostConstruct;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 @Service
@@ -42,7 +41,6 @@ public class SystemRoleManagerService {
 
   private final RoleInitializationService roleInitializationService;
 
-  @Autowired
   public SystemRoleManagerService(final RolePermissionService rolePermissionService,
                                   final PortalConfig portalConfig,
                                   final RoleInitializationService roleInitializationService) {

+ 6 - 2
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/AuthConfiguration.java

@@ -195,10 +195,14 @@ public class AuthConfiguration {
 
     private final LdapProperties properties;
     private final Environment environment;
+    private final LdapTemplate ldapTemplate;
 
-    public SpringSecurityLDAPAuthAutoConfiguration(final LdapProperties properties, final Environment environment) {
+    public SpringSecurityLDAPAuthAutoConfiguration(final LdapProperties properties,
+        final Environment environment,
+        final LdapTemplate ldapTemplate) {
       this.properties = properties;
       this.environment = environment;
+      this.ldapTemplate = ldapTemplate;
     }
 
     @Bean
@@ -222,7 +226,7 @@ public class AuthConfiguration {
     @Bean
     @ConditionalOnMissingBean(UserService.class)
     public UserService springSecurityUserService() {
-      return new LdapUserService();
+      return new LdapUserService(ldapTemplate);
     }
 
     @Bean

+ 37 - 2
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/configuration/RoleConfiguration.java

@@ -16,8 +16,15 @@
  */
 package com.ctrip.framework.apollo.portal.spi.configuration;
 
+import com.ctrip.framework.apollo.openapi.repository.ConsumerRoleRepository;
+import com.ctrip.framework.apollo.portal.component.config.PortalConfig;
+import com.ctrip.framework.apollo.portal.repository.PermissionRepository;
+import com.ctrip.framework.apollo.portal.repository.RolePermissionRepository;
+import com.ctrip.framework.apollo.portal.repository.RoleRepository;
+import com.ctrip.framework.apollo.portal.repository.UserRoleRepository;
 import com.ctrip.framework.apollo.portal.service.RoleInitializationService;
 import com.ctrip.framework.apollo.portal.service.RolePermissionService;
+import com.ctrip.framework.apollo.portal.spi.UserService;
 import com.ctrip.framework.apollo.portal.spi.defaultimpl.DefaultRoleInitializationService;
 import com.ctrip.framework.apollo.portal.spi.defaultimpl.DefaultRolePermissionService;
 import org.springframework.context.annotation.Bean;
@@ -28,13 +35,41 @@ import org.springframework.context.annotation.Configuration;
  */
 @Configuration
 public class RoleConfiguration {
+
+    private final RoleRepository roleRepository;
+    private final RolePermissionRepository rolePermissionRepository;
+    private final UserRoleRepository userRoleRepository;
+    private final PermissionRepository permissionRepository;
+    private final PortalConfig portalConfig;
+    private final ConsumerRoleRepository consumerRoleRepository;
+    private final UserService userService;
+
+    public RoleConfiguration(final RoleRepository roleRepository,
+        final RolePermissionRepository rolePermissionRepository,
+        final UserRoleRepository userRoleRepository,
+        final PermissionRepository permissionRepository,
+        final PortalConfig portalConfig,
+        final ConsumerRoleRepository consumerRoleRepository,
+        final UserService userService) {
+      this.roleRepository = roleRepository;
+      this.rolePermissionRepository = rolePermissionRepository;
+      this.userRoleRepository = userRoleRepository;
+      this.permissionRepository = permissionRepository;
+      this.portalConfig = portalConfig;
+      this.consumerRoleRepository = consumerRoleRepository;
+      this.userService = userService;
+    }
+
     @Bean
     public RoleInitializationService roleInitializationService() {
-        return new DefaultRoleInitializationService();
+        return new DefaultRoleInitializationService(rolePermissionService(), portalConfig,
+            permissionRepository);
     }
 
     @Bean
     public RolePermissionService rolePermissionService() {
-        return new DefaultRolePermissionService();
+        return new DefaultRolePermissionService(roleRepository, rolePermissionRepository,
+            userRoleRepository, permissionRepository, portalConfig, consumerRoleRepository,
+            userService);
     }
 }

+ 11 - 7
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRoleInitializationService.java

@@ -31,7 +31,6 @@ import com.ctrip.framework.apollo.portal.service.RolePermissionService;
 import com.ctrip.framework.apollo.portal.service.SystemRoleManagerService;
 import com.ctrip.framework.apollo.portal.util.RoleUtils;
 import com.google.common.collect.Sets;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.HashSet;
@@ -45,12 +44,17 @@ import java.util.stream.Stream;
  */
 public class DefaultRoleInitializationService implements RoleInitializationService {
 
-  @Autowired
-  private RolePermissionService rolePermissionService;
-  @Autowired
-  private PortalConfig portalConfig;
-  @Autowired
-  private PermissionRepository permissionRepository;
+  private final RolePermissionService rolePermissionService;
+  private final PortalConfig portalConfig;
+  private final PermissionRepository permissionRepository;
+
+  public DefaultRoleInitializationService(final RolePermissionService rolePermissionService,
+      final PortalConfig portalConfig,
+      final PermissionRepository permissionRepository) {
+    this.rolePermissionService = rolePermissionService;
+    this.portalConfig = portalConfig;
+    this.permissionRepository = permissionRepository;
+  }
 
   @Transactional
   public void initAppRoles(App app) {

+ 24 - 15
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/defaultimpl/DefaultRolePermissionService.java

@@ -36,7 +36,6 @@ import com.google.common.collect.Multimap;
 import com.google.common.collect.Sets;
 import java.util.Comparator;
 import java.util.LinkedHashSet;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 
@@ -52,20 +51,30 @@ import java.util.stream.StreamSupport;
  * Created by timothy on 2017/4/26.
  */
 public class DefaultRolePermissionService implements RolePermissionService {
-    @Autowired
-    private RoleRepository roleRepository;
-    @Autowired
-    private RolePermissionRepository rolePermissionRepository;
-    @Autowired
-    private UserRoleRepository userRoleRepository;
-    @Autowired
-    private PermissionRepository permissionRepository;
-    @Autowired
-    private PortalConfig portalConfig;
-    @Autowired
-    private ConsumerRoleRepository consumerRoleRepository;
-    @Autowired
-    private UserService userService;
+
+    private final RoleRepository roleRepository;
+    private final RolePermissionRepository rolePermissionRepository;
+    private final UserRoleRepository userRoleRepository;
+    private final PermissionRepository permissionRepository;
+    private final PortalConfig portalConfig;
+    private final ConsumerRoleRepository consumerRoleRepository;
+    private final UserService userService;
+
+    public DefaultRolePermissionService(final RoleRepository roleRepository,
+        final RolePermissionRepository rolePermissionRepository,
+        final UserRoleRepository userRoleRepository,
+        final PermissionRepository permissionRepository,
+        final PortalConfig portalConfig,
+        final ConsumerRoleRepository consumerRoleRepository,
+        final UserService userService) {
+      this.roleRepository = roleRepository;
+      this.rolePermissionRepository = rolePermissionRepository;
+      this.userRoleRepository = userRoleRepository;
+      this.permissionRepository = permissionRepository;
+      this.portalConfig = portalConfig;
+      this.consumerRoleRepository = consumerRoleRepository;
+      this.userService = userService;
+    }
 
     /**
      * Create role with permissions, note that role name should be unique

+ 5 - 12
apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/spi/ldap/LdapUserService.java

@@ -22,8 +22,6 @@ import static org.springframework.ldap.query.LdapQueryBuilder.query;
 
 import com.ctrip.framework.apollo.portal.entity.bo.UserInfo;
 import com.ctrip.framework.apollo.portal.spi.UserService;
-import com.ctrip.framework.apollo.portal.spi.configuration.LdapExtendProperties;
-import com.ctrip.framework.apollo.portal.spi.configuration.LdapProperties;
 import com.google.common.base.Strings;
 import com.google.common.collect.Sets;
 import java.util.ArrayList;
@@ -35,7 +33,6 @@ import java.util.TreeSet;
 import javax.naming.directory.Attribute;
 import javax.naming.ldap.LdapName;
 import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.dao.EmptyResultDataAccessException;
 import org.springframework.ldap.core.AttributesMapper;
@@ -59,11 +56,7 @@ import org.springframework.util.CollectionUtils;
  */
 public class LdapUserService implements UserService {
 
-  @Autowired
-  private LdapProperties ldapProperties;
-
-  @Autowired
-  private LdapExtendProperties ldapExtendProperties;
+  private final LdapTemplate ldapTemplate;
 
   /**
    * ldap search base
@@ -125,13 +118,13 @@ public class LdapUserService implements UserService {
   @Value("${ldap.group.groupMembership:}")
   private String groupMembershipAttrName;
 
-
-  @Autowired
-  private LdapTemplate ldapTemplate;
-
   private static final String MEMBER_OF_ATTR_NAME = "memberOf";
   private static final String MEMBER_UID_ATTR_NAME = "memberUid";
 
+  public LdapUserService(final LdapTemplate ldapTemplate) {
+    this.ldapTemplate = ldapTemplate;
+  }
+
   /**
    * 用户信息Mapper
    */