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>
source share