Alice52 / spring-5.2.x

spring source code
https://github.com/Alice52/spring-5.2.x/issues/2
0 stars 1 forks source link

[aop] spring boot aop #12

Closed Alice52 closed 2 years ago

Alice52 commented 4 years ago
  1. get current proxy failed using follow code:
 // TestServiceImpl service
ITestService proxy = (ITestService) AopContext.currentProxy();

@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}
  1. exception
2020-07-29 20:41:08.949 ERROR 3716 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.] with root cause

java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.
    at org.springframework.aop.framework.AopContext.currentProxy(AopContext.java:69) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
    at cn.edu.ntu.jsr303.service.TestServiceImpl.validateBetweenService(TestServiceImpl.java:56) ~[classes/:na]
    at cn.edu.ntu.jsr303.controller.TestController.validateBetweenService(TestController.java:79) ~[classes/:na]
    at cn.edu.ntu.jsr303.controller.TestController$$FastClassBySpringCGLIB$$732be859.invoke(<generated>) ~[classes/:na]
  1. if i use follow code to set exposeProxy
@Order(Ordered.LOWEST_PRECEDENCE)
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
      throws BeansException {
    BeanDefinition testServiceImpl = beanFactory.getBeanDefinition("testServiceImpl");
    MutablePropertyValues propertyValues = testServiceImpl.getPropertyValues();
    propertyValues.addPropertyValue("exposeProxy", true);
  }
}
  1. but exception changing
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testServiceImpl' defined in file [F:\repository\jsr303\validation\target\classes\cn\edu\ntu\jsr303\service\TestServiceImpl.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'exposeProxy' of bean class [cn.edu.ntu.jsr303.service.TestServiceImpl]: Bean property 'exposeProxy' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testServiceImpl' defined in file [F:\repository\jsr303\validation\target\classes\cn\edu\ntu\jsr303\service\TestServiceImpl.class]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'exposeProxy' of bean class [cn.edu.ntu.jsr303.service.TestServiceImpl]: Bean property 'exposeProxy' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'exposeProxy' of bean class [cn.edu.ntu.jsr303.service.TestServiceImpl]: Bean property 'exposeProxy' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

reference

  1. exposeProxy
  2. BeanCurrentlyInCreationException
  3. bug:exposeProxy
Alice52 commented 4 years ago

java.lang.IllegalStateException: Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available.

I really saw proxy-object property of exposeProxy is false but i donot when and how to change it to true.

Alice52 commented 4 years ago

循环依赖

  1. code
public class TestServiceImpl implements ITestService {

  // advice to use AopContext.currentProxy(): but still get error
  @Resource private ITestService testService;
}
  1. exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testServiceImpl': Bean with name 'testServiceImpl' has been injected into other beans [testServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.

Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'testServiceImpl': Bean with name 'testServiceImpl' has been injected into other beans [testServiceImpl] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.