In this example you will see the use of DefaultAdvisorAutoproxyCreator. It automatically creates the prxy for every bean of an application. There is no need to create the proxy bean manually if you are using DefaultAdvisorAutoproxyCreator.
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"
|
| Inside Method Before Advice Executing showDetail() Method Name Vinay Id 7 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.