下面是我尝试测试 spring AOP 的示例。
SpringConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
">
<bean id="beanPostProcessor" class="springTest.SpringBeanPostProcessor" />
<bean id="contestPostProcessor" class="springTest.SpringPostProcessor" />
<bean id="emp" class="springTest.Employee">
<property name="id" value="1"/>
<property name="name" value="sunil"/>
</bean>
<bean id="loginCheckAspect" class="springTest.Aspect" init-method="initAspect"/>
<bean id="msg" class="springTest.LoadMessages" >
<property name="id" value="1" />
</bean>
<aop:config>
<aop:pointcut id="login" expression="execution(* *.showMsg*(..))"/>
<aop:aspect id="loginAspect" ref="loginCheckAspect" >
<aop:before method="checkLogin" pointcut-ref="login" />
</aop:aspect>
</aop:config>
</beans>
LoadMessages.class
package springTest;
public class LoadMessages {
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public LoadMessages() {
}
public void showMsg(){
System.out.println("#################################################");
System.out.println("Welcome neel");
System.out.println();
}
public void getInfo(){
System.out.println("#################################################");
System.out.println("Welcome neel");
System.out.println();
}
}
方面.java
package springTest;
import java.io.DataInputStream;
import java.io.IOException;
public class Aspect {
public void checkLogin() throws IOException{
DataInputStream ds=new DataInputStream(System.in);
//BufferedInputStream bf=new BufferedInputStream(System.in);
System.out.println("\n\n** Credential Check Point using AOP login aspect ** ");
System.out.print("Please login to view employee details");
System.out.print("\nUser Name : ");
String userName=ds.readLine();
String pass;
if(userName!=null && userName.equals("neel")){
System.out.print("Password : ");
pass=ds.readLine();
if(pass!=null && pass.equals("77")){
System.out.println();
}else{
throw new IOException("Invalid Password");
}
}else{
throw new IOException("Invalid User Name");
}
}
public void initAspect(){
//System.out.println("@@@@@@ Init method of aspect @@@@@");
}
}
SpringBeanPostProcessor.java
package springTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class SpringBeanPostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("Initialization Completes "+arg1);
return arg0;
}
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("Initialization....");
return arg0;
}
}
SpringPostProcessor.java
package springTest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class SpringPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("Initialization Context Succssefull.");
}
}
希望这会帮助你。