为了账号安全,请及时绑定邮箱和手机立即绑定

Spring 一二事(10) - annotation AOP

标签:
Java

先贴出POM的内容,这个毕竟是用的maven来简单构建的

<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.springframework.samples</groupId>
  <artifactId>maven-spring002-aop</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <properties>

        <!-- Generic properties -->
        <java.version>1.7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Spring -->
        <spring-framework.version>3.2.3.RELEASE</spring-framework.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.7</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

        <!-- aspectj -->
<!--         <aspectj.version>1.8.7</aspectj.version> -->
        <aspectj.version>1.6.6</aspectj.version>

    </properties>

    <dependencies>
        <!-- Spring and Transactions -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Test Artifacts -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring-framework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

    </dependencies>

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

</project>

关于AOP的配置,我个人还是喜欢用XML来配置,一来方便管理,看的清楚,一个项目的aop也不会太多,二来注解形式的不好管理

applicationContext.xml的话只要有2行就行

1    <context:component-scan base-package="com.lee.spring003.aop.annotation"></context:component-scan>
2     
3     <!-- 自动创建代理 -->
4     <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

ITeacherDAO.java

1 package com.lee.spring003.aop.annotation;
2 
3 public interface ITeacherDAO {
4     public String saveTeacher();
5 }

TeacherDAOImpl.java

package com.lee.spring003.aop.annotation;

import org.springframework.stereotype.Repository;

@Repository("teacherDAO")
public class TeacherDAOImpl implements ITeacherDAO {

    @Override
    public String saveTeacher() {
        System.out.println("TeacherDAOImpl - saveTeacher()");
        return "save successfully";
    }

}

Transaction.java

package com.lee.spring003.aop.annotation;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component("transaction")
@Aspect
public class Transaction {

    @Pointcut("execution(* com.lee.spring003.aop.annotation.TeacherDAOImpl.*(..))")
    public void beginTransactionPointCut() {    // 方法签名
    };

    @Before("beginTransactionPointCut()")
    public void beginTransaction() {
        System.out.println("Begin transaction...");
    }

    @AfterReturning("beginTransactionPointCut()")
    public void commit() {
        System.out.println("Transaction commit...");
    } 

}
点击查看更多内容
“小礼物走一走,来慕课关注我”
赞赏支持
风间影月说 去围观
创业公司技术总监, 10年+开发和技术管理经验。SUN认证SCJP、PMP、MCP认证。主要从事后端技术和架构领域,有丰富的电商平台与物流平台核心系统的架构设计和开发经验。
评论

作者其他优质文章

正在加载中
全栈工程师
手记
粉丝
1.2万
获赞与收藏
914

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消