Commit 8651c310 authored by 郭燕飞's avatar 郭燕飞 💬

version

parent b5609bb6
package cn.com.duiba.spring.test.web.config;
import cn.com.duiba.spring.test.web.service.TestAopBean;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Created by gyf .
* 2020/10/16 .
*/
@Configuration
@Import(TestAopFactoryBeanImportBeanDefinitionRegistrar.class)
//@Import(TestAopFactoryBeanImportBeanDefinitionRegistrar.class)
public class TestAopBeanConfig {
// @RefreshScope
// @Bean
// public TestAopBean testAopBean() {
// return new TestAopBean();
// }
@RefreshScope
@Bean
public TestAopBean testAopBean() {
return new TestAopBean("主类");
}
// @Bean
// public static TestAopBeanPostProcessor testAopBeanPostProcessor(){
// return new TestAopBeanPostProcessor();
// }
@Bean
public static TestAopBeanPostProcessor testAopBeanPostProcessor(){
return new TestAopBeanPostProcessor();
}
}
package cn.com.duiba.spring.test.web.config;
import cn.com.duiba.spring.test.web.service.TestAopBean;
import java.util.HashMap;
import java.util.Map;
import lombok.SneakyThrows;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
/**
* Created by gyf .
* 2020/10/16 .
*/
public class TestAopBeanPostProcessor implements BeanPostProcessor {
public class TestAopBeanPostProcessor implements DestructionAwareBeanPostProcessor {
private Map<TestAopBean, TestAopBean> map = new HashMap<>();
@SneakyThrows
@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
if (bean.getClass() == TestAopBean.class) {
TestAopBean perftestBean = map.get(bean);
map.remove(bean);
perftestBean.destroy();
System.out.println("我进来过,销毁");
}
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean.getClass() == TestAopBean.class) {
TestAopBean normalBean = (TestAopBean) bean;
TestAopBean perftestBean = new TestAopBean("代理类");
map.put(normalBean, perftestBean);
ProxyFactory factory = new ProxyFactory();
factory.setTarget(bean);
factory.addAdvice(new TestAopBeanMethodInterceptor());
Object proxy = factory.getProxy();
return proxy;
factory.addAdvice(new TestAopBeanMethodInterceptor(normalBean, perftestBean));
return factory.getProxy();
}
return bean;
}
......@@ -32,11 +52,27 @@ public class TestAopBeanPostProcessor implements BeanPostProcessor {
private static class TestAopBeanMethodInterceptor implements MethodInterceptor {
private TestAopBean normalBean;
private TestAopBean perftestBean;
public TestAopBeanMethodInterceptor(TestAopBean normalBean, TestAopBean perftestBean) {
this.normalBean = normalBean;
this.perftestBean = perftestBean;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = invocation.getMethod().getName();
System.out.println("我进来过," + methodName);
System.out.println("代理:我进来过," + methodName);
if (methodName.equals("afterPropertiesSet")) {
perftestBean.afterPropertiesSet();
return invocation.proceed();
} else if (methodName.equals("destroy")) {
perftestBean.destroy();
return invocation.proceed();
}
return invocation.proceed();
}
}
}
......@@ -37,6 +37,7 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
@Override
public TestAopBean getObject() throws Exception {
System.out.println(this + " getObject");
return testAopBean;
}
......@@ -54,4 +55,5 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
public void afterPropertiesSet() throws Exception {
testAopBean.afterPropertiesSet();
}
}
......@@ -31,4 +31,5 @@ public class TestAopFactoryBeanImportBeanDefinitionRegistrar implements ImportBe
beanDefinitionHolder = ScopedProxyUtils.createScopedProxy(beanDefinitionHolder, registry, true);
BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry);
}
}
......@@ -21,8 +21,8 @@ public class TestAopBeanController {
@SneakyThrows
@GetMapping("/test")
public String test() {
testAopBean.destroy();
testAopBean.afterPropertiesSet();
// testAopBean.destroy();
// testAopBean.afterPropertiesSet();
return testAopBean.test();
}
}
......@@ -9,18 +9,35 @@ import org.springframework.beans.factory.InitializingBean;
*/
public class TestAopBean implements InitializingBean, DisposableBean {
private String name;
public TestAopBean() {
}
public TestAopBean(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void destroy() throws Exception {
// System.out.println("我被销毁了" + ExceptionUtils.getStackTrace(new Exception()));
System.out.println("我被销毁了");
System.out.println("我被销毁了 " + name);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("我初始化完成了");
System.out.println("我初始化完成了 " + name);
}
public String test() {
return "ok";
return "ok " + name;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment