您的位置:首页 > 编程语言 > Java开发

spring aop简单编程

2013-08-15 16:58 316 查看
1、工程需要的jar包:

<properties>

<org.springframework.version>3.0.5.RELEASE</org.springframework.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${org.springframework.version}</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.6.8</version>

</dependency>

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.7.0</version>

</dependency>

</dependencies>

2、定义接口:

package com.csair.test;

public interface TestService {

void test();

}

3、实现接口:

package com.csair.test;

public class TestServiceImpl implements TestService {

public void test() {

System.out.println("hello test");

}

}

4、定义切入点:

package com.csair.interceptor;

import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.Before;

@Aspect

public class MyInterceptor {

@Before("execution(* com.csair.test.**.*(..))")

public void doSomething() {

System.out.println("hello interceptor");

}

}

5、applicationContext文件:

<?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:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"

default-lazy-init="true">

<aop:aspectj-autoproxy/>

<bean id="myInterceptor" class="com.csair.interceptor.MyInterceptor"></bean>

<bean id="testServiceImpl" class="com.csair.test.TestServiceImpl"></bean>

<!--

public class AopTest {

public void testBefore() {

System.out.println("testBefore");

}

public void testAfter() {

System.out.println("testAfter");

}

}

public class UserServiceImpl implements UserService{

public void addUse(String name) {

System.out.println("hello " + name);

}

}

public static void main(String[] args) {

String configLocation = "applicationContext-aop.xml";

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(configLocation);

UserService service = (UserService)context.getBean("service");

service.addUse("ppt");

}

<bean id="service" class="com.csair.aop.UserServiceImpl"></bean>

<bean id="aspect" class="com.csair.aop.AopTest"></bean>

<aop:config>

<aop:aspect id="myAspect" ref="aspect">

<aop:pointcut expression="execution(* com.csair.aop.**.*(..))" id="pointcut"/>

<aop:before method="testBefore" pointcut-ref="pointcut"/>

<aop:after method="testAfter" pointcut-ref="pointcut"/>

</aop:aspect>

</aop:config>

-->

</beans>

6、测试:

package com.csair.test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

TestService test = (TestService)context.getBean("testServiceImpl");

test.test();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: