при использовании Spring Aop для прокси -сервера Bean в сочетании с пользовательским Beanpostprocessor (
Код: Выделить всё
MyPostProcessorКод: Выделить всё
MyFactoryBeanКод: Выделить всё
GoodByeServiceожидаемое поведение
HelloService Bean должен быть прокси -AOP, а следующие проверки должны вернуть true :
- AopUtils.isAopProxy(helloService)
- or AopUtils.isCglibProxy(helloService)
Код: Выделить всё
AopUtils.isJdkDynamicProxy(helloService)
фактическое поведение
HelloService Bean не прокси. Следующие проверки возвращают false < /code>: < /p>
Код: Выделить всё
AopUtils.isAopProxy(helloService)Код: Выделить всё
AopUtils.isJdkDynamicProxy(helloService)Код: Выделить всё
AopUtils.isCglibProxy(helloService)
шаги для воспроизведения
здесь - минимум репрессионо. />
pom file < /strong> < /h4>
[code]
4.0.0
spring-aop
com.cj.lb
spring-exploration
1.0-SNAPSHOT
6.0.11
17
UTF-8
org.springframework
spring-context
${spring-versrion}
org.springframework
spring-aop
${spring-versrion}
org.aspectj
aspectjweaver
1.9.5
[/code]
configuration class [/b]
Код: Выделить всё
@EnableAspectJAutoProxy
@Configuration
@ComponentScan
public class MyConfig {
@Bean
MyPostProcessor myPostProcessor(GoodByeService goodByeService) {
MyPostProcessor myPostProcessor = new MyPostProcessor();
myPostProcessor.setGoodByeService(goodByeService);
return myPostProcessor;
}
@Bean
MyFactoryBean myFactoryBean(HelloService helloService) {
MyFactoryBean myFactoryBean = new MyFactoryBean();
myFactoryBean.setHelloService(helloService);
return myFactoryBean;
}
}
beanpostprocessor
Код: Выделить всё
public class MyPostProcessor implements Ordered, BeanPostProcessor {
private GoodByeService goodByeService;
public GoodByeService getGoodByeService() {
return goodByeService;
}
public void setGoodByeService(GoodByeService goodByeService) {
this.goodByeService = goodByeService;
}
@Override
public int getOrder() {
return 0;
}
}
классы службы
Код: Выделить всё
@Service
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello() {
String result = "hello world";
System.out.println(result);
return result;
}
}
@Service
public class GoodByeServiceImpl implements GoodByeService {
@Override
public String sayGoodbye() {
return "Goodbye";
}
}
factorybean
Код: Выделить всё
public class MyFactoryBean implements FactoryBean {
private HelloService helloService;
public HelloService getHelloService() {
return helloService;
}
public void setHelloService(HelloService helloService) {
this.helloService = helloService;
}
@Override
public Object getObject() throws Exception {
return new MyBean();
}
@Override
public Class getObjectType() {
return MyBean.class;
}
public static class MyBean {
}
}
аспект
Код: Выделить всё
@Component
@Aspect
public class MyAspect {
@Pointcut("execution(* com.cj.lb.service.HelloService.sayHello(..))")
public void pointcut() {}
@AfterReturning(pointcut = "pointcut()", returning = "result")
public void afterReturning(JoinPoint jp, Object result) {
System.out.println("my aspect aop ...");
}
}
Основной класс
Код: Выделить всё
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
HelloService helloService = context.getBean(HelloService.class);
System.out.println("helloService.getClass() = " + helloService.getClass());
System.out.println("Is AOP Proxy: " + AopUtils.isAopProxy(helloService));
System.out.println("Is JDK Dynamic Proxy: " + AopUtils.isJdkDynamicProxy(helloService));
System.out.println("Is CGLIB Proxy: " + AopUtils.isCglibProxy(helloService));
context.close();
}
}
output
Код: Выделить всё
helloService.getClass() = class com.cj.lb.service.impl.HelloServiceImpl
Is AOP Proxy: false
Is JDK Dynamic Proxy: false
Is CGLIB Proxy: false
< /code>
< /h3>
[b] анализ и обходной путь < /strong> < /h3>
Проблема может быть решена любого из следующих: < /p>
[list]
[*] annoteaner in my in my in my in my codenean in my in my in spectianevice < /p>
. @Lazy[*]Adding a proper generic type declaration for MyFactoryBean.
[/list]
Question[/b]
Why does the presence of a Beanpostprocessor с зависимостями мешает созданию прокси -сервера AOP для HelloService ? Порядок инициализации или сроки создания прокси, особенно когда участвуют зависимости Beanpostprocessor и Factorybean . Дополнительные разъяснения будут оценены.
Подробнее здесь: https://stackoverflow.com/questions/795 ... pendencies