如何在 Spring 上有条件地创建 @Controller 和 @Service 实例
How to create @Controller and @Service instance conditionally on Spring
我正在使用 Spring Boot 1.5.9.
有没有办法把on/off变成@Controller
和@Services
?
诸如 @ConditionalOnProperty
、@Conditional
之类的东西。
@ConditionalController // <--- something like this
@RestController
public class PingController {
@Value("${version}")
private String version;
@RequestMapping(value = CoreHttpPathStore.PING, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Map<String, Object>> ping() throws Exception {
HashMap<String, Object> map = new HashMap<>();
map.put("message", "Welcome to our API");
map.put("date", new Date());
map.put("version", version);
map.put("status", HttpStatus.OK);
return new ResponseEntity<>(map, HttpStatus.OK);
}
}
然后使用一些配置 bean 来加载它。
您想尝试以编程方式加载 bean 吗?
您可以使用两种机制之一访问应用程序上下文
@Autowired private ApplicationContext appContext;
或者通过扩展 ApplicationAware
创建类似于 bean 工厂的东西
public class ApplicationContextProvider implements
ApplicationContextAware{
获得应用程序上下文的句柄后,您可以通过编程方式将 bean 添加到上下文。
默认情况下,在 Spring 中,所有已定义的 bean 及其依赖项都在创建应用程序上下文时创建。
我们可以通过配置延迟初始化的 bean 来关闭它,只有在需要时才会创建 bean 并注入其依赖项。
您可以通过配置application.properties
来启用惰性初始化。
spring.main.lazy-initialization=true
将 属性 值设置为 true 意味着应用程序中的所有 bean 都将使用惰性初始化。
所有定义的bean都将使用惰性初始化,除了那些我们明确配置为@Lazy(false)
的bean。
或者您可以通过 @Lazy
方法来完成。当我们把 @Lazy
注释放在 @Configuration
class 上时,它表示所有带有 @Bean
注释的方法都应该延迟加载。
@Lazy
@Configuration
@ComponentScan(basePackages = "com.app.lazy")
public class AppConfig {
@Bean
public Region getRegion(){
return new Region();
}
@Bean
public Country getCountry(){
return new Country();
}
}
@ConditionalOnProperty
也应该适用于控制器(或服务),因为它也是一个 Spring bean。
添加到您的 PingController
@ConditionalOnProperty(prefix="ping.controller",
name="enabled",
havingValue="true")
@RestController
public class PingController {...}
然后到application.properties转一下on/off
ping.controller.enabled=false
我正在使用 Spring Boot 1.5.9.
有没有办法把on/off变成@Controller
和@Services
?
诸如 @ConditionalOnProperty
、@Conditional
之类的东西。
@ConditionalController // <--- something like this
@RestController
public class PingController {
@Value("${version}")
private String version;
@RequestMapping(value = CoreHttpPathStore.PING, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<Map<String, Object>> ping() throws Exception {
HashMap<String, Object> map = new HashMap<>();
map.put("message", "Welcome to our API");
map.put("date", new Date());
map.put("version", version);
map.put("status", HttpStatus.OK);
return new ResponseEntity<>(map, HttpStatus.OK);
}
}
然后使用一些配置 bean 来加载它。
您想尝试以编程方式加载 bean 吗?
您可以使用两种机制之一访问应用程序上下文
@Autowired private ApplicationContext appContext;
或者通过扩展 ApplicationAware
创建类似于 bean 工厂的东西public class ApplicationContextProvider implements ApplicationContextAware{
获得应用程序上下文的句柄后,您可以通过编程方式将 bean 添加到上下文。
默认情况下,在 Spring 中,所有已定义的 bean 及其依赖项都在创建应用程序上下文时创建。
我们可以通过配置延迟初始化的 bean 来关闭它,只有在需要时才会创建 bean 并注入其依赖项。
您可以通过配置application.properties
来启用惰性初始化。
spring.main.lazy-initialization=true
将 属性 值设置为 true 意味着应用程序中的所有 bean 都将使用惰性初始化。
所有定义的bean都将使用惰性初始化,除了那些我们明确配置为@Lazy(false)
的bean。
或者您可以通过 @Lazy
方法来完成。当我们把 @Lazy
注释放在 @Configuration
class 上时,它表示所有带有 @Bean
注释的方法都应该延迟加载。
@Lazy
@Configuration
@ComponentScan(basePackages = "com.app.lazy")
public class AppConfig {
@Bean
public Region getRegion(){
return new Region();
}
@Bean
public Country getCountry(){
return new Country();
}
}
@ConditionalOnProperty
也应该适用于控制器(或服务),因为它也是一个 Spring bean。
添加到您的 PingController
@ConditionalOnProperty(prefix="ping.controller",
name="enabled",
havingValue="true")
@RestController
public class PingController {...}
然后到application.properties转一下on/off
ping.controller.enabled=false