Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有更好的方法使用JavaConfig配置拦截器?_Java_Spring_Interceptor_Spring Java Config - Fatal编程技术网

有没有更好的方法使用JavaConfig配置拦截器?

有没有更好的方法使用JavaConfig配置拦截器?,java,spring,interceptor,spring-java-config,Java,Spring,Interceptor,Spring Java Config,我正在将XMLSpring配置移植到JavaConfig。 我的带有拦截器的bean定义如下所示: @Autowired private MyService myServiceImpl; @Bean MyService myService() { final ProxyFactoryBean proxy = new ProxyFactoryBean(); final Class<?>[] proxyInterfaces = { MyService.class };

我正在将XMLSpring配置移植到JavaConfig。 我的带有拦截器的bean定义如下所示:

@Autowired
private MyService myServiceImpl;

@Bean
MyService myService() {
    final ProxyFactoryBean proxy = new ProxyFactoryBean();

    final Class<?>[] proxyInterfaces = { MyService.class };
    proxy.setProxyInterfaces(proxyInterfaces);

    proxy.setTarget(this.myServiceImpl);

    final String[] interceptorNames = { "myInterceptor" };
    proxy.setInterceptorNames(interceptorNames);

    return (MyService) proxy.getObject();
}
@Autowired
私有MyService-MyService-impl;
@豆子
MyService MyService(){
最终ProxyFactoryBean代理=新的ProxyFactoryBean();
最终类[]proxyInterfaces={MyService.Class};
代理.setProxyInterfaces(proxyInterfaces);
setTarget(this.myServiceImpl);
最后一个字符串[]interceptorNames={“myInterceptor”};
代理.setInterceptorNames(interceptorNames);
return(MyService)proxy.getObject();
}
,其中“myInterceptor”名称在编译时未验证


使用JavaConfig配置拦截器有更好的方法吗?

配置拦截器的更好方法:

@Configuration
public class MyServiceConfig {
    @Autowired
    private BeanFactory beanFactory;

    @Autowired
    private IMyService myService;

    @Autowired
    private MyInterceptor myInterceptor;

    @Bean
    public IMyService myServiceIntercepted() {
        final Class<?>[] proxyInterfaces = { IMyService.class };
        final Advice[] advices = { this.myInterceptor };
        return createProxy(proxyInterfaces, this.myService, this.beanFactory,
            advices);
    }

    <T> T createProxy(final Class<?>[] proxyInterfaces, final T target,
            final BeanFactory beanFactory) {
        final ProxyFactoryBean proxy =
                createProxyReturnFactoryBean(proxyInterfaces, target, beanFactory);

        return (T) proxy.getObject();
    }

    <T> ProxyFactoryBean createProxyReturnFactoryBean(
            final Class<?>[] proxyInterfaces, final T target, final BeanFactory beanFactory) {
        final ProxyFactoryBean proxy = new ProxyFactoryBean();
        proxy.setBeanFactory(beanFactory);

        if (proxyInterfaces != null) {
            proxy.setProxyInterfaces(proxyInterfaces);
        }

        proxy.setTarget(target);

        return proxy;
    }

    <T> T createProxy(final Class<?>[] proxyInterfaces, final T target,
            final BeanFactory beanFactory, final Advice[] advices) {
        final ProxyFactoryBean proxy =
                createProxyReturnFactoryBean(proxyInterfaces, target, beanFactory);

        for (final Advice advice : advices) {
            proxy.addAdvice(advice);
        }

        return (T) proxy.getObject();
    }
}
@配置
公共类MyServiceConfig{
@自动连线
私人豆厂豆厂;
@自动连线
私有IMyService myService;
@自动连线
私人MyInterceptor MyInterceptor;
@豆子
公共IMyService myservicecintercepted(){
最终类[]proxyInterfaces={IMyService.Class};
final Advice[]advices={this.myInterceptor};
返回createProxy(proxyInterfaces、this.myService、this.beanFactory、,
建议);
}
T createProxy(最终类[]代理接口,最终T目标,
最终豆工厂(豆工厂){
最终ProxyFactoryBean代理=
createProxyReturnFactoryBean(proxyInterfaces、target、beanFactory);
返回(T)proxy.getObject();
}
ProxyFactoryBean createProxyReturnFactoryBean(
最终类[]代理接口,最终T目标,最终BeanFactory(BeanFactory){
最终ProxyFactoryBean代理=新的ProxyFactoryBean();
委托人:setBeanFactory(beanFactory);
if(代理接口!=null){
代理.setProxyInterfaces(proxyInterfaces);
}
proxy.setTarget(target);
返回代理;
}
T createProxy(最终类[]代理接口,最终T目标,
最终Bean工厂Bean工厂,最终建议[]建议){
最终ProxyFactoryBean代理=
createProxyReturnFactoryBean(proxyInterfaces、target、beanFactory);
对于(最终建议:建议){
委托书。添加建议(建议);
}
返回(T)proxy.getObject();
}
}