Java Code Examples for org.springframework.aop.framework.ProxyFactory#setTarget()

The following examples show how to use org.springframework.aop.framework.ProxyFactory#setTarget() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AnnotationTransactionInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void crossClassInterfaceMethodLevelOnJdkProxy() throws Exception {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new SomeServiceImpl());
	proxyFactory.addInterface(SomeService.class);
	proxyFactory.addAdvice(this.ti);

	SomeService someService = (SomeService) proxyFactory.getProxy();

	someService.bar();
	assertGetTransactionAndCommitCount(1);

	someService.foo();
	assertGetTransactionAndCommitCount(2);

	someService.fooBar();
	assertGetTransactionAndCommitCount(3);
}
 
Example 2
Source File: AnnotationTransactionInterceptorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withSingleMethodOverride() {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new TestWithSingleMethodOverride());
	proxyFactory.addAdvice(this.ti);

	TestWithSingleMethodOverride proxy = (TestWithSingleMethodOverride) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomethingCompletelyElse();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(4);
}
 
Example 3
Source File: ScopedBeanInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void interceptorWithCglibProxy() throws Exception {
	final Object realObject = new Object();
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(realObject);
	proxyFactory.setProxyTargetClass(true);
	final Object proxy = proxyFactory.getProxy();

	ScopedObject scoped = new ScopedObject() {
		@Override
		public Object getTargetObject() {
			return proxy;
		}
		@Override
		public void removeFromScope() {
			// do nothing
		}
	};

	assertEquals(realObject.getClass().getName(), interceptor.getEntityName(scoped));
}
 
Example 4
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void withCommitOnCheckedException() {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new TestWithExceptions());
	proxyFactory.addAdvice(this.ti);

	TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();

	try {
		proxy.doSomethingElseWithCheckedException();
		fail("Should throw Exception");
	}
	catch (Exception ex) {
		assertGetTransactionAndCommitCount(1);
	}
}
 
Example 5
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void crossClassInterfaceMethodLevelOnJdkProxy() {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new SomeServiceImpl());
	proxyFactory.addInterface(SomeService.class);
	proxyFactory.addAdvice(this.ti);

	SomeService someService = (SomeService) proxyFactory.getProxy();

	someService.bar();
	assertGetTransactionAndCommitCount(1);

	someService.foo();
	assertGetTransactionAndCommitCount(2);

	someService.fooBar();
	assertGetTransactionAndCommitCount(3);
}
 
Example 6
Source File: AnnotationTransactionInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void withSingleMethodOverrideInverted() {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new TestWithSingleMethodOverrideInverted());
	proxyFactory.addAdvice(this.ti);

	TestWithSingleMethodOverrideInverted proxy = (TestWithSingleMethodOverrideInverted) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomethingCompletelyElse();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(4);
}
 
Example 7
Source File: ApplicationListenerMethodAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void invokeListenerInvalidProxy() {
	Object target = new InvalidProxyTestBean();
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(target);
	proxyFactory.addInterface(SimpleService.class);
	Object bean = proxyFactory.getProxy(getClass().getClassLoader());

	Method method = ReflectionUtils.findMethod(
			InvalidProxyTestBean.class, "handleIt2", ApplicationEvent.class);
	StaticApplicationListenerMethodAdapter listener =
			new StaticApplicationListenerMethodAdapter(method, bean);
	assertThatIllegalStateException().isThrownBy(() ->
			listener.onApplicationEvent(createGenericTestEvent("test")))
		.withMessageContaining("handleIt2");
}
 
Example 8
Source File: AnnotationTransactionInterceptorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void classLevelOnly() {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new TestClassLevelOnly());
	proxyFactory.addAdvice(this.ti);

	TestClassLevelOnly proxy = (TestClassLevelOnly) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(4);
}
 
Example 9
Source File: RemoteExporter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get a proxy for the given service object, implementing the specified
 * service interface.
 * <p>Used to export a proxy that does not expose any internals but just
 * a specific interface intended for remote access. Furthermore, a
 * {@link RemoteInvocationTraceInterceptor} will be registered (by default).
 * @return the proxy
 * @see #setServiceInterface
 * @see #setRegisterTraceInterceptor
 * @see RemoteInvocationTraceInterceptor
 */
protected Object getProxyForService() {
	checkService();
	checkServiceInterface();

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.addInterface(getServiceInterface());

	if (this.registerTraceInterceptor != null ? this.registerTraceInterceptor : this.interceptors == null) {
		proxyFactory.addAdvice(new RemoteInvocationTraceInterceptor(getExporterName()));
	}
	if (this.interceptors != null) {
		AdvisorAdapterRegistry adapterRegistry = GlobalAdvisorAdapterRegistry.getInstance();
		for (Object interceptor : this.interceptors) {
			proxyFactory.addAdvisor(adapterRegistry.wrap(interceptor));
		}
	}

	proxyFactory.setTarget(getService());
	proxyFactory.setOpaque(true);

	return proxyFactory.getProxy(getBeanClassLoader());
}
 
Example 10
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void classLevelOnly() {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(new TestClassLevelOnly());
	proxyFactory.addAdvice(this.ti);

	TestClassLevelOnly proxy = (TestClassLevelOnly) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(4);
}
 
Example 11
Source File: AopTestUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private Foo jdkProxy(Foo foo) {
	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(foo);
	pf.addInterface(Foo.class);
	Foo proxy = (Foo) pf.getProxy();
	assertTrue("Proxy is a JDK dynamic proxy", AopUtils.isJdkDynamicProxy(proxy));
	assertThat(proxy, instanceOf(Foo.class));
	return proxy;
}
 
