spring aop 注解方式
spring aop 注解方式
杨中仁 发表于1年前
spring aop 注解方式
  • 发表于 1年前
  • 阅读 89
  • 收藏 3
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

今天大概看了一下spring4的jar 看到了aop  于是看看源码写了一下用例。比较简单,适用于 初学者。

aop的意思 大概应该都懂的。不懂的 可以先百度看看。 下面直接看下简单的例子吧。

 

第一  需要在项目的maven项目下面增加aspect 。

<?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>org.young</groupId>
    <artifactId>insane-conf</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>insane-dao</module>
        <module>insane-util</module>
        <module>insane-common</module>
        <module>insane-ba</module>
        <module>insane-app</module>
        <module>insane-wap</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <!-- 设置框架的版本  v -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        
        <spring.release.version>4.3.3.RELEASE</spring.release.version>
        <mybatis.version>3.3.0</mybatis.version>
        <mysql.version>5.1.26</mysql.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
    </properties>


    <dependencies>
        <!-- spring 框架 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.release.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.9.RELEASE</version>
        </dependency>

        <!--aop-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.9</version>
        </dependency>

        <!--log-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.6</version>
        </dependency>

        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>


        <!--配置阿里 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.4</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <!-- mybatis 框架 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>

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

    </dependencies>

</project>

第二 定义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: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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 使用注解 2016年09月28日23:38:24 -->
    <context:annotation-config/>

    <!-- 配置包名下面 所有的可以使用注解 注入 -->
    <context:component-scan base-package="org.young.common"></context:component-scan>

    <aop:aspectj-autoproxy/>

    <!--加载properties配置文件-->
    <!--<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
        <!--<property name="locations">-->
            <!--<list>-->
                <!--<value>classpath*:jdbc.properties</value>-->
            <!--</list>-->
        <!--</property>-->
    <!--</bean>-->

    <!--加载其他的spring配置文件-->
    <!--<import resource="spring-db.xml"/>-->

</beans>

 

第三  定义一个接口和实现类 代码如下。

package org.young.common.service;

/**
 * Created by young on 16/9/28.
 */
public interface UserService {

    String say(String name);

}
package org.young.common.service.impl;

import org.springframework.stereotype.Component;
import org.young.common.service.UserService;

/**
 * Created by young on 16/9/28.
 */
@Component("userService")
public class UserServiceImpl implements UserService{


    @Override
    public String say(String name){
        System.out.println("[UserServiceImpl say] --->>> " + name);
        return "[UserServiceImpl say] --->>> " + name;
    }

}

第四  定义一个规范  也就是对于这个service包下面 前后执行的业务类

package org.young.common.aop.aspect;

/**
 * Created by young on 16/10/1.
 */
public interface MQProductAspect {
}
package org.young.common.aop.aspect.impl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.young.common.aop.aspect.MQProductAspect;

/**
 * Created by young on 16/10/1.
 */
@Aspect
@Component
public class MQProductAspectImpl implements MQProductAspect{

    // 定义一个方法  2016年10月01日22:59:32
    @Pointcut(value = "execution ( * org.young.common.service.*.* (..))")
    public void pointcutMethod(){}

    // 在方法之前执行
    @Before(value = "pointcutMethod()")
    public void beforeMethod(){
        System.out.println("beforeMethod");
    }

    @Around(value = "pointcutMethod()")
    public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("aroundMethod begin");
        Object object = proceedingJoinPoint.proceed();
        System.out.println("aroundMethod end");
        return object;
    }

    @AfterReturning(value = "pointcutMethod()")
    public void afterRetunrningMethod(){
        System.out.println("afterRetunrningMethod ");
    }

    @AfterThrowing(value = "pointcutMethod()")
    public void afterThrowingMethod(){
        System.out.println("afterThrowingMethod ");
    }



    @After(value = "pointcutMethod()")
    public void afterMethod(){
        System.out.println("afterMethod");
    }
}

 

最后写一个类测试一下。

package org.young.common.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.young.common.service.UserService;

/**
 * Created by young on 16/10/1.
 */
public class MQProductAspectTest {

    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring/spring-context.xml");
        UserService userService = (UserService) applicationContext.getBean("userService");
        userService.say("yangzhongren");
    }

}

 

 

 

com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 org.young.common.test.MQProductAspectTest
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
aroundMethod begin
beforeMethod
[UserServiceImpl say] --->>> yangzhongren
aroundMethod end
afterMethod
afterRetunrningMethod 

Process finished with exit code 0

 

完成。

 

所有的代码  我都提交到了  

https://git.oschina.net/yangzhongren/insane.git

 

标签: spring aop 注解
共有 人打赏支持
粉丝 59
博文 36
码字总数 23334
×
杨中仁
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: