执行方法[置顶] aspectj动态代理

在写这篇文章之前,xxx已经写过了几篇关于改执行方法主题的文章,想要了解的朋友可以去翻一下之前的文章

    记笔之用....

    

    UserService

package com.spring.test;

import org.springframework.stereotype.Component;

@Component
public interface UserService {
	
	public void createUser();
	
	public void deleteUser();
	
	public void updateUser(int id);

}

    实现类UserDao

package com.spring.test;

import org.springframework.stereotype.Component;

@Component
public class UserDao implements UserService {

	public void createUser() {
		System.out.println("user saved...");

	}
	
	public void deleteUser(){
		System.out.println("delete user...");
	}
	
	public void updateUser(int id){
		System.out.println("update user...");
	}

}

    配置文件

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

		<context:annotation-config></context:annotation-config>
		<context:component-scan base-package="com.spring.test,com.spring.log"></context:component-scan>
		<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>
    每日一道理
感叹人生,是因为曾经没有过轰轰烈烈的壮举,觉得渺小,觉得平庸,似乎生活过于简单,简单得让人感觉烦躁。没有大言不惭地说过将来,只是比较现实地握住了现在,我想,这是一条路,每个人所必须踏上的一次旅程,曾经看到过这样一句话:成长的过程漫长却充实,自毁的过程短暂却留下一生痛苦,人生可以说是一次考验,何去何从取决于自我。

    要切入的

package com.spring.log;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogInterceptor {
	
	@Before(value="execution (* com.spring.test.*.*(..))")
	public void beforeMethod(JoinPoint point){
		System.out.println("方法执行前执行");
		 System.out.println(point.getTarget()+"类的"+point.getSignature().getName()+"方法执行!!!");  
	}
	@After(value="execution (* com.spring.test.*.*(..))")
	public void afterMethod(JoinPoint point){
		System.out.println("方法执行后执行");
		 System.out.println(point.getTarget()+"类的"+point.getSignature().getName()+"方法执行终了!!!");  
	}

}

    Junit试测

package com.junit.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.spring.test.UserService;


public class Test {
	
	@org.junit.Test
	public void Test(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserService service = (UserService) ctx.getBean("userDao");
		service.createUser();
	}
	

}

    试测结果:

    方法执行前执行
com.spring.test.UserDao@1977b9b类的createUser方法执行!!!
user saved...
方法执行后执行
com.spring.test.UserDao@1977b9b类的createUser方法执行终了!!!

文章结束给大家分享下程序员的一些笑话语录: 大家喝的是啤酒,这时你入座了。
你给自己倒了杯可乐,这叫低配置。
你给自已倒了杯啤酒,这叫标准配置。
你给自己倒了杯茶水,这茶的颜色还跟啤酒一样,这叫木马。
你给自己倒了杯可乐,还滴了几滴醋,不仅颜色跟啤酒一样,而且不冒热气还有泡泡,这叫超级木马。
你的同事给你倒了杯白酒,这叫推荐配置。
菜过三巡,你就不跟他们客气了。
你向对面的人敬酒,这叫p2p。
你向对面的人敬酒,他回敬你,你又再敬他……,这叫tcp。
你向一桌人挨个敬酒,这叫令牌环。
你说只要是兄弟就干了这杯,这叫广播。
有一个人过来向这桌敬酒,你说不行你先过了我这关,这叫防火墙。
你的小弟们过来敬你酒,这叫一对多。
你是boss,所有人过来敬你酒,这叫服务器。
酒是一样的,可是喝酒的人是不同的。
你越喝脸越红,这叫频繁分配释放资源。
你越喝脸越白,这叫资源不释放。
你已经醉了,却说我还能喝,叫做资源额度不足。
你明明能喝,却说我已经醉了,叫做资源保留。
喝酒喝到最后的结果都一样
你突然跑向厕所,这叫捕获异常。
你在厕所吐了,反而觉得状态不错,这叫清空内存。
你在台面上吐了,觉得很惭愧,这叫程序异常。
你在boss面前吐了,觉得很害怕,这叫系统崩溃。
你吐到了boss身上,只能索性晕倒了,这叫硬件休克。

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3060167.html