Spring中aop用法

1.pom.xml

<!-- 7个AOP所需包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>5.0.8.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.9.2</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.9.2</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib-nodep</artifactId>
			<version>3.2.5</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>

2.application.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: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-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">

    <bean id="person" class="com.woodata.test.Person"></bean>
	<bean id="personAspect" class="com.woodata.test.PersonAspect"></bean>
	
	<aop:config>
		<!-- 切点(在哪里做) -->
		<aop:pointcut expression="execution(* *.*.*.*.*(..))" id="pointct"/>
		<!-- 切面-->
		<aop:aspect ref="personAspect">
			<aop:before method="before" pointcut-ref="pointct"/>
			<aop:after method="after" pointcut-ref="pointct"/>
		</aop:aspect>
		
	</aop:config>
</beans>

person类

package com.woodata.test;

public class Person {
	public void goWC(){
		System.out.println("我在上厕所");
	}
	public void goEat(){
		System.out.println("我在吃饭");
	}
}

personAspect类

package com.woodata.test;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.aspectj.lang.JoinPoint;
public class PersonAspect {
	//前置通知
	public void before(JoinPoint jp){
		Date date = new Date();
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date1 = sm.format(date);
		System.out.println(date1+":"+jp.getSignature().getName()+"方法开始执行了");
	}
	//后置通知
	public void after(JoinPoint jp){
		Date date = new Date();
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date1 = sm.format(date);
		System.out.println(date1+":"+jp.getSignature().getName()+"方法执行完毕");
	}
}

test2测试类

package com.woodata.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.woodata.entity.HelloWorld;

public class test2 {
	public static void main(String[] args) {
		//加载配置文件ClassPathXmlApplictionContext,解析配置文件
		ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("application.xml");
		//获取bean对象
		Person person =  (Person)app.getBean(Person.class);
		person.goWC();
		person.goEat();
	}
}

效果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值