簡體   English   中英

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,所以我認為創建代理存在問題。

testProxyFactoryBean 是自動裝配到 testServiceImpl 而不是 testProxyFactoryBean

簡而言之

我不知道它是如何/為什么是:

在一個新的 spring 應用程序上並工作。

但:

我似乎無法讓它在我需要的 spring 應用程序上運行。

..可能可以修復!

使其一致

或者:

@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;
  ...

或:實施! (始終使用 Interface 和 Impl!)


詳細地

6.4. 使用 ProxyFactoryBean 創建 AOP 代理

尤其是 代理接口

所以“影響最小”(和 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;
  }
}

..請享用; ;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM