@ConditionalOnSingleCandidate 注解用来判断指定类在 BeanFactory 中是否只有一个实例:
(1)如果在 BeanFactory 中存在多个实例,则匹配失败;
(2)如果在 BeanFactory 中仅仅存在一个实例,则匹配成功;
(1)创建 InitUser 和 InitOrder 初始化类,这两个类用作条件判断。代码如下:
a、InitUser.java
package com.huangx.springboot.autoconfig.init; public class InitUser { }
b、InitOrder.java
package com.huangx.springboot.autoconfig.init; public class InitOrder { }
(2)创建 UserService 和 OrderService 服务,代码如下:
a、UserService.java
package com.huangx.springboot.autoconfig.service; public class UserService { // 什么也不做 }
b、OrderService
package com.huangx.springboot.autoconfig.service; public class OrderService { // 什么也不做 }
(3)向 Spring 容器注入初始化对象,故意初始化两个 InitOrder 实例,我后续 @ConditionalOnSingleCandidate 注解创建条件。代码如下:
package com.huangx.springboot.autoconfig.config; import com.huangx.springboot.autoconfig.init.InitOrder; import com.huangx.springboot.autoconfig.init.InitUser; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class InitConfig { @Bean public InitUser initUser() { return new InitUser(); } @Bean public InitOrder initOrder() { return new InitOrder(); } @Bean public InitOrder initOrder2() { return new InitOrder(); } }
(4)创建用户和订单配置类,在配置类中使用 @ConditionalOnSingleCandidate 注解指定它们被创建的条件。代码如下:
a、UserConfig.java
package com.huangx.springboot.autoconfig.config; import com.huangx.springboot.autoconfig.init.InitUser; import com.huangx.springboot.autoconfig.service.UserService; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnSingleCandidate(InitUser.class) public class UserConfig { @Bean public UserService userService() { System.out.println("UserService -> userService()"); return new UserService(); } }
b、OrderConfig.java
package com.huangx.springboot.autoconfig.config; import com.huangx.springboot.autoconfig.init.InitOrder; import com.huangx.springboot.autoconfig.service.OrderService; import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnSingleCandidate(InitOrder.class) public class OrderConfig { @Bean public OrderService orderService() { System.out.println("OrderConfig -> orderService()"); return new OrderService(); } }
(5)客户端,验证用户和订单服务是否成功创建。代码如下:
package com.huangx.springboot.autoconfig; import com.huangx.springboot.autoconfig.service.OrderService; import com.huangx.springboot.autoconfig.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class Demo10Application { @Autowired private ApplicationContext applicationContext; public static void main(String[] args) { SpringApplication.run(Demo10Application.class, args); } @GetMapping("/") public String index() { UserService userService = null; try { userService = applicationContext.getBean(UserService.class); } catch (Exception e) { System.err.println(e.getMessage()); } OrderService orderService = null; try { orderService = applicationContext.getBean(OrderService.class); } catch (Exception e) { System.err.println(e.getMessage()); } return "userService=" + userService + "<br/>" + "orderService=" + orderService; } }
运行上面程序,在浏览器中访问 http://localhost:8080 地址,如下图: