Share on Google+Share on Google+

Spring AOP BeanNameAutoProxyCreator

Advertisement
In this tutorial you will learn how to create bean proxy for every bean manually using BeanNameAutoProxyCreator

Spring AOP BeanNameAutoProxyCreator Example

In this example I have used BeanNameAutoProxyCreator. It is used to create the proxy bean manually for every bean who needs AOP supports. If you have more than one bean in your application then you have to create proxy bean for every bean.

EmployeBean.java

package roseindia.net.bean;

public class EmployeBean {
	String empName;
	int empId;

	public EmployeBean() {

	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public int getEmpId() {
		return empId;
	}

	public void setEmpId(int empId) {
		this.empId = empId;
	}

	public EmployeBean(String empName, int empId) {
		this.empName = empName;
		this.empId = empId;
	}

	public void showDetail() {
		System.out.println("Name " + this.empName + " Id " + this.empId);
	}
}

MethodBeforeAdviceExample.java

package roseindia.net.advice;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.MethodBeforeAdvice;

public class MethodBeforeAdviceExample implements MethodBeforeAdvice {

	@Override
	public void before(Method method, Object[] objects, Object object)
			throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("Inside Method Before Advice");
		System.out.println("Executing " + method.getName() + "() Method");
	}

}

MainClaz.java

package roseindia.net.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import roseindia.net.bean.EmployeBean;

public class MainClaz {
	public static void main(String args[]) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				new String[] { "roseindia/net/config/springConfig.xml" });
		EmployeBean employeBean = (EmployeBean) applicationContext
				.getBean("employeBean");
		employeBean.showDetail();

	}
}

springConfig.xml

<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="employeBean" class="roseindia.net.bean.EmployeBean">
    <property name="empName" value="Vinay" />
    <property name="empId" value="007" />
  </bean>

  <bean id="beforeAdvice" class="roseindia.net.advice.MethodBeforeAdviceExample" />

  <bean id="employeAdvisor"
    class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
    <property name="mappedName" value="showDetail" />
    <property name="advice" ref="beforeAdvice" />
  </bean>
  <bean
    class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <property name="beanNames">
      <list>
        <value>Service</value>
      </list>
    </property>
    <property name="interceptorNames">
      <list>
        <value>employeAdvisor</value>
      </list>
    </property>
  </bean>

</beans>

When you run this application it will display message as shown below:


Name Vinay Id 7

Download this example code

Advertisements

Advertisement

Posted on: October 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.