简介:
这几天有在学习Spring框架,学习资源有W3school的教程以及油管上韩顺平的spring教程,不过视频是2011年录制的。
因为W3上的内容太过简洁,于是不难以理解AOP的情况下看了一下视频教程,从而对AOP概念有了初步的了解。
当我再次回到W3尝试理解其中所讲的AOP的时候,发现两者的不同:
W3:
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:config>
<aop:aspect id="log" ref="logging">
<aop:pointcut id="selectAll"
expression="execution(* com.tutorialspoint.*.*(..))"/>
<aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
<aop:after pointcut-ref="selectAll" method="afterAdvice"/>
<aop:after-returning pointcut-ref="selectAll"
returning="retVal"
method="afterReturningAdvice"/>
<aop:after-throwing pointcut-ref="selectAll"
throwing="ex"
method="AfterThrowingAdvice"/>
</aop:aspect>
</aop:config>
<!-- Definition for student bean -->
<bean id="student" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean>
<!-- Definition for logging aspect -->
<bean id="logging" class="com.tutorialspoint.Logging"/>
</beans>
HSP:
<?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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean name="testService" class="com.tutorialspoint.TestService">
<property name="name" value="hongchang"></property>
</bean>
<bean name="testService2" class="com.tutorialspoint.TestService">
<property name="name" value="dydy"></property>
</bean>
<bean name="myMethodBeforeAdvice" class="com.tutorialspoint.MyMethodBeforeAdvice"></bean>
<bean name="myMethodAfterAdvice" class="com.tutorialspoint.MyMethodAfterAdvice"></bean>
<bean name="proxyFactoryBean1" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>com.tutorialspoint.TestServiceInter</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
<value>myMethodAfterAdvice</value>
</list>
</property>
<property name="target" ref="testService">
</property>
</bean>
</beans>
可以发现,W3 (2016编辑)
更接近现在的编写方式:AOP 的 XML架构 -------- 使用 aop 命名空间标签
更加简洁,而且在调用的层面来看,现在的方式也更加合理:
W3:
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml");
Student student = (Student) context.getBean("student");
student.getName();
student.getAge();
student.printThrowException();
}
}
像完全没有配置aop一样进行正常的beans的获取和使用
HSP:
public class MainApp {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
TestServiceInter ts = (TestServiceInter)context.getBean("proxyFactoryBean1");
ts.sayHello();
}
}
而以前,我们发现我们需要通过获取代理对象并转成相应接口来调用相应方法,从某种程度来看,这是一种耦合的表现。