Spring重温(六)— — AOP入门

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

前言:

        spring除了ioc重要外,还有一个就是重中之重的AOP了。面向切面编程,我们可以不修改原来代码的情况下,可以让程序更加灵活。AOP理解是比较难的,但是相对来说用却是很简单。只要了解一下切点,切面等,差不多就会用了。


不废话啦!开始学习~!

开发环境:

        idea+win10

        所需要的包:

            有很多是不是特别需要的。小编用之前项目写的。没有太多去改。凑合着来吧!

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wen</groupId>
    <artifactId>testRedis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--版本控制-->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.3.7.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.3.0</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.7.5</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <jboss.version>3.1.0.CR2</jboss.version>
        <jackson.version>2.9.2</jackson.version>     
        <jstl.version>1.2</jstl.version>
        <jsp-api.version>2.0</jsp-api.version>
        <joda-time.version>2.5</joda-time.version>
        <commons-lang3.version>3.3.2</commons-lang3.version>
        <commons-io.version>2.4</commons-io.version>
        <commons-net.version>3.3</commons-net.version>
        <jsqlparser.version>0.9.1</jsqlparser.version>
        <commons-fileupload.version>1.3.1</commons-fileupload.version>
        <jackson.version>2.9.2</jackson.version>
        <jackson-mapper.version>1.9.11</jackson-mapper.version>
    </properties>




        <dependencies>
            <!-- 云片短信接口版本-->
            <!-- https://mvnrepository.com/artifact/com.yunpian.sdk/yunpian-java-sdk -->
            <dependency>
                <groupId>com.yunpian.sdk</groupId>
                <artifactId>yunpian-java-sdk</artifactId>
                <version>${yunpian-sdk.bersion}</version>
            </dependency>

            <!--jackon 工具-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>${jackson.version}</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
            <dependency>
                <groupId>org.codehaus.jackson</groupId>
                <artifactId>jackson-mapper-asl</artifactId>
                <version>${jackson-mapper.version}</version>
            </dependency>


            <!-- Apache工具组件 -->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
                <version>${commons-lang3.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>${commons-net.version}</version>

            </dependency>
            <!-- Jackson Json处理工具包 -->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>${jackson.version}</version>
            </dependency>

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



            <!-- httpclient -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpclient.version}</version>
            </dependency>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- 日志处理 -->
            <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${slf4j.version}</version>
            </dependency>

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.version}</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.jboss.logging</groupId>
                <artifactId>jboss-logging</artifactId>
                <version>${jboss.version}</version>
            </dependency>

           
            <!-- Spring start -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jsp-api.version}</version>
                <scope>provided</scope>
            </dependency>
            <!-- 文件上传组件 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${commons-fileupload.version}</version>
            </dependency>
            
            <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>
 
            <dependency>
                <groupId>tomcat</groupId>
                <artifactId>servlet-api</artifactId>
                <version>5.5.23</version>
            </dependency>
        </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>


</project>
            上面的搞定后,我们先写一个对象类~!没什么好说的,就是这样的。这步可以略过~
package com.wen.test;

public class User {
    private String name;
    private String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
        我们在准备一个类,这个类的方法是要作为一个被切的一个点。

package com.wen.test;

public class MyOrders {

    public User orders(){
        System.out.println("居然这样。。之前被人切了额");
        User user=new User();
        user.setAge("100");
        user.setName("123");
        return user;
    }
}
        还需要一个切这个方法的类,并且类里需要有切这个上面那个点的方法。
package com.wen.test;

import org.aspectj.lang.ProceedingJoinPoint;

public class AOPController {
    //这个方法将要切的方法。
    public void ControllerAop(ProceedingJoinPoint point ) throws Throwable {
        System.out.println("开始切了");
        //调用被切的点,可以取出切的那个方法
        User user= (User) point.proceed();
        user.setName("文");
        System.out.println(user);
        System.out.println("切完了");
    }

}
        好了,介绍完了,我们看看配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

    <!--定义spring组件的扫描器 -->
    <context:component-scan base-package="com.wen.test"/>

    <aop:aspectj-autoproxy/>
    <mvc:annotation-driven/>

    <bean id="myOrders" class="com.wen.test.MyOrders"></bean>

    <bean id="controllerAop" class="com.wen.test.AOPController"/>
    <!--SpringAop 切面-->
    <aop:config>
        <aop:aspect id="myAop" ref="controllerAop">
            <!--切点-->
            <aop:pointcut id="target"
                          expression="execution(* com.wen.test.MyOrders.orders(..)) "/>
         <aop:around method="ControllerAop" pointcut-ref="target"/>
        </aop:aspect>
    </aop:config>


</beans>

        没了,下面是测试类!

package com.wen.servlet;

import com.wen.test.MyOrders;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {


    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-mvc.xml");
       MyOrders my= (MyOrders) applicationContext.getBean("myOrders");
        my.orders();

    }
}

        测试结果:

        


        搞定~1大功告成。写的有些乱,这只是做个入门的。后续在慢慢完善。






阅读更多
想对作者说点什么?

博主推荐

换一批

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