Change auto-registration intitialization.
The import-selected AutoServiceRegistrationConfiguration now creates the configuration properties bean, which is used as a marker bean for AutoServiceRegistrationAutoConfiguration. This gives auto registration implementations a chance to create an AutoServiceRegistration impl bean.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package org.springframework.cloud.client.serviceregistry;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
|
||||
public class AutoServiceRegistrationAutoConfiguration {
|
||||
|
||||
@Autowired(required = false)
|
||||
private AutoServiceRegistration autoServiceRegistration;
|
||||
|
||||
@Autowired
|
||||
private AutoServiceRegistrationProperties properties;
|
||||
|
||||
@PostConstruct
|
||||
protected void init() {
|
||||
if (autoServiceRegistration == null && this.properties.isFailFast()) {
|
||||
throw new IllegalStateException("Auto Service Registration has been requested, but there is no AutoServiceRegistration bean");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,12 @@
|
||||
package org.springframework.cloud.client.serviceregistry;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(AutoServiceRegistrationProperties.class)
|
||||
public class AutoServiceRegistrationConfiguration {
|
||||
|
||||
@Autowired(required = false)
|
||||
private AutoServiceRegistration autoServiceRegistration;
|
||||
|
||||
@Autowired
|
||||
private AutoServiceRegistrationProperties properties;
|
||||
|
||||
@PostConstruct
|
||||
protected void init() {
|
||||
if (autoServiceRegistration == null && this.properties.isFailFast()) {
|
||||
throw new IllegalStateException("Auto Service Registration has been requested, but there is no AutoServiceRegistration bean");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@ConfigurationProperties("spring.cloud.service-registry.auto-registration")
|
||||
public class AutoServiceRegistrationProperties {
|
||||
|
||||
/** If Auto-Service Registration is enabled, default to true. */
|
||||
private boolean enabled = true;
|
||||
|
||||
/** Should startup fail if there is no AutoServiceRegistration, default to false. */
|
||||
private boolean failFast = false;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class AutoServiceRegistrationConfigurationTests {
|
||||
public class AutoServiceRegistrationAutoConfigurationTests {
|
||||
|
||||
@Rule
|
||||
public ExpectedException exception = ExpectedException.none();
|
||||
@@ -69,6 +69,7 @@ public class AutoServiceRegistrationConfigurationTests {
|
||||
private AnnotationConfigApplicationContext setup(String property, Class... classes) {
|
||||
ArrayList<Class> list = new ArrayList<>();
|
||||
list.add(AutoServiceRegistrationConfiguration.class);
|
||||
list.add(AutoServiceRegistrationAutoConfiguration.class);
|
||||
list.addAll(Arrays.asList(classes));
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
context.register(list.toArray(new Class[0]));
|
||||
Reference in New Issue
Block a user