Spring ProxyFactoryBean @Autowired 不工作

Spring ProxyFactoryBean @Autowired not working

我需要从接口中拦截方法,并找到了 MethodInterceptor 的这个实现,我在一个新的 spring 应用程序上对其进行了测试并且有效。

问题是,我似乎无法让它在我需要它的 spring 应用程序上运行。

@Configuration
public class TestMethodConfig {

@Autowired
private TestService testService;


  @Bean
  @Primary
  public ProxyFactoryBean testProxyFactoryBean() {
    ProxyFactoryBean testProxyFactoryBean = new ProxyFactoryBean();
    testProxyFactoryBean.setTarget(testService);
    testProxyFactoryBean.setInterceptorNames("testMethodInterceptor");
    return testProxyFactoryBean;
  }
}

@Service
public class TestServiceImpl implements TestService{
  @Override
  public void testMethod(String test) {
    System.out.println("testService String");
  }
}

public interface TestService{
  void testMethod(String test);
}

@RestController
public class Controller {
  @Autowired
  private TestService testProxyFactoryBean;

  @GetMapping(value = "/test")
  public void test(){
    testProxyFactoryBean.testMethod("valor");
  }
}

@Component
public class TestMethodInterceptor implements MethodInterceptor {

  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable {
    System.out.println("before method");
    System.out.println("invocation: " +      Arrays.toString(invocation.getArguments()));
    Object retVal = invocation.proceed();
    System.out.println("after method");
    return retVal;
  }
}

我使用 Spring Actuator 来检查 bean 关系,我发现 Controller 上的 @Autowired TestService 应该被分配给 testProxyFactoryBean,但它却被分配给了 TestServiceImpl bean,所以我认为创建代理。

简而言之

我不知道 how/why 是:

on a new spring app and worked.

但是:

I can't seem to get it working on the spring application I need it to.

..可能会修复!

保持一致

或:

@Configuration
public class TestMethodConfig {

  @Autowired
  private TestService testService;
}
...
// !!
public class TestServiceImpl implements TestService{
  @Override
  public void testMethod(String test) {
    System.out.println("testService String");
  }
}
...
@Service // !!!
public interface TestService{
  void testMethod(String test);
}
...
@RestController
public class Controller {
  @Autowired
  private TestService testProxyFactoryBean;
  ...

或者:实施! (始终如一地使用接口和实现!)


详细

6.4. Using the ProxyFactoryBean to Create AOP Proxies

特别是。 Proxying Interfaces.

所以“影响最小”(和 java 配置)应该是:

@Configuration
public class TestMethodConfig {

  // !!! Impl from component-scan (@Service), NOT interface:
  @Autowired
  private TestServiceImpl testServiceImpl; // or define custom, or "inline"...

  @Bean
  @Primary // only if you need it, better would be: distinct!
  public ProxyFactoryBean testProxyFactoryBean() {
    ProxyFactoryBean testProxyFactoryBean = new ProxyFactoryBean();
     // !!! set proxyInterface as documented:
    testProxyFactoryBean.setProxyInterface(TestService.class);
    testProxyFactoryBean.setTarget(testServiceImpl);
    testProxyFactoryBean.setInterceptorNames("testMethodInterceptor");
    // ...
    return testProxyFactoryBean;
  }
}

..享受吧! ;)