J2EE学习--Spring

J2EE进阶学习——Spring框架(五):AOP

2018-03-13  本文已影响15人  TiHom

AOP概念

1.AOP:面向切面(方面)编程,扩展功能不修改源代码实现

2.AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码(性能监测、事务管理、安全检查、缓存)


AOP原理

未改进的做法

纵向抽取机制

动态代理方式

横向抽取机制

AOP操作术语(重点掌握加粗的三个)

操作术语

Spring的AOP操作

1.在Sping里面进行AOP操作,使用aspectj实现

2.使用aspectj实现AOP有两种方式

AOP操作的准备工作:
<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

</beans>

附上文档网站:https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/xsd-configuration.html

3.使用表达式配置切入点

4.基于AspecJ的配置文件方式的AOP操作

Book

public class Book {
    public void add(){
        System.out.println("add...");
    }
}

MyBook

public class MyBook {
    public void before1(){
        System.out.println("before...");
    }

    public void after1(){
        System.out.println("after...");
    }

    //环绕增强
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("方法之前");
        //执行被增强的方法
        proceedingJoinPoint.proceed();
        System.out.println("方法之后");
    }
}

bean3.xml

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    <aop:aspectj-autoproxy />
    <bean id="book" class="com.TiHom.aop.Book"></bean>
    <bean id="myBook" class="com.TiHom.aop.MyBook"></bean>

    <aop:config>
        <aop:pointcut id="pointcut1" expression="execution(* com.TiHom.aop.Book.*(..))"/>
        <aop:aspect ref="myBook">
            <aop:before method="before1" pointcut-ref="pointcut1"/>
            <aop:after-returning method="after1" pointcut-ref="pointcut1"/>
            <aop:around method="around" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
</beans>    

5.log4j介绍

6.spring web整合

让spring的配置文件在服务器启动的时候加载

要导入spring-web的jar包,这里贴一个spring jar包下载地址
https://repo.spring.io/webapp/#/artifacts/browse/tree/General/libs-release-local/org/springframework/spring-web/4.3.1.RELEASE/spring-web-4.3.1.RELEASE.jar

在web.xml中配置

<!-- 指定监听器的位置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring配置的位置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:bean1.xml</param-value>
</context-param>

7.基于Aspecj注解的AOP操作

切面中

@Aspect
public class MyBook {
    @Before(value = "execution(* com.TiHom.aop.Book.*(..))")
    public void before1(){
        System.out.println("before...");
    }
}

bean3.xml中

    <!-- 开启AOP操作 -->
    <aop:aspectj-autoproxy />
    <!-- 创建对象 -->
    <bean id="book" class="com.TiHom.aop.Book"></bean>
    <bean id="myBook" class="com.TiHom.aop.MyBook"></bean>
上一篇 下一篇

猜你喜欢

热点阅读