程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Spring 一二事(10),spring10

Spring 一二事(10),spring10

編輯:JAVA綜合教程

Spring 一二事(10),spring10


先貼出POM的內容,這個畢竟是用的maven來簡單構建的

 1 <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">
 2   <modelVersion>4.0.0</modelVersion>
 3   <groupId>org.springframework.samples</groupId>
 4   <artifactId>maven-spring002-aop</artifactId>
 5   <version>0.0.1-SNAPSHOT</version>
 6   
 7   <properties>
 8 
 9         <!-- Generic properties -->
10         <java.version>1.7</java.version>
11         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
13 
14         <!-- Spring -->
15         <spring-framework.version>3.2.3.RELEASE</spring-framework.version>
16 
17         <!-- Logging -->
18         <logback.version>1.0.13</logback.version>
19         <slf4j.version>1.7.7</slf4j.version>
20 
21         <!-- Test -->
22         <junit.version>4.11</junit.version>
23         
24         <!-- aspectj -->
25 <!--         <aspectj.version>1.8.7</aspectj.version> -->
26         <aspectj.version>1.6.6</aspectj.version>
27 
28     </properties>
29     
30     <dependencies>
31         <!-- Spring and Transactions -->
32         <dependency>
33             <groupId>org.springframework</groupId>
34             <artifactId>spring-context</artifactId>
35             <version>${spring-framework.version}</version>
36         </dependency>
37         <dependency>
38             <groupId>org.springframework</groupId>
39             <artifactId>spring-tx</artifactId>
40             <version>${spring-framework.version}</version>
41         </dependency>
42 
43         <!-- Logging with SLF4J & LogBack -->
44         <dependency>
45             <groupId>org.slf4j</groupId>
46             <artifactId>slf4j-api</artifactId>
47             <version>${slf4j.version}</version>
48             <scope>compile</scope>
49         </dependency>
50         <dependency>
51             <groupId>ch.qos.logback</groupId>
52             <artifactId>logback-classic</artifactId>
53             <version>${logback.version}</version>
54             <scope>runtime</scope>
55         </dependency>
56 
57         <!-- Test Artifacts -->
58         <dependency>
59             <groupId>org.springframework</groupId>
60             <artifactId>spring-test</artifactId>
61             <version>${spring-framework.version}</version>
62             <scope>test</scope>
63         </dependency>
64         <dependency>
65             <groupId>junit</groupId>
66             <artifactId>junit</artifactId>
67             <version>${junit.version}</version>
68             <scope>test</scope>
69         </dependency>
70 
71         <dependency>
72             <groupId>org.aspectj</groupId>
73             <artifactId>aspectjweaver</artifactId>
74             <version>${aspectj.version}</version>
75         </dependency>
76 
77     </dependencies>
78 
79     <build>
80         <plugins>
81             <plugin>
82                 <groupId>org.apache.maven.plugins</groupId>
83                 <artifactId>maven-compiler-plugin</artifactId>
84                 <configuration>
85                     <source>1.7</source>
86                     <target>1.7</target>
87                 </configuration>
88             </plugin>
89         </plugins>
90     </build> 
91 
92 </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

 1 package com.lee.spring003.aop.annotation;
 2 
 3 import org.springframework.stereotype.Repository;
 4 
 5 @Repository("teacherDAO")
 6 public class TeacherDAOImpl implements ITeacherDAO {
 7 
 8     @Override
 9     public String saveTeacher() {
10         System.out.println("TeacherDAOImpl - saveTeacher()");
11         return "save successfully";
12     }
13 
14 }

Transaction.java

 1 package com.lee.spring003.aop.annotation;
 2 
 3 import org.aspectj.lang.annotation.AfterReturning;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.aspectj.lang.annotation.Pointcut;
 7 import org.springframework.stereotype.Component;
 8 
 9 @Component("transaction")
10 @Aspect
11 public class Transaction {
12     
13     @Pointcut("execution(* com.lee.spring003.aop.annotation.TeacherDAOImpl.*(..))")
14     public void beginTransactionPointCut() {    // 方法簽名
15     };
16     
17     @Before("beginTransactionPointCut()")
18     public void beginTransaction() {
19         System.out.println("Begin transaction...");
20     }
21     
22     @AfterReturning("beginTransactionPointCut()")
23     public void commit() {
24         System.out.println("Transaction commit...");
25     } 
26     
27 }

 

github地址:https://github.com/leechenxiang/maven-spring002-aop

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved