# 项目代码资源:
可能还在审核中,请等待。。。
https://download.csdn.net/download/chenhz2284/89433361
# 项目代码
【pom.xml】
<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=*
【IMainBean4.java】
package com.chz.myBeanMain.bean;
public interface IMainBean4
{
String getName();
}
【MainBean1.java】
package com.chz.myBeanMain.bean;
@Slf4j
@Getter
@Setter
@Component
public class MainBean1 {
public MainBean1()
{
log.info("chz >>> MainBean1.<init>()");
}
private String name = "MainBean1";
}
【MainBean2.java】
package com.chz.myBeanMain.bean;
@Slf4j
@Getter
@Setter
// @Component // 注意没有注解
public class MainBean2 {
public MainBean2()
{
log.info("chz >>> MainBean2.<init>()");
}
private String name = "MainBean2";
}
【MainBean3.java】
package com.chz.myBeanMain.bean;
@Slf4j
@Getter
@Setter
// @Component // 注意没有注解
public class MainBean3 {
public MainBean3()
{
log.info("chz >>> MainBean3.<init>()");
}
private String name = "MainBean3";
}
【MainBean4.java】
package com.chz.myBeanMain.bean;
@Slf4j
@Getter
@Setter
@Component
public class MainBean4 implements IMainBean4 {
public MainBean4()
{
log.info("chz >>> MainBean4.<init>()");
}
private String name = "MainBean4";
}
【MainBean4Proxy.java】
package com.chz.myBeanMain.bean;
public class MainBean4Proxy implements IMainBean4{
private MainBean4 mainBean4;
public MainBean4Proxy(MainBean4 mainBean4)
{
this.mainBean4 = mainBean4;
}
@Override
public String getName() {
return "[我被代理了:"+ mainBean4.getName()+"]";
}
}
【MainController.java】
package com.chz.myBeanMain.controller;
@Slf4j
@RestController
@RequestMapping("/main")
public class MainController {
@Autowired(required = false)
private MainBean1 mainBean1;
@Autowired(required = false)
private MainBean2 mainBean2;
@Autowired(required = false)
private MainBean3 mainBean3;
@Autowired(required = false)
private IMainBean4 mainBean4;
@GetMapping("/mainBean1")
public String mainBean1() {
return "mainBean1: " + mainBean1.getName();
}
@GetMapping("/mainBean2")
public String mainBean2() {
return "mainBean2: " + mainBean2.getName();
}
@GetMapping("/mainBean3")
public String mainBean3() {
return "mainBean3: " + mainBean3.getName();
}
@GetMapping("/mainBean4")
public String mainBean4() {
return "mainBean4: " + mainBean4.getName();
}
}
【MyBeanDefinitionRegistryPostProcessor.java】
package com.chz.myBeanMain.postProcessor;
@Slf4j
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor
{
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException
{
log.info("chz >>> MyBeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry():");
// 修改bean的属性
BeanDefinition mainBean1 = registry.getBeanDefinition("mainBean1");
mainBean1.getPropertyValues().add("name", "[mainBean1 - 我被MyBeanDefinitionRegistryPostProcessor改了]");
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
log.info("chz >>> MyBeanDefinitionRegistryPostProcessor.postProcessBeanFactory():");
// 添加一个bean
MainBean2 mainBean2 = new MainBean2();
mainBean2.setName(mainBean2.getName() + " - 我是通过MyBeanDefinitionRegistryPostProcessor新加的bean");
beanFactory.registerSingleton("mainBean2", mainBean2);
}
}
【MyBeanFactoryPostProcessor.java】
package com.chz.myBeanMain.postProcessor;
@Slf4j
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor
{
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
{
log.info("chz >>> MyBeanFactoryPostProcessor.postProcessBeanFactory():");
// 添加一个bean
MainBean3 mainBean3 = new MainBean3();
mainBean3.setName(mainBean3.getName() + " - 我是通过MyBeanFactoryPostProcessor新加的bean");
beanFactory.registerSingleton("mainBean3", mainBean3);
}
}
【MyBeanPostProcessor.java】
package com.chz.myBeanMain.postProcessor;
@Slf4j
@Component
public class MyBeanPostProcessor implements BeanPostProcessor
{
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
log.info("chz >>> MyBeanPostProcessor.postProcessBeforeInitialization(): {}", beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
{
log.info("chz >>> MyBeanPostProcessor.postProcessAfterInitialization(): {}", beanName);
if( (bean instanceof IMainBean4) && !(bean instanceof MainBean4Proxy)){
// 代码bean
bean = new MainBean4Proxy((MainBean4)bean);
}
return bean;
}
}
【MyBeanMainTest.java】
package com.chz.myBeanMain;
@SpringBootApplication
public class MyBeanMainTest {
public static void main(String[] args) {
SpringApplication.run(MyBeanMainTest.class, args);
}
}
# 运行与测试
运行【MyBeanMainTest】,然后:
访问【http://localhost:8080/main/mainBean1】
访问【http://localhost:8080/main/mainBean2】
访问【http://localhost:8080/main/mainBean3】
访问【http://localhost:8080/main/mainBean4】