美文网首页
Spring Aop

Spring Aop

作者: 青柠_efca | 来源:发表于2019-03-08 01:05 被阅读0次

Aop

pom.xml中添加AOP相关依赖

<?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.spring</groupId>
    <artifactId>aop</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>aop</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.1.5.RELEASE</spring.version>
        <aspectj.version>1.9.2</aspectj.version>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.12</slf4j.version>
    </properties>

    <dependencies>
        <!--spring-context依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring-aop依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--aspectj依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <!--spring-test依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--junit依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- log4j日志依赖 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <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>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Move接口

package com.spring.work01;

public interface Move {
    void  move();
}

实现

package com.spring.work01;

public class Tank implements Move{
    @Override
    public void move() {
        System.out.println("tank is moving...");
    }
}
package com.spring.work01;

public class TankProxy implements Move{
    private  Move t;
    public  TankProxy(Move t){
        super();
        this.t=t;
    }
    @Override
    public void move() {
        System.out.println("starting");
        t.move();
        System.out.println("stop ");
    }
}
package com.spring.work01;

public class MoveApp {
    public static void main( String[] args )
    {
        System.out.println( "" );
    }
}

使用@AspectJ注解的例子

  • 定义业务模型
    Tiger类
package com.spring.aspectj;

public class Tiger {
    public void walk() {
        System.out.println("Tiger is walking...");
    }
}

  • 定义切面和配置
    前置通知和后置通知
    Fighter类
package com.spring.aspectj;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class Fighter {
    @Pointcut("execution(* com.spring.aspectj.Tiger.walk())")
    public void foundTiger() {

    }

    @Before(value = "foundTiger()")
    public void foundBefore() {
        System.out.println("Fighter wait for tiger...");
    }

    @AfterReturning("foundTiger()")
    public void foundAfter() {
        System.out.println("Fighter fight with tiger...");
    }
}

  • 配置文件
    <!--启动AspectJ支持-->
    <aop:aspectj-autoproxy/>
    <!--配置bean-->
    <bean id="tiger" class="com.spring.aspectj.Tiger"/>
    <bean id="fighter" class="com.spring.aspectj.Fighter"/>

  • 主程序
package com.spring.aspectj;

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

public class FighterApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Tiger tiger = context.getBean(Tiger.class);
        tiger.walk();
    }
}

在IDEA中进行远程库推送的步骤

1.在settings中设置gi t和github

2.创建本地项目和远程项目

3.在VCS菜单中,找到git的remotes, 用git协议添加远程库

4.编写程序

5.选中项目,ctrl+alt+a添加文件, 如果冲突,用菜单操作

6. ctrl+K, 进行commi t提交

7. ctrl+shift+k,进行push操作

注:

推荐文章:Spring AOP就是这么简单啦

相关文章

网友评论

      本文标题:Spring Aop

      本文链接:https://www.haomeiwen.com/subject/etxlpqtx.html