容器负责维护实例的,jndi是从容器中主动寻找实例,而ioc则是由容器被动注入到系统中
AOP面向切面编程,能更大的使模块重用,如日志记录每个类中都需加入,可单独抽象出来形成一个公共模块切面,使每个类都要经过。
简单例子:
类SpeakerInterface
package practice;
public interface SpeakerInterface {
public void sayHello();
}
类SpeakerImpl1
package practice;
public class SpeakerImpl1 implements SpeakerInterface{ //接口的实现类1
public void sayHello(){
System.out.println("hello 1!");
};
}
类SpeakerImpl2
package practice;
public class SpeakerImpl2 implements SpeakerInterface{ //接口的实现类2
public void sayHello(){
System.out.println("hello 2!!");
};
}
类Greeting
package practice;
public class Greeting {
private SpeakerInterface speaker;
public void setSpeaker(SpeakerInterface speaker){ //,面向接口编程,ioc由容器注入接口的实现类
this.speaker=speaker;
};
public void greet(){
speaker.sayHello();
};
}
类LogAdvice(在切面之前执行输出信息)
package practice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class LogAdvice implements MethodBeforeAdvice {
public void before(Method arg0,Object[] arg1,Object arg2)throws Throwable{
System.out.println("hello will appear");
};
}
测试类:
package practice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testPractice {
public static void main(String args[]){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Greeting greeting=(Greeting)context.getBean("Greeting");
greeting.greet();
};
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--以下为practice的例子-->
</bean>
<bean id="Speaker1" class="practice.SpeakerImpl1"></bean>
<bean id="Speaker2" class="practice.SpeakerImpl2"></bean>
<bean id="LogAdvice" class="practice.LogAdvice"></bean>
<bean id="Greeting" class="practice.Greeting">
<property name="speaker">
<ref bean="SpeakerProxy"></ref> --交给代理实现
</property>
</bean>
<bean id="SpeakerProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> --spring提供的代理类具有重要的三个属性
<property name="proxyInterfaces"> --需要代理的接口
<value>practice.SpeakerInterface</value>
</property>
<property name="interceptorNames"> --代理类
<list>
<value>LogAdvice</value>
</list>
</property>
<property name="target"> --需要代理对接口的实现类
<ref local="Speaker2"/>
</property>
</bean>
</beans>
ProxyFactoryBean每次配置台麻烦,用自动代理DefaultAdvisorAutoProxyCreator实现,并寻找插入点在特殊的方法前加入切面:
SpeakerInterface接口增加方法check(),修改后如下;
package automaticProxy;
public interface SpeakerInterface {
public void sayHello();
public void check();
}
package automaticProxy;
public class SpeakerImpl1 implements SpeakerInterface{
public void sayHello(){
System.out.println("hello 1!");
};
public void check(){
System.out.println("check");
};
}
package automaticProxy;
public class SpeakerImpl2 implements SpeakerInterface{
public void sayHello(){
System.out.println("hello 2!!");
};
public void check(){
System.out.println("check");}
}
package automaticProxy;
public class Greeting {
private SpeakerInterface speaker;
public void setSpeaker(SpeakerInterface speaker){
this.speaker=speaker;
};
public void greet(){
speaker.sayHello();
speaker.check();
};
}
package automaticProxy;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class LogAdvice implements MethodBeforeAdvice {
public void before(Method arg0,Object[] arg1,Object arg2)throws Throwable{
System.out.println("hello will appear");
};
}
package automaticProxy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testPractice {
public static void main(String args[]){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Greeting greeting=(Greeting)context.getBean("aGreeting");
greeting.greet();
};
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--以下为AOP的例子-->
<bean id="businessTarget" class="AOP.BusinessInterfaceImpl"/>
<bean id="myInterceptor" class="AOP.MyInterceptor"/>
<bean id="businessBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>AOP.BusinessInterface</value>
</property>
<property name="target">
<ref bean="businessTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>myInterceptor</value>
</list>
</property>
<!--以下为practice的例子-->
</bean>
<bean id="Speaker1" class="practice.SpeakerImpl1"></bean>
<bean id="Speaker2" class="practice.SpeakerImpl2"></bean>
<bean id="LogAdvice" class="practice.LogAdvice"></bean>
<bean id="Greeting" class="practice.Greeting">
<property name="speaker">
<ref bean="SpeakerProxy"></ref>
</property>
</bean>
<bean id="SpeakerProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>practice.SpeakerInterface</value>
</property>
<property name="interceptorNames">
<list>
<value>LogAdvice</value>
</list>
</property>
<property name="target">
<ref local="Speaker2"/>
</property>
</bean>
<!--以下为automaticProxy的例子-->
<bean id="aSpeaker1" class="automaticProxy.SpeakerImpl1"></bean>
<bean id="aSpeaker2" class="automaticProxy.SpeakerImpl2"></bean>
<bean id="aLogAdvice" class="automaticProxy.LogAdvice"></bean>
<bean id="aGreeting" class="automaticProxy.Greeting">
<property name="speaker">
<ref bean="aSpeaker1"></ref>
</property>
</bean>
<bean id="aSpeakerProxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> --动态代理
</bean>
<bean id="logBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> --定义切入点
<property name="advice">
<ref bean="aLogAdvice"/></property>
<!--指定以do开头的方法-->
<property name="patterns"> --切入点匹配的方法,所有正则匹配方法前均增加切面
<value>.*check.*</value>
</property>
</bean>
</beans>
浙公网安备 33010602011771号