spring AOP学习笔记

public class User {
private int id;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}








public interface UserManager {

public void aadUser(User user);

public void deleteById(int id);

public void updateUser(User user);

public User findById(int id);
}


public class UserManagerImp implements UserManager {

@Override
public void aadUser(User user) {
System.out.println("----------addUser------------");
}

@Override
public void deleteById(int id) {
System.out.println("----------deleteById------------");
}

@Override
public User findById(int id) {
System.out.println("----------findById------------");
return null;
}

@Override
public void updateUser(User user) {
System.out.println("----------updateUser------------");
}

}


public class SecurityHandler {
public void checkSecurity(){
System.out.println("-----------checkSecurity()----------");
}
}


public class TestMain{

public static void main(String[] args) throws Exception{
//产生一个安全代理的对象方法
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

UserManager userManager = (UserManager)ctx.getBean("userManager");

User user = new User();
user.setAge(23);
user.setId(1);
user.setName("huangbiao");

userManager.aadUser(user);
}
}


<bean id="userManager" class="hb.com.UserManagerImp" />
<bean id="securityHandler" class="hb.com.SecurityHandler" />
<aop:config>
<aop:aspect id="asp" ref="securityHandler">
<aop:pointcut id="allAddMethod" expression="execution(* hb.com.UserManagerImp.*(..))" />
<aop:before method="checkSecurity" pointcut-ref="allAddMethod"/>
</aop:aspect>
</aop:config>
</beans>

 spring AOP术语:
 aspect----实现切面的类(SecurityHandler)
 advice----before after (实现切面的方法)
 pointcut----(过滤条件)指定横切的方法(add*())
 weave ---- 把advice应用到目标方法上的过程叫做织入
 target Object----目标对象
 Proxy ----代理
 Introduction ----动态添加方法

posted @ 2011-10-27 22:34  胖鹅  阅读(163)  评论(0编辑  收藏  举报