Spring Auto proxy creator example

In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean (ProxyFactoryBean) for each beans whose need AOP support.

This is not an efficient way, for example, if you want all of your DAO classes in customer module to implement the AOP feature with SQL logging support (advise), then you have to create many proxy factory beans manually, as a result you just flood your bean configuration file with tons of proxy beans.

Fortunately, Spring comes with two auto proxy creators to create proxies for your beans automatically.

1. BeanNameAutoProxyCreator example

Before that, you have to create a proxy bean (ProxyFactoryBean) manually.


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

	<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
		<property name="name" value="Yong Mook Kim" />
		<property name="url" value="http://www.mkyong.com" />
	</bean>

	<bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.HijackAroundMethod" />

	<bean id="customerServiceProxy" 
	    class="org.springframework.aop.framework.ProxyFactoryBean">

		<property name="target" ref="customerService" />

		<property name="interceptorNames">
			<list>
				<value>customerAdvisor</value>
			</list>
		</property>
	</bean>

	<bean id="customerAdvisor"
		class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedName" value="printName" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	</bean>
</beans>

And get the bean with the bean with proxy name “customerServiceProxy”.


	CustomerService cust = (CustomerService)appContext.getBean("customerServiceProxy");

In auto proxy mechanism, you just need to create a BeanNameAutoProxyCreator, and include all your beans (via bean name, or regular expression name) and ‘advisor’ into a single unit.


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

	<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
		<property name="name" value="Yong Mook Kim" />
		<property name="url" value="http://www.mkyong.com" />
	</bean>

	<bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.HijackAroundMethod" />

	<bean id="customerAdvisor"
		class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedName" value="printName" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	</bean>

	<bean
		class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
		<property name="interceptorNames">
			<list>
				<value>customerAdvisor</value>
			</list>
		</property>
	</bean>
</beans>

Now, you can get the bean via original name “customerService”, you dun even know this bean has been proxies.


	CustomerService cust = (CustomerService)appContext.getBean("customerService");

2. DefaultAdvisorAutoProxyCreator example

This DefaultAdvisorAutoProxyCreator is extremely powerful, if any of the beans is matched by an advisor, Spring will create a proxy for it automatically.


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

	<bean id="customerService" class="com.mkyong.customer.services.CustomerService">
		<property name="name" value="Yong Mook Kim" />
		<property name="url" value="http://www.mkyong.com" />
	</bean>

	<bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.HijackAroundMethod" />

	<bean id="customerAdvisor"
		class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
		<property name="mappedName" value="printName" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	</bean>

       <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

</beans>

This is just over power, since you don’t have control what bean should be proxy, what you can do is just trust Spring will do the best for you. Please take good care if you want to implement this into your project.

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
Binh Thanh NguyenSpring AOP ?? | qiyadengJoydeep Bhattacharyagoji activesfryzury Recent comment authors
newest oldest most voted
Binh Thanh Nguyen
Guest
Binh Thanh Nguyen

Thanks, nice post

trackback
Spring AOP ?? | qiyadeng

[…] ??proxy?????????proxy example? […]

Joydeep Bhattacharya
Guest
Joydeep Bhattacharya

The example works fine but I faced a small problem
that is if your class is implementing any interface
for example say
com.mkyong.customer.services.CustomerService implements Serializable
then it fails to instantiate the bean, it gives the following error
Exception in thread “main” java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to com.mkyong.customer.services.CustomerService
at com.oracle.spring.aop.test.AOPMain.main(AOPMain.java:18)
Any work around for this problem ?

goji actives
Guest
goji actives

It’s the best time to make some plans for the future and it’s time to be happy.
I have read this post and if I could I desire to suggest you
few interesting things or suggestions. Maybe you can write next articles referring to this article.
I wish to read even more things about it!

fryzury
Guest
fryzury

Valuable information. Fortunate me I discovered your site by accident, and
I am stunned why this twist of fate didn’t happened in advance! I bookmarked it.

IT Support Buckingham
Guest
IT Support Buckingham

Wow, this article is good, my sister is analyzing these
things, thus I am going to inform her.

Shiva Krishna
Guest
Shiva Krishna

hi Mkyong,

The Auto proxy Beans creation is explained in an overview kind of.. Could you please detail on this topic a lil bit more.

I think this page is a direct rush to the entire topic.

Regards,
Shiva Krishna.

Kristof Debruyne
Guest
Kristof Debruyne

Hi Mkyong, I am a big fan of you page and at work your results show up often a lot ! Good job ! I have a standalone application (www.facebook.com/elitetaggerpro) and I want to use Spring AOP for some concerns I have .. like logging, security etc… I have to say that I don’t use Spring as container but only want to use it for AOP… I tried several things and didn’t work… when I create a service and autowire it with spring then the AOP is kicking in … probably cause Spring auto proxy that bean which makes it… Read more »

trackback
Spring Tutorials

[…] AOP interceptor sequence The sequence of the AOP interceptor will affect the functionality.Auto proxy creator example An auto proxy creator example to create proxies object for your beans automatically, useful to […]