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

The following examples show how to use org.springframework.aop.framework.ProxyFactory#setProxyTargetClass() . 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: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
	ProxyFactory pf = new ProxyFactory(target);
	if (interfaces.length > 1 || interfaces[0].isInterface()) {
		pf.setInterfaces(interfaces);
	}
	else {
		pf.setProxyTargetClass(true);
	}

	// Required everywhere we use AspectJ proxies
	pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);

	for (Object a : advisors) {
		pf.addAdvisor((Advisor) a);
	}

	pf.setExposeProxy(true);
	return pf.getProxy();
}
 
Example 2
Source File: ScriptFactoryPostProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a refreshable proxy for the given AOP TargetSource.
 * @param ts the refreshable TargetSource
 * @param interfaces the proxy interfaces (may be {@code null} to
 * indicate proxying of all interfaces implemented by the target class)
 * @return the generated proxy
 * @see RefreshableScriptTargetSource
 */
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(ts);
	ClassLoader classLoader = this.beanClassLoader;

	if (interfaces == null) {
		interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader);
	}
	proxyFactory.setInterfaces(interfaces);
	if (proxyTargetClass) {
		classLoader = null;  // force use of Class.getClassLoader()
		proxyFactory.setProxyTargetClass(true);
	}

	DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
	introduction.suppressInterface(TargetSource.class);
	proxyFactory.addAdvice(introduction);

	return proxyFactory.getProxy(classLoader);
}
 
Example 3
Source File: JmsListenerContainerFactoryIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void parameterAnnotationWithCglibProxy() throws JMSException {
	ProxyFactory pf = new ProxyFactory(sample);
	pf.setProxyTargetClass(true);
	listener = (JmsEndpointSampleBean) pf.getProxy();

	containerFactory.setMessageConverter(new UpperCaseMessageConverter());

	MethodJmsListenerEndpoint endpoint = createDefaultMethodJmsEndpoint(
			JmsEndpointSampleBean.class, "handleIt", String.class, String.class);
	Message message = new StubTextMessage("foo-bar");
	message.setStringProperty("my-header", "my-value");

	invokeListener(endpoint, message);
	assertListenerMethodInvocation("handleIt");
}
 
Example 4
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 5
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
	ProxyFactory pf = new ProxyFactory(target);
	if (interfaces.length > 1 || interfaces[0].isInterface()) {
		pf.setInterfaces(interfaces);
	}
	else {
		pf.setProxyTargetClass(true);
	}

	// Required everywhere we use AspectJ proxies
	pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pf.addAdvisors(advisors);

	pf.setExposeProxy(true);
	return pf.getProxy();
}
 
Example 6
Source File: EncryptablePropertySourceConverter.java    From jasypt-spring-boot with MIT License 6 votes vote down vote up
@SuppressWarnings("unchecked")
private <T> PropertySource<T> proxyPropertySource(PropertySource<T> propertySource) {
    //Silly Chris Beams for making CommandLinePropertySource getProperty and containsProperty methods final. Those methods
    //can't be proxied with CGLib because of it. So fallback to wrapper for Command Line Arguments only.
    if (CommandLinePropertySource.class.isAssignableFrom(propertySource.getClass())
            // Other PropertySource classes like org.springframework.boot.env.OriginTrackedMapPropertySource
            // are final classes as well
            || Modifier.isFinal(propertySource.getClass().getModifiers())) {
        return instantiatePropertySource(propertySource);
    }
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTargetClass(propertySource.getClass());
    proxyFactory.setProxyTargetClass(true);
    proxyFactory.addInterface(EncryptablePropertySource.class);
    proxyFactory.setTarget(propertySource);
    proxyFactory.addAdvice(new EncryptablePropertySourceMethodInterceptor<>(propertySource, propertyResolver, propertyFilter));
    return (PropertySource<T>) proxyFactory.getProxy();
}
 
Example 7
Source File: AnnotationTransactionAttributeSourceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Test the important case where the invocation is on a proxied interface method
 * but the attribute is defined on the target class.
 */
@Test
public void transactionAttributeDeclaredOnCglibClassMethod() throws Exception {
	Method classMethod = ITestBean.class.getMethod("getAge");
	TestBean1 tb = new TestBean1();
	ProxyFactory pf = new ProxyFactory(tb);
	pf.setProxyTargetClass(true);
	Object proxy = pf.getProxy();

	AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
	TransactionAttribute actual = atas.getTransactionAttribute(classMethod, proxy.getClass());

	RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
	rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception.class));
	assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
}
 
