AOP学习心得

简介:

这几天有在学习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();
    }
}

而以前,我们发现我们需要通过获取代理对象并转成相应接口来调用相应方法,从某种程度来看,这是一种耦合的表现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值