Example 12
Source File: AbstractMetadataAssemblerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCglibProxy() throws Exception {
	IJmxTestBean tb = createJmxTestBean();
	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(tb);
	pf.addAdvice(new NopInterceptor());
	Object proxy = pf.getProxy();

	MetadataMBeanInfoAssembler assembler = (MetadataMBeanInfoAssembler) getAssembler();

	MBeanExporter exporter = new MBeanExporter();
	exporter.setBeanFactory(getContext());
	exporter.setAssembler(assembler);

	String objectName = "spring:bean=test,proxy=true";

	Map<String, Object> beans = new HashMap<String, Object>();
	beans.put(objectName, proxy);
	exporter.setBeans(beans);
	start(exporter);

	MBeanInfo inf = getServer().getMBeanInfo(ObjectNameManager.getInstance(objectName));
	assertEquals("Incorrect number of operations", getExpectedOperationCount(), inf.getOperations().length);
	assertEquals("Incorrect number of attributes", getExpectedAttributeCount(), inf.getAttributes().length);

	assertTrue("Not included in autodetection", assembler.includeBean(proxy.getClass(), "some bean name"));
}
 
Example 13
Source File: AspectJExpressionPointcutTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
	TestBean target = new TestBean();

	Pointcut pointcut = getPointcut(pointcutExpression);

	DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
	advisor.setAdvice(interceptor);
	advisor.setPointcut(pointcut);

	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(target);
	pf.addAdvisor(advisor);

	return (TestBean) pf.getProxy();
}
 
Example 14
Source File: CodelessDaoFactoryBean.java    From sca-best-practice with Apache License 2.0 5 votes vote down vote up
@Override
public CodelessDao getObject() {
    ProxyFactory result = new ProxyFactory();
    result.setTarget(codelessDaoProxy);
    result.setInterfaces(CodelessDao.class, SpringProxy.class);
    result.addAdvice(SurroundingTransactionDetectorMethodInterceptor.INSTANCE);
    result.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    result.addAdvice(new DefaultMethodInvokingMethodInterceptor());
    return (CodelessDao)result.getProxy(classLoader);
}
 
Example 15
Source File: NativeQueryProxyFactoryImpl.java    From spring-native-query with MIT License 5 votes vote down vote up
@Override
public Object create(Class<? extends NativeQuery> classe) {
    ProxyFactory proxy = new ProxyFactory();
    proxy.setTarget(Mockito.mock(classe));
    proxy.setInterfaces(classe, NativeQuery.class);
    proxy.addAdvice((MethodInterceptor) invocation -> {
        if ("toString".equals(invocation.getMethod().getName())) {
            return "NativeQuery Implementation";
        }
        NativeQueryInfo info = NativeQueryCache.get(classe, invocation);
        return nativeQueryMethodInterceptor.executeQuery(info);
    });
    return proxy.getProxy(classe.getClassLoader());
}
 
Example 16
Source File: AnnotationTransactionInterceptorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void withInterfaceOnTargetCglibProxy() {
	ProxyFactory targetFactory = new ProxyFactory();
	targetFactory.setTarget(new TestWithInterfaceImpl());
	targetFactory.setProxyTargetClass(true);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(targetFactory.getProxy());
	proxyFactory.addInterface(TestWithInterface.class);
	proxyFactory.addAdvice(this.ti);

	TestWithInterface proxy = (TestWithInterface) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(4);

	proxy.doSomethingDefault();
	assertGetTransactionAndCommitCount(5);
}
 
Example 17
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void withInterfaceOnTargetCglibProxy() {
	ProxyFactory targetFactory = new ProxyFactory();
	targetFactory.setTarget(new TestWithInterfaceImpl());
	targetFactory.setProxyTargetClass(true);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(targetFactory.getProxy());
	proxyFactory.addInterface(TestWithInterface.class);
	proxyFactory.addAdvice(this.ti);

	TestWithInterface proxy = (TestWithInterface) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(4);

	proxy.doSomethingDefault();
	assertGetTransactionAndCommitCount(5);
}
 
Example 18
Source File: AnnotationTransactionInterceptorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void withInterfaceOnTargetJdkProxy() {
	ProxyFactory targetFactory = new ProxyFactory();
	targetFactory.setTarget(new TestWithInterfaceImpl());
	targetFactory.addInterface(TestWithInterface.class);

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTarget(targetFactory.getProxy());
	proxyFactory.addInterface(TestWithInterface.class);
	proxyFactory.addAdvice(this.ti);

	TestWithInterface proxy = (TestWithInterface) proxyFactory.getProxy();

	proxy.doSomething();
	assertGetTransactionAndCommitCount(1);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(2);

	proxy.doSomethingElse();
	assertGetTransactionAndCommitCount(3);

	proxy.doSomething();
	assertGetTransactionAndCommitCount(4);

	proxy.doSomethingDefault();
	assertGetTransactionAndCommitCount(5);
}
 
Example 19
Source File: ModelMapTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAopCglibProxy() throws Exception {
	ModelMap map = new ModelMap();
	ProxyFactory factory = new ProxyFactory();
	SomeInnerClass val = new SomeInnerClass();
	factory.setTarget(val);
	factory.setProxyTargetClass(true);
	map.addAttribute(factory.getProxy());
	assertTrue(map.containsKey("someInnerClass"));
	assertEquals(val, map.get("someInnerClass"));
}
 
Example 20
Source File: AspectJExpressionPointcutTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
	TestBean target = new TestBean();

	Pointcut pointcut = getPointcut(pointcutExpression);

	DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
	advisor.setAdvice(interceptor);
	advisor.setPointcut(pointcut);

	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(target);
	pf.addAdvisor(advisor);

	return (TestBean) pf.getProxy();
}