spring aop

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_38672391/article/details/80693869

AOP (Aspect-Oriented Programming:面向其切面编程)是一种编程范式,一种新的方法论,是对传统OOP的补充,其主要编程对象是切面(aspect),而切面关注切点。
AOP 优势:
1.降低模块耦合度,增强代码的复用性
2.使系统更加容易扩展
AOP 术语
切面(Aspect):一个横切关注点的模块化,这个关注点可能会横切多个对象。它是横切关注点的另一种表达方式。
连接点(Joinpoint):在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。
切入点(Pointcut):匹配连接点的断言。它通常是一个表达式,有专门的语法,用于指明在哪里(或者说在哪些方法调用上)嵌入横切逻辑
通知(Advice):在切面的某个特定的连接点上执行的动作,也就是我们前面提到的横切逻辑,如日志处理逻辑,事务处理逻辑。
目标对象(Target Object):被一个或者多个切面所通知的对象,也被称作被通知对象
代理对象(Proxy Object):AOP框架创建的对象,它和目标对象遵循同样的接口,使用它的方式和使用目标对象的方式是一样的,但是它是目标对象的增强版,“通知”中的代码执行将会被代理对象的方法调用触发。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。

Spring AOP相关Jar包
spring-aop-4.3.3.RELEASE.jar,
aspectjweaver-1.8.5.jar
aspectjrt-1.8.5.jar
Studen类

package com.springlearn.spring.aop.moudel;
/**
 * Student model
 * 记录学生的信息
 *
 */
public class Student {

    private int studentId;

    private String studentName;

    private String sex;

    StudentCard stuCard;

    /**
     * @return the studentId
     */
    public int getStudentId() {
        return studentId;
    }

    /**
     * @param studentId the studentId to set
     */
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    /**
     * @return the studentName
     */
    public String getStudentName() {
        return studentName;
    }

    /**
     * @param studentName the studentName to set
     */
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    /**
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * @param sex the sex to set
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * @return the stuCard
     */
    public StudentCard getStuCard() {
        return stuCard;
    }

    /**
     * @param stuCard the stuCard to set
     */
    public void setStuCard(StudentCard stuCard) {
        this.stuCard = stuCard;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Student [studentId=" + studentId + ", studentName=" + studentName + ", sex=" + sex + ", stuCard="
                + stuCard + "]";
    }




}

StudentAspect类

package com.springlearn.spring.aop.aspect;

import java.math.BigDecimal;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

//@Aspect
public class StudentAspect {

    public void pointCut(){

    }

    public void beforeAdvice(JoinPoint jp){
        System.out.println("前置通知:方法:" + jp.getSignature().getName() + "将要被执行") ; 
        System.out.println("--------------->" + jp);
    }

    public void afterAdvice(JoinPoint jp){
        System.out.println("后置通知:方法:" + jp.getSignature().getName() + "执行完毕");
    }

    public void afterReturnAdvice(JoinPoint jp, Object result){
        System.out.println("后置结果通知:方法:" + jp.getSignature().getName() + "执行完毕, 返回值: " + result) ;
    }

    public void arountAdvice(ProceedingJoinPoint pjp){
        String methodName =  pjp.getSignature().getName();
        System.out.println("环绕通知 ----》方法:" + pjp.getSignature().getName() + "将要被执行") ; 
        try {
            Object returnValue = pjp.proceed();
            System.out.println("------------------->方法:" + pjp.getSignature().getName() +"执行完毕:返回值为:" + returnValue);
        } catch (Throwable e) {
            e.printStackTrace();
        } //连接点开始执行
    }

    public void throwsAdvice(JoinPoint jp, Exception e){
        System.out.println("异常通知:方法" + jp.getSignature().getName() + "执行时抛出异常:" + e);
    }

}

applicationContext.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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/util 
               http://www.springframework.org/schema/util/spring-util.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd ">
    <bean id="student" class="com.springlearn.spring.aop.moudel.Student"></bean>

    <bean id="studentAspect" class="com.springlearn.spring.aop.aspect.StudentAspect"></bean>

    <bean id="studentCard" class="com.springlearn.spring.aop.moudel.StudentCard"></bean>

    <bean id="studentService" class="com.springlearn.spring.aop.service.StudentService"></bean>

    <aop:config>
        <aop:pointcut expression="execution(* com.springlearn.spring.aop.service.*.*(..))" id="studentPointCut"/>
        <aop:aspect ref="studentAspect">
            <aop:before method="beforeAdvice" pointcut-ref="studentPointCut"/>
            <aop:after method="afterAdvice" pointcut-ref="studentPointCut"/>
            <aop:after-returning method="afterReturnAdvice" pointcut-ref="studentPointCut" returning="result"/>
            <aop:around method="arountAdvice" pointcut-ref="studentPointCut"/>
        </aop:aspect>
    </aop:config>
     <!-- 
            <aop:after-returning method="" returning="result" pointcut-ref="beforeAdvice"/>
            <aop:around method=""/>
            <aop:after-throwing method="" throwing="e" pointcut-ref="beforeAdvice"/>
             -->
</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.lw.spring.learn</groupId>
    <artifactId>spring.learn</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring.learn Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.5</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.5</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>spring.learn</finalName>
    </build>
</project>

StudentCard类

package com.springlearn.spring.aop.moudel;

import java.math.BigDecimal;
/**
 * 学生卡类
 * 记录学生卡的信息 
 *
 */
public class StudentCard {

    private int studentId;

    private String account;

    private String password;

    private BigDecimal money;

    /**
     * @return the studentId
     */
    public int getStudentId() {
        return studentId;
    }

    /**
     * @param studentId the studentId to set
     */
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    /**
     * @return the account
     */
    public String getAccount() {
        return account;
    }

    /**
     * @param account the account to set
     */
    public void setAccount(String account) {
        this.account = account;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the money
     */
    public BigDecimal getMoney() {
        return money;
    }

    /**
     * @param money the money to set
     */
    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "StudentCard [studentId=" + studentId + ", account=" + account + ", password=" + password + ", money="
                + money + "]";
    }

}

StudentService类

package com.springlearn.spring.aop.service;

import org.springframework.stereotype.Service;

import com.springlearn.spring.aop.moudel.StudentCard;

@Service("studentService")
public class StudentService {

    public void showStudentCardInfo(StudentCard studentCard){
        System.out.println(studentCard.toString());
    }

    public StudentCard getStudentCardInfo(StudentCard studentCard){
        return studentCard;
    }

}

StudentDemo类

package demo;

import java.math.BigDecimal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springlearn.spring.aop.moudel.StudentCard;
import com.springlearn.spring.aop.service.StudentService;

public class StudentDemo {

    public static void main(String[] args){
        ApplicationContext ioc = new  ClassPathXmlApplicationContext("config/spring/applicationContext.xml");
        StudentCard studentCard = (StudentCard) ioc.getBean("studentCard");
        studentCard.setAccount("1234564");
        studentCard.setMoney(new BigDecimal(100));
        studentCard.setPassword("12314546");
        studentCard.setStudentId(12);
        StudentService studentService = (StudentService) ioc.getBean("studentService");
        studentService.showStudentCardInfo(studentCard);
        studentService.getStudentCardInfo(studentCard);

    }

}
阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页