spring学习笔记(十四)

如果不实现接口,必须使用cglib库的支持才行.   
spring 会自动在CGLIB和JDK动态代理之间切换  

 

spring学习笔记(十四)

 

package zhc.love.dj.cglib;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Client {

	public static void main(String[] args) {
		//用spring的类来加载配置文件
		BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
		//通过工厂产生类 调用对应类的id
		UserMangerImp mangaer = (UserMangerImp)factory.getBean("UserManger");
		mangaer.addUser("zhang", "jingjing");
         // mangaer.delUser(1);
	}

}

 

  

package zhc.love.dj.cglib;

public class UserMangerImp  {

	public void addUser(String name, String password) {
		System.out.println("==============UserMangerImp.addUser()===================");
	}

	public void delUser(int id) {
		System.out.println("==============UserMangerImp.delUser()===================");
	}

	public void modUser(int id, String name, String password) {
		System.out.println("==============UserMangerImp.modUser()===================");
	}
}

 

  

package zhc.love.dj.cglib;

import org.aspectj.lang.JoinPoint;



public  class MySecurtyMangerImp  {
    
	public void checkSecurity(JoinPoint joinPoint) {	
		System.out.println("joinPoint =" + joinPoint.getSignature().getName());
		
		Object[]  args = joinPoint.getArgs();
		
		for(int i=0;i<args.length;i++){
			System.out.println(args[i]);
		}
		System.out.println("=========checkSecurity()=========");
	}
	
}

 

 

 

<?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.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<aop:config>
	<aop:aspect id="MySecurty" ref="MySecurtyMangerImp">
		<aop:pointcut id="allAddMethod" expression="execution(* add*(..))||execution(* del*(..))" />
		<aop:before pointcut-ref="allAddMethod" method="checkSecurity"/>
	</aop:aspect>
</aop:config>
<bean id="MySecurtyMangerImp" class="zhc.love.dj.cglib.MySecurtyMangerImp"></bean>
<bean id="UserManger" class="zhc.love.dj.cglib.UserMangerImp"></bean>

</beans>

 

你可能感兴趣的:(spring,AOP,jdk,bean,xml)