使用Spring(AOP?)实现Java接口 - java

我有几个带有getter和setter的简单接口,以及一些其他从文件系统读取和写入的方法。
使用直接的Java代码,我可以编写一个“调用处理程序”,并使用它为所有这些接口实例化对象(我没有尝试过,但是我认为可以做到)。

我想知道是否可以使用Spring做同样的事情。

下面的代码实现了给定的接口。如您所见,任何接口都可以使用相同的调用处理程序。

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class AOPTester {

    public static void main(String[] args) {
        InvocationHandler handler = new MyInvocationHandler();
        AnyInterface proxy = (AnyInterface) Proxy.newProxyInstance(
                                    AnyInterface.class.getClassLoader(),
                                    new Class[] { AnyInterface.class },
                                    handler);

        proxy.sayHello();

    }

}

interface AnyInterface {
    public void sayHello();
}

class MyInvocationHandler implements InvocationHandler{

    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        System.out.println("Hello!");

        return null;
    }
}   

参考方案

实际上,使用ProxyFactoryBean使用Spring可以做到这一点。
在下面的示例中,此类在没有目标Bean的情况下初始化。
创建的对象没有任何转发请求的目标,但它可以像Java中的其他任何代理一样实现任何接口。

当然,如果尝试在传递给MethodInterceptor的invoke方法的调用对象上调用proce方法,则会得到NullPointerException。

Better-application-context.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <bean id="goodbyeMethodInterceptor" class="com.someco.GoodbyeMethodInterceptor" />

    <bean name="goodbyeProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="interfaces">
            <list>
                <value>com.someco.AnyInterface</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>goodbyeMethodInterceptor</value>
            </list>
        </property>
    </bean>
</beans>

再见方法拦截器:

package com.someco;

import org.aopalliance.intercept.MethodInvocation;

public class GoodbyeMethodInterceptor implements org.aopalliance.intercept.MethodInterceptor {

    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("Goodbye");

        return null;
    }

}

ProxyTester:

package com.someco;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.someco.AnyInterface;

public class ProxyTester {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("better-application-context.xml"); 
        AnyInterface tester = (AnyInterface) context.getBean("goodbyeProxy");
        tester.sayHello();
    }
}

AnyInterface:

package com.someco;

public interface AnyInterface {
    public void sayHello();
}

基本的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.someco</groupId>
    <artifactId>proxy-tester</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>main</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.0.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>

Java中的<<或>>>是什么意思? - java

This question already has answers here: Closed 7 years ago. Possible Duplicate: What does >> and >>> mean in Java?我在一些Java代码中遇到了一些陌生的符号,尽管代码可以正确编译和运行,但对于括号在此代码中的作用却感…

菱形运算符<>是否等于<?> - java

我在util.TreeSet类中发现,其中一个构造函数正在使用具有空泛型类型的新TreeMap调用另一个构造函数。 public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } new TreeMap<>是什么意思…

休眠映射<键,设置<值>> - java

我有以下表格:@Entity @Table(name = "events") Event --id --name @Entity @Table(name = "state") State --id --name @Entity @Table(name = "action") Action --id …

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…

合并List <T>和List <Optional <T >> - java

鉴于: List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.em…