//创建起步接口
package busispring;
public interface UserManager {
public void addUser(String user,String pass);
public void delUser(String user);
}
//定义接口实现类
package busispring;
public class UserManagerImpl implements UserManager{
@Override
public void addUser(String username,String password) {
// TODO Auto-generated method stub
System.out.println("begin add user...");
}
@Override
public void delUser(String username) {
// TODO Auto-generated method stub
System.out.println("del user");
}
}
//定义另一个操作类
package busispring;
import org.aspectj.lang.JoinPoint;
public class LogMarkWriter {
public void before(JoinPoint jp){
System.out.println("before:"+jp.getSignature());
}
public void after(JoinPoint jp){
System.out.println("after:"+jp.getSignature());
}
}
//配置文件bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="userManager" class="busispring.UserManagerImpl" />
<bean id="logMark" class="busispring.LogMarkWriter" />
<aop:config>
<aop:aspect id="myAop" ref="logMark">
<aop:pointcut id="target" expression="execution(* busispring.UserManagerImpl.add*(..))" />
<aop:before method="before" pointcut-ref="target" />
<aop:after method="after" pointcut-ref="target" />
</aop:aspect>
</aop:config>
</beans>
//调用实现
package busispring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
public static void main(String args[]){
ApplicationContext context = new ClassPathXmlApplicationContext("busispring/bean.xml");
UserManager userM=context.getBean("userManager",UserManager.class);
userM.addUser("user001", "secret");
userM.delUser("jione");
}
}
//运行结果
before:void busispring.UserManager.addUser(String,String)
begin add user...
after:void busispring.UserManager.addUser(String,String)
del user