`

Spring AOP

 
阅读更多
package com.deppon.test04.bean;
public interface Sleepable {
    void sleep();
}

package com.deppon.test04.bean;
public class Human implements Sleepable {

    public void sleep() {
        System.out.println("睡觉吧 Sleep");
    }
}


package com.deppon.test04.bean;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SleepHelper {
    public SleepHelper() {
       
    }
   
    @Pointcut("execution(* *.sleep())")
    public void sleeppoint(){}
   
   
    @Before("sleeppoint()")
    public void beforeSleep(){
        System.out.println("睡觉前要脱衣服 Before Sleep");
    }
   
    @AfterReturning("sleeppoint()")
    public void afterSleep(){
        System.out.println("起床后要穿衣服 After Sleep");
    }
   
}



测试类:
package com.deppon.test04.bean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AspectJTesting {

    public static void main(String[] args) {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("/applicationContext.xml");
       
        Sleepable sleeper = (Sleepable)beanFactory.getBean("human");
        sleeper.sleep();
    }
}


applicationContext.xml:

<?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:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx  
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/aop  
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <!-- AOP -->
    <aop:aspectj-autoproxy/>
    <bean id="human" class="com.deppon.test04.bean.Human"></bean>
         
    <bean id="sleepHelper" class="com.deppon.test04.bean.SleepHelper">
    </bean> 
    <aop:config>
        <aop:aspect ref="sleepHelper">
            <aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))" />
            <aop:after method="afterSleep" pointcut="execution(* *.sleep(..))" />
        </aop:aspect>
    </aop:config>
   
</beans>
pom.xml

<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>SpringWeb</groupId>
  <artifactId>SpringWeb</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <!-- 属性配置 --> 
  <properties> 
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
      <spring.version>3.2.3.RELEASE</spring.version>
     
      <pkgGroupId>org.aspectj</pkgGroupId>
      <pkgArtifactId>aspectj</pkgArtifactId>
      <aspectj.version>1.8.2</aspectj.version>
      <servicemix.osgi.export.pkg>
            org.aspectj
        </servicemix.osgi.export.pkg>
        <servicemix.osgi.import.pkg>
            javax.xml*,
            org.xml.sax*,
            com.bea.jvm;resolution:=optional,
            org.objectweb.asm;resolution:=optional,
            org.apache.commons.logging;resolution:=optional,
            aj.org.objectweb.asm;resolution:=optional
        </servicemix.osgi.import.pkg>
  </properties> 
   
  <dependencies> 
    <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.10</version> 
      <scope>test</scope> 
    </dependency> 
     
    <!-- 添加Spring依赖 --> 
    <dependency> 
        <groupId>org.springframework</groupId> 
        <artifactId>spring-core</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-context</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-test</artifactId>
        <version>${spring.version}</version>
    </dependency>
           
    <dependency>
            <groupId>${pkgGroupId}</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>${pkgGroupId}</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <!-- sources -->
        <dependency>
            <groupId>${pkgGroupId}</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
            <classifier>sources</classifier>
        </dependency>
        <dependency>
            <groupId>${pkgGroupId}</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
            <classifier>sources</classifier>
        </dependency>

   
                         
  </dependencies> 
  <build>
    <finalName>test04</finalName>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <includes>
                                    <include>${pkgGroupId}:aspectjrt</include>
                                    <include>${pkgGroupId}:aspectjweaver</include>
                                </includes>
                            </artifactSet>
                            <filters>
                                <filter>
                                    <artifact>${pkgGroupId}:aspectjrt</artifact>
                                    <excludes>
                                        <exclude>**</exclude>
                                    </excludes>
                                </filter>
                                <filter>
                                    <artifact>${pkgGroupId}:aspectjweaver</artifact>
                                    <excludes>
                                        <exclude>**</exclude>
                                    </excludes>
                                </filter>
                            </filters>                           
                            <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
  </build>
</project>

参考:
http://www.mkyong.com/spring3/spring-aop-aspectj-annotation-example/
http://www.mkyong.com/spring/spring-aop-examples-advice/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics