Uses Java interface using Spring (AOP?)

I have some simple interfaces with getters and setters and several other ways to read and write from the file system. Using the Java code directly, I could write one β€œcall handler” and use it to create objects for all of these interfaces (I have not tried, but I think it can be done).

I am wondering if you can do the same with Spring.

The code below implements this interface. As you can see, the same call handler can be used for any interface.

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; } } 
+6
source share
2 answers

There is actually a clean way to do this using Spring using a ProxyFactoryBean. In the example below, this class is initialized without a target bean. The created object has no purpose for sending requests, but can implement any interface, like any other proxy server in Java.

Of course, if you try to call the continue method on the call object passed to the invoke MethodInterceptor, you will get a 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> 

GoodbyeMethodInterceptor:

 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(); } 

Base 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> 
0
source

The following configuration should work for you (I used your classes, but I moved them to another package to make the code more readable). I used the spring context to make the same factory method call newProxyInstance () that you used.

 <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="pojoInvocationHandler" class="com.someco.PojoInvocationHandler"></bean> <bean id="AnyInterfaceClass" class="java.lang.Class" factory-method="forName"> <constructor-arg value="com.someco.AnyInterface"/> </bean> <bean id="anyInterface" class="java.lang.reflect.Proxy" factory-method="newProxyInstance"> <constructor-arg> <bean factory-bean="AnyInterfaceClass" factory-method="getClassLoader" /> </constructor-arg> <constructor-arg> <list> <ref bean="AnyInterfaceClass" /> </list> </constructor-arg> <constructor-arg ref="pojoInvocationHandler"/> </bean> </beans> 

ProxyTester:

 package com.someco; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; 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 contex = new ClassPathXmlApplicationContext("application-context.xml"); AnyInterface tester = (AnyInterface) contex.getBean("anyInterface"); tester.sayHello(); /* Implemented with the previous code */ // callProxy(); } /** * @deprecated * explanation of why function was deprecated, if possible include what * should be used. */ @Deprecated public static void callProxy() { InvocationHandler handler = new PojoInvocationHandler(); AnyInterface proxy = (AnyInterface) Proxy.newProxyInstance( AnyInterface.class.getClassLoader(), new Class[] { AnyInterface.class }, handler); proxy.sayHello(); } } 

PojoInvocationHandler:

 package com.someco; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class PojoInvocationHandler implements InvocationHandler{ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("Hello!"); return null; } } 

AnyInterface:

 package com.someco; public interface AnyInterface { public void sayHello(); } 

Base 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> 
+2
source

All Articles