一、IOC反向控制:
反向控制,是工厂模式的简单实现。引用类的接口,在Client程序中,为了注入远程方法,直接将使用BeanFactory来得到希望得到的类。该类在XML文件中被定义。
1.applicationContext.xml文件定义
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="fileHelloWorld"
class="com.openv.spring.HelloWorld">
<constructor-arg>
<ref bean="fileHello"/>
</constructor-arg>
</bean>
<bean name="fileHello"
class="com.openv.spring.FileHelloStr">
<constructor-arg>
<value>helloworld.properties</value>
</constructor-arg>
</bean>
</beans>
2. Client调用:
Resource resource = new ClassPathResource("applicationContext.xml");//读入资源文件
BeanFactory factory = new XmlBeanFactory(resource);//构造Bean工厂
IHelloWorld hw = (IHelloWorld) factory.getBean("helloworldbean");//得到XML定义的Bean;
log.info(hw.getContent("luoshifei"));//调用BEAN方法。
二、AOP切面编程:
该功能主要是模拟EJB的日志,线程管理等任务性功能。不是业务功能。被称为装置。这些装置分为:提前装置,之后装置,异常装置,环绕装置(LoggingAroundAdvice)。
环绕装置具有其他所有的功能,因此最提倡使用。
1、 装置XML的定义模板:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloworldbean"
class="org.springframework.aop.framework.ProxyFactoryBean">//实现协议ProxyFactoryBean
<property name="proxyInterfaces">
<value>com.openv.spring.IHelloWorld</value>
</property>
<property name="target">//该属性“目标”代表要实际运行的Bean
<ref local="helloworldbeanTarget"/>
</property>
<property name="interceptorNames">//该属性“交互者”代表指向装置BEAN.
<list>
<value>loggingAroundAdvisor</value>
</list>
</property>
</bean>
<bean id="helloworldbeanTarget"
class="com.openv.spring.HelloWorld"/>
<bean id="loggingAroundAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="loggingAroundAdvice"/>
</property>
<property name="pattern">
<value>.*</value>
</property>
</bean>
<bean id="loggingAroundAdvice"
class="com.openv.spring.LoggingAroundAdvice"/>
</beans>
2、 装置的自我实现:
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Around装备实现
*
* @author luoshifei
*/
public class LoggingAroundAdvice implements MethodInterceptor {
protected static final Log log = LogFactory.getLog(LoggingAroundAdvice.class);
public Object invoke(MethodInvocation invocation) throws Throwable {
log.info("before: The Invocation of getContent()");
invocation.getArguments()[0] = "luoshifei";
invocation.proceed();
log.info("after: The Invocation of getContent()");
return null;
}
}
3、 装置会在目标对象动作时自我启动和按要求运行:
/**
* HelloWorld客户应用
*
* @author luoshifei
*/
public class HelloClient {
protected static final Log log = LogFactory.getLog(HelloClient.class);
public static void main(String[] args) {
Resource resource = new ClassPathResource("appcontext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
IHelloWorld hw = (IHelloWorld) factory.getBean("helloworldbean");//注意这个BEAN是实际的运行BEAN和装置绑定后的结果。
log.info(hw.getContent("luoshifei"));
}
}
1571

被折叠的 条评论
为什么被折叠?



