1java的动态代理
1.1建立一个接口
public interface IPreson {
//吃
public void eat();
}
1.2创建一个实现类
public class XiaoMing implements IPreson {
@Override
public void eat() {
System.out.println("hahah");
}
}
1.3创建代理实现接口(InvocationHandler)
public class Proxy implements InvocationHandler {
private IPreson pr;
public Proxy(IPreson pr) {
this.pr = pr;
}
//实现InvocationHandler方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(pr,args);
}
}
1.4测试
public class proxy2 {
public static void main(String[] args) {
//里氏替换原则
Ipreson ip = new XiaoMing();
//实例化代理类
pojo.Proxy proxy = new pojo.Proxy(ip);
//调用动态代理方法
Ipreson ip= ( Ipreson ) Proxy.newProxyInstance(XiaoMing.class.getClassLoader(), XiaoMing.class.getInterfaces(), proxy);
//实现吃
ir.eat();
spring代理
1.接口
public interface Iperson {
public void slip();
public void personadd();
}
2.实现类
@Override
public void slip() {
System.out.println("睡觉!");
}
3.spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!--配置目标-->
<bean id="xm" class="Xiaoming"></bean>
<!--配置混合代理-->
<bean id="myproxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--引用目标对象-->
<property name="target" ref="xm"></property>
<property name="proxyInterfaces">
<list>
<value>Iperson</value>
</list>
</property>
</bean>
</beans>
4.测试
@Test
public void Springproxy() {
//加载spring.xml
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//获取bean
Iperson ip = (Iperson) applicationContext.getBean("myproxy");
//实现方法
ip.slip();
}
2通知
前置通知:在方法前调用(可以存放session)实现MethodBeforeAdvice接口
public class mybefore implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("使用了前置通知");
}
}
后置通知:在方法后调用(可以做权限的判断)实现AfterReturningAdvice接口
public class myafor implements AfterReturningAdvice{
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("我是后置通知");
}
}
环绕通知:在方法执行前后执行(日志)实现MethodInterceptor接口
public class Hrtongzhi implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("环绕通知");
return methodInvocation.proceed();
}
}
配置spring.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
<!--配置目标-->
<!--配置前置通知-->
<bean id="mybefore" class="mybefore"></bean>
<!--配置通知的过滤-->
<bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="mybefore"> </property>
<property name="pattern" value=".*add.*"></property>
</bean>
<!--配置后置通知->
<bean id="afore" class="myafor"></bean>
<!--配置环绕通知-->
<bean id="hr" class="Hrtongzhi"></bean>
<!--配置混合代理-->
<bean id="myproxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--引用目标对象-->
<property name="target" ref="xm"></property>
<!--引用接口-->
<property name="proxyInterfaces">
<list>
<value>Iperson</value>
</list>
</property>
<property name="interceptorNames" >
<!--应用通知-->
<list>
<!--引用过滤前置通知-->
<idref bean="advisor"></idref>
<!--引用前置通知-->
<!--<idref bean="mybefore"></idref>-->
<!--引用后置通知-->
<!--<idref bean="afore"></idref>-->
<!--引用环绕通知-->
<!--<idref bean="hr"></idref>-->
</list>
</property>
</beans>
3测试
@Test
public void Springproxy() {
//加载spring.xml
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
//获取bean
Iperson ip = (Iperson) applicationContext.getBean("myproxy");
//实现方法
ip.slip();
//实现方法过滤通知
ip.presonadd();
}