簡體   English   中英

有一個“Spring方式”從注釋界面獲得實現嗎?

[英]It there a “Spring way” to get an implementation from an annotated interface?

我想廢棄一些樣板。 假設我有一個帶自定義注釋的自定義界面:

interface MyInterface {
    @DoSomething("crazy")
    public String aMethod(int numberOfJumps); 
}

現在我可以編寫一個InvocationHandler ,並根據注釋和方法參數生成一個或多或少有用的Proxy實現,並返回一個合適的結果。 這很好用。

我的問題是,如果我可以使用Spring的某些機制來獲得相同的結果,但可能更安全,更快速,更靈活和/或更可配置。 Spring AOP注釋看起來很有希望,但它們似乎需要一個類,而不是一個接口。

[更新]

為了清楚我想要的東西,我現在的代碼大綱:

public interface TestInterface {
    @MyAnnotation(name = "foo")
    public void testMethod(String arg);
}

public class AnnotationProxy {
    @SuppressWarnings("unchecked")
    public static <T> T getImplementation(Class<T> annotatedInterface) {
       return (T) Proxy.newProxyInstance(annotatedInterface.getClassLoader(),
               new Class<?>[]{annotatedInterface},
               new AnnotationHandler());
    }

    private static class AnnotationHandler implements InvocationHandler {
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("Args " + Arrays.toString(args));
            System.out.println("Annotations" + Arrays.toString(method.getAnnotations()));
            return "useful value";
        }
    }
}

TestInterface ti = getImplementation(TestInterface.class);
String s = ti.testMethod("xyz"); //"useful value"

如你所見,我憑空創造了一個物體(以及一些難看的反射物)。 我想知道我是否能夠以更文明和更像春天的方式做到這一點。

一種方法是掃描具有您喜歡的注釋的方法的接口,在Spring應用程序上下文中創建和注冊代理。

讓我們有一個接口Test我們要創建並注冊代理:

package com.test;

public interface Test {
    @DoSomething(pattern = "[%s]")
    void print(String value);
}

我們的注釋看起來像這樣:

package com.test;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface DoSomething {

    String pattern();

}

我們想在這樣的應用程序中使用它:

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.test.Test;

public class Main {
    public static void main(final String[] args) throws Exception {
        final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
        final Test test = ctx.getBean(Test.class);
        test.print("Hello");
    }
}

我們的test.xml Spring配置文件只包含對組件的掃描:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="com.test"/>
</beans>

不着邊際。 為了創建和注冊代理,我們必須實現BeanFactoryPostProcessor來掃描接口並創建代理:

package com.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
import org.springframework.stereotype.Component;

@Component
public class DoSomethingPostprocessor implements BeanFactoryPostProcessor, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Object createDoSomethingBean(final MethodMetadata mmd, final Map<String, Object> attributes)
            throws Exception {
        final String pattern = (String) attributes.get("pattern");
        final Class<?> clazz = Class.forName(mmd.getDeclaringClassName());
        return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz },
                new InvocationHandler() {

                    @Override
                    public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
                        System.out.println(String.format(pattern, args[0]));

                        return null;
                    }
                });
    }

    @Override
    public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) throws BeansException {
        try {
            final String packageSearchPath = "classpath*:com/**/*.class";

            final Resource[] resources =
                    applicationContext.getResources(packageSearchPath);
            final SimpleMetadataReaderFactory factory = new
                    SimpleMetadataReaderFactory(applicationContext);

            for (final Resource resource : resources) {
                final MetadataReader mdReader = factory.getMetadataReader(resource);

                final AnnotationMetadata am = mdReader.getAnnotationMetadata();
                final Set<MethodMetadata> methodMetadata =
                        am.getAnnotatedMethods(DoSomething.class.getName());
                for (final MethodMetadata mmd : methodMetadata) {
                    final Map<String, Object> attributes =
                            mmd.getAnnotationAttributes(DoSomething.class.getName());
                    final String beanName = mmd.getDeclaringClassName();
                    beanFactory.registerSingleton(beanName, createDoSomethingBean(mmd, attributes));
                }
            }
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

您可以使用Spring AOP攔截和發布帶注釋的方法:

@Aspect
public class ProcessDoSomethingAspect {

    @AfterReturning(value = "execution(@annotation(doSomething) * *.*(..))", returning = "result")
    public String processResult(my.package.DoSomething doSomething, String result) {
        // process result here
        return result;
    }   

}

暫無
暫無

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

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