# 代码结构
# 项目【myBeanBranch】
【pom.xml】
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.15.RELEASE</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
【BranchBean.java】
package com.chz.myBeanBranch.bean;
@Slf4j
@Getter
@Setter
// 被【ImportSelector】指过来的类不需要添加【@Component】
public class BranchBean {
private String name;
@PostConstruct
public void init()
{
name = "BranchBeanName put in from @PostConstruct";
}
}
【BranchController.java】
package com.chz.myBeanBranch.controller;
// 被【ImportSelector】指过来的类不需要添加【@RestController】,但要加【@ResponseBody】
@Slf4j
@ResponseBody
@RequestMapping("/branch")
public class BranchController {
@Autowired
private BranchBean branchBean;
@GetMapping("/test1")
public String test1() {
return "test1: " + branchBean.getName();
}
}
【BranchImportBean.java】
package com.chz.myBeanBranch.importSelector;
// 被【@Bean】指过来的类不需要添加【@Component】
@Getter
@Setter
public class BranchImportBean
{
private Integer id = 1;
private String name = "BranchImportBeanName";
}
【BranchImportConfiguration.java】
package com.chz.myBeanBranch.importSelector;
// 被【ImportSelector】指过来的类不需要添加【@Configuration】
public class BranchImportConfiguration
{
@Bean
public BranchImportBean branchImportBean() {
BranchImportBean branchBean = new BranchImportBean();
branchBean.setId(19);
branchBean.setName("I am branchImportBean!!!");
return branchBean;
}
}
【BranchImportSelector.java】
package com.chz.myBeanBranch.importSelector;
public class BranchImportSelector implements ImportSelector
{
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata)
{
// 这里想让哪个类变成bean就在返回值里面返回,所以ImportSelector可以用来做为动态添加bean的一个方法,
// 比如扫描某一个自定义注解,然后返回类名,变成bean
return new String[]{
BranchImportConfiguration.class.getName(),
BranchController.class.getName(),
BranchBean.class.getName()
};
}
}
【EnableBranchImportSelector.java】
package com.chz.myBeanBranch.importSelector;
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target(ElementType.TYPE)
@Import(BranchImportSelector.class) // 这里使用了@ImportSelector
public @interface EnableBranchImportSelector {
}
# 项目【myBeanMain】
【pom.xm】
<dependency>
<groupId>com.chz</groupId>
<artifactId>myBeanBranch</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
【application.properties】
server.port=8080
spring.application.name=myBeanMain
management.server.port=7001
management.endpoints.web.exposure.include=*
【MainBean.java】
package com.chz.myBeanMain.bean;
@Getter
@Setter
@Component
public class MainBean
{
@Autowired
private NotAnnotatedBean notAnnotatedBean;
@Autowired
private BranchBean branchBean;
@Autowired
private BranchImportBean branchImportBean;
}
【NotAnnotatedBean.java】
package com.chz.myBeanMain.bean;
// 被【@Import】指过来的bean不需要添加【@Component】
@Getter
@Setter
public class NotAnnotatedBean
{
private String name = "I am " + this.getClass().getSimpleName();
}
【MainController.java】
package com.chz.myBeanMain.controller;
@Slf4j
@RestController
@RequestMapping("/main")
public class MainController
{
@Autowired
private MainBean mainBean;
@GetMapping("/notAnnotatedBean")
public String notAnnotatedBean() {
return "notAnnotatedBean: " + mainBean.getNotAnnotatedBean().getName();
}
@GetMapping("/branchBean")
public String branchBean() {
return "branchBean: " + mainBean.getBranchBean().getName();
}
@GetMapping("/branchImportBean")
public String branchImportBean() {
return "branchImportBean: " + mainBean.getBranchImportBean().getName();
}
}
【MyBeanMainTest.java】
package com.chz.myBeanMain;
@Import(NotAnnotatedBean.class) // 这里使用了@Import
@EnableBranchImportSelector // 这里使用了@ImportSelector
@SpringBootApplication
public class MyBeanMainTest
{
public static void main(String[] args) {
SpringApplication.run(MyBeanMainTest.class, args);
}
}
# 测试
启动【MyBeanMainTest】,然后:
访问【http://localhost:8080/branch/test1】
访问【http://localhost:8080/main/branchBean】
访问【http://localhost:8080/main/branchImportBean】
访问【http://localhost:8080/main/notAnnotatedBean】