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

version

parent b5609bb6
package cn.com.duiba.spring.test.web.config; 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.Configuration;
import org.springframework.context.annotation.Import;
/** /**
* Created by gyf . * Created by gyf .
* 2020/10/16 . * 2020/10/16 .
*/ */
@Configuration @Configuration
@Import(TestAopFactoryBeanImportBeanDefinitionRegistrar.class) //@Import(TestAopFactoryBeanImportBeanDefinitionRegistrar.class)
public class TestAopBeanConfig { public class TestAopBeanConfig {
// @RefreshScope @RefreshScope
// @Bean @Bean
// public TestAopBean testAopBean() { public TestAopBean testAopBean() {
// return new TestAopBean(); return new TestAopBean("主类");
// } }
// @Bean @Bean
// public static TestAopBeanPostProcessor testAopBeanPostProcessor(){ public static TestAopBeanPostProcessor testAopBeanPostProcessor(){
// return new TestAopBeanPostProcessor(); return new TestAopBeanPostProcessor();
// } }
} }
package cn.com.duiba.spring.test.web.config; package cn.com.duiba.spring.test.web.config;
import cn.com.duiba.spring.test.web.service.TestAopBean; 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.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ProxyFactory; import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
/** /**
* Created by gyf . * Created by gyf .
* 2020/10/16 . * 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 @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean.getClass() == TestAopBean.class) { if (bean.getClass() == TestAopBean.class) {
TestAopBean normalBean = (TestAopBean) bean;
TestAopBean perftestBean = new TestAopBean("代理类");
map.put(normalBean, perftestBean);
ProxyFactory factory = new ProxyFactory(); ProxyFactory factory = new ProxyFactory();
factory.setTarget(bean); factory.setTarget(bean);
factory.addAdvice(new TestAopBeanMethodInterceptor()); factory.addAdvice(new TestAopBeanMethodInterceptor(normalBean, perftestBean));
Object proxy = factory.getProxy(); return factory.getProxy();
return proxy;
} }
return bean; return bean;
} }
...@@ -32,11 +52,27 @@ public class TestAopBeanPostProcessor implements BeanPostProcessor { ...@@ -32,11 +52,27 @@ public class TestAopBeanPostProcessor implements BeanPostProcessor {
private static class TestAopBeanMethodInterceptor implements MethodInterceptor { 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 @Override
public Object invoke(MethodInvocation invocation) throws Throwable { public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = invocation.getMethod().getName(); 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(); return invocation.proceed();
} }
} }
} }
...@@ -37,6 +37,7 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin ...@@ -37,6 +37,7 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
@Override @Override
public TestAopBean getObject() throws Exception { public TestAopBean getObject() throws Exception {
System.out.println(this + " getObject");
return testAopBean; return testAopBean;
} }
...@@ -54,4 +55,5 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin ...@@ -54,4 +55,5 @@ public class TestAopFactoryBean implements FactoryBean<TestAopBean>, Initializin
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
testAopBean.afterPropertiesSet(); testAopBean.afterPropertiesSet();
} }
} }
...@@ -31,4 +31,5 @@ public class TestAopFactoryBeanImportBeanDefinitionRegistrar implements ImportBe ...@@ -31,4 +31,5 @@ public class TestAopFactoryBeanImportBeanDefinitionRegistrar implements ImportBe
beanDefinitionHolder = ScopedProxyUtils.createScopedProxy(beanDefinitionHolder, registry, true); beanDefinitionHolder = ScopedProxyUtils.createScopedProxy(beanDefinitionHolder, registry, true);
BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry); BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry);
} }
} }
...@@ -21,8 +21,8 @@ public class TestAopBeanController { ...@@ -21,8 +21,8 @@ public class TestAopBeanController {
@SneakyThrows @SneakyThrows
@GetMapping("/test") @GetMapping("/test")
public String test() { public String test() {
testAopBean.destroy(); // testAopBean.destroy();
testAopBean.afterPropertiesSet(); // testAopBean.afterPropertiesSet();
return testAopBean.test(); return testAopBean.test();
} }
} }
...@@ -9,18 +9,35 @@ import org.springframework.beans.factory.InitializingBean; ...@@ -9,18 +9,35 @@ import org.springframework.beans.factory.InitializingBean;
*/ */
public class TestAopBean implements InitializingBean, DisposableBean { 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 @Override
public void destroy() throws Exception { public void destroy() throws Exception {
// System.out.println("我被销毁了" + ExceptionUtils.getStackTrace(new Exception())); // System.out.println("我被销毁了" + ExceptionUtils.getStackTrace(new Exception()));
System.out.println("我被销毁了"); System.out.println("我被销毁了 " + name);
} }
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
System.out.println("我初始化完成了"); System.out.println("我初始化完成了 " + name);
} }
public String test() { 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