Example 8
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) {
	ProxyFactory pf = new ProxyFactory(target);
	if (interfaces.length > 1 || interfaces[0].isInterface()) {
		pf.setInterfaces(interfaces);
	}
	else {
		pf.setProxyTargetClass(true);
	}

	// Required everywhere we use AspectJ proxies
	pf.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pf.addAdvisors(advisors);

	pf.setExposeProxy(true);
	return pf.getProxy();
}
 
Example 9
Source File: GroovyAspectTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void testAdvice(Advisor advisor, LogUserAdvice logAdvice, TestService target, String message,
		boolean proxyTargetClass) throws Exception {

	logAdvice.reset();

	ProxyFactory factory = new ProxyFactory(target);
	factory.setProxyTargetClass(proxyTargetClass);
	factory.addAdvisor(advisor);
	TestService bean = (TestService) factory.getProxy();

	assertEquals(0, logAdvice.getCountThrows());
	try {
		bean.sayHello();
		fail("Expected exception");
	}
	catch (TestException ex) {
		assertEquals(message, ex.getMessage());
	}
	assertEquals(1, logAdvice.getCountThrows());
}
 
Example 10
Source File: TigerAspectJExpressionPointcutTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAnnotationOnDynamicProxyMethod() throws Exception {
	String expression = "@annotation(test.annotation.transaction.Tx)";
	AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
	ajexp.setExpression(expression);

	ProxyFactory factory = new ProxyFactory(new BeanA());
	factory.setProxyTargetClass(false);
	IBeanA proxy = (IBeanA) factory.getProxy();
	assertTrue(ajexp.matches(IBeanA.class.getMethod("getAge"), proxy.getClass()));
}
 
Example 11
Source File: AbstractAutoProxyCreator.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Create an AOP proxy for the given bean.
 * @param beanClass the class of the bean
 * @param beanName the name of the bean
 * @param specificInterceptors the set of interceptors that is
 * specific to this bean (may be empty, but not null)
 * @param targetSource the TargetSource for the proxy,
 * already pre-configured to access the bean
 * @return the AOP proxy for the bean
 * @see #buildAdvisors
 */
protected Object createProxy(
		Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {

	if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
		AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
	}

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.copyFrom(this);

	if (!proxyFactory.isProxyTargetClass()) {
		if (shouldProxyTargetClass(beanClass, beanName)) {
			proxyFactory.setProxyTargetClass(true);
		}
		else {
			evaluateProxyInterfaces(beanClass, proxyFactory);
		}
	}

	Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
	for (Advisor advisor : advisors) {
		proxyFactory.addAdvisor(advisor);
	}

	proxyFactory.setTargetSource(targetSource);
	customizeProxyFactory(proxyFactory);

	proxyFactory.setFrozen(this.freezeProxy);
	if (advisorsPreFiltered()) {
		proxyFactory.setPreFiltered(true);
	}

	return proxyFactory.getProxy(getProxyClassLoader());
}
 
Example 12
Source File: AopTestUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private Foo cglibProxy(Foo foo) {
	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(foo);
	pf.setProxyTargetClass(true);
	Foo proxy = (Foo) pf.getProxy();
	assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy));
	assertThat(proxy, instanceOf(FooImpl.class));
	return proxy;
}
 
Example 13
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 14
Source File: ModelMapTests.java    From spring-analysis-note with MIT License 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 15
Source File: ScriptFactoryPostProcessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a refreshable proxy for the given AOP TargetSource.
 * @param ts the refreshable TargetSource
 * @param interfaces the proxy interfaces (may be {@code null} to
 * indicate proxying of all interfaces implemented by the target class)
 * @return the generated proxy
 * @see RefreshableScriptTargetSource
 */
protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces, boolean proxyTargetClass) {
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setTargetSource(ts);
	ClassLoader classLoader = this.beanClassLoader;

	if (interfaces != null) {
		proxyFactory.setInterfaces(interfaces);
	}
	else {
		Class<?> targetClass = ts.getTargetClass();
		if (targetClass != null) {
			proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader));
		}
	}

	if (proxyTargetClass) {
		classLoader = null;  // force use of Class.getClassLoader()
		proxyFactory.setProxyTargetClass(true);
	}

	DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
	introduction.suppressInterface(TargetSource.class);
	proxyFactory.addAdvice(introduction);

	return proxyFactory.getProxy(classLoader);
}
 
Example 16
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
static SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean nestedProxy() {
	ProxyFactory pf1 = new ProxyFactory(new SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean());
	pf1.setProxyTargetClass(true);
	ProxyFactory pf2 = new ProxyFactory(pf1.getProxy());
	pf2.setProxyTargetClass(true);
	return (SeveralFixedRatesWithRepeatedScheduledAnnotationTestBean) pf2.getProxy();
}
 
Example 17
Source File: AbstractAutoProxyCreator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create an AOP proxy for the given bean.
 * @param beanClass the class of the bean
 * @param beanName the name of the bean
 * @param specificInterceptors the set of interceptors that is
 * specific to this bean (may be empty, but not null)
 * @param targetSource the TargetSource for the proxy,
 * already pre-configured to access the bean
 * @return the AOP proxy for the bean
 * @see #buildAdvisors
 */
protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
		@Nullable Object[] specificInterceptors, TargetSource targetSource) {

	if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
		AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
	}

	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.copyFrom(this);

	if (!proxyFactory.isProxyTargetClass()) {
		if (shouldProxyTargetClass(beanClass, beanName)) {
			proxyFactory.setProxyTargetClass(true);
		}
		else {
			evaluateProxyInterfaces(beanClass, proxyFactory);
		}
	}

	Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
	proxyFactory.addAdvisors(advisors);
	proxyFactory.setTargetSource(targetSource);
	customizeProxyFactory(proxyFactory);

	proxyFactory.setFrozen(this.freezeProxy);
	if (advisorsPreFiltered()) {
		proxyFactory.setPreFiltered(true);
	}

	return proxyFactory.getProxy(getProxyClassLoader());
}
 
Example 18
Source File: TigerAspectJExpressionPointcutTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAnnotationOnDynamicProxyMethod() throws Exception {
	String expression = "@annotation(test.annotation.transaction.Tx)";
	AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut();
	ajexp.setExpression(expression);

	ProxyFactory factory = new ProxyFactory(new BeanA());
	factory.setProxyTargetClass(false);
	IBeanA proxy = (IBeanA) factory.getProxy();
	assertTrue(ajexp.matches(IBeanA.class.getMethod("getAge"), proxy.getClass()));
}
 
Example 19
Source File: AopTestUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Foo cglibProxy(Foo foo) {
	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(foo);
	pf.setProxyTargetClass(true);
	Foo proxy = (Foo) pf.getProxy();
	assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy));
	assertThat(proxy, instanceOf(FooImpl.class));
	return proxy;
}
 
Example 20
Source File: AbstractAutoProxyCreator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create an AOP proxy for the given bean.
 *
 * 注释 8.9 为给定的bean创建AOP代理,实际在这一步中进行切面织入,生成动态代理
 *
 * @param beanClass the class of the bean
 * @param beanName the name of the bean
 * @param specificInterceptors the set of interceptors that is
 * specific to this bean (may be empty, but not null)
 * @param targetSource the TargetSource for the proxy,
 * already pre-configured to access the bean
 * @return the AOP proxy for the bean
 * @see #buildAdvisors
 */
protected Object createProxy(Class<?> beanClass, @Nullable String beanName,
		@Nullable Object[] specificInterceptors, TargetSource targetSource) {

	if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
		AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
	}

	ProxyFactory proxyFactory = new ProxyFactory();
	// 拷贝,获取当前类中的相关属性
	proxyFactory.copyFrom(this);

	// 决定对于给定 bean 是否应该使用 targetClass 而不是他的接口代理
	if (!proxyFactory.isProxyTargetClass()) {
		// 检查 proxyTargetClass 设置以及 preserveTargetClass 属性
		if (shouldProxyTargetClass(beanClass, beanName)) {
			proxyFactory.setProxyTargetClass(true);
		}
		else {
			// 添加代理接口
			evaluateProxyInterfaces(beanClass, proxyFactory);
		}
	}
	// 这一步中,主要将拦截器封装为增强器
	Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
	proxyFactory.addAdvisors(advisors);
	proxyFactory.setTargetSource(targetSource);
	// 定制代理
	customizeProxyFactory(proxyFactory);
	// 用来控制代理工厂被配置之后,是否含允许修改通知
	// 缺省值为 false,不允许修改代理的配置
	proxyFactory.setFrozen(this.freezeProxy);
	if (advisorsPreFiltered()) {
		proxyFactory.setPreFiltered(true);
	}
	// 生成代理,委托给了 ProxyFactory 去处理。
	return proxyFactory.getProxy(getProxyClassLoader());
}