1.在xml中需要配置自动代理


/**
*
*/
package com.junge.demo.spring.dao; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.junge.demo.spring.advice.MyLogImpl; /**
* @author Administrator
*
*/
@Configuration
@ComponentScan(basePackageClasses= {MyLogImpl.class,UserDao.class})
@EnableAspectJAutoProxy
public class DaoConfig { }

2.java类需要添加@Aspect注解。

package com.junge.demo.spring.advice;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSONObject; @Component("myLog")
@Aspect
public class MyLogImpl { @Pointcut("execution(* com.junge.demo.spring.dao..*(..))")
public void pointcut() { } @Before("pointcut()")
public void beforeFunc() {
System.out.println("beforeFunc ..."); } @After("pointcut()")
public void afterFunc() {
System.out.println("afterFunc ..."); } @AfterReturning("pointcut()")
public void returnFunc() {
System.out.println("returnFunc ..."); } @AfterThrowing(pointcut="pointcut()", throwing="e")
public void throwExpFunc(Exception e) {
System.out.println("throwExpFunc ...");
System.out.println(e.getMessage()); } @Around("pointcut()")
public void aroundFunc(ProceedingJoinPoint point) {
System.out.println("arount before invoke ..."); if (null != point.getArgs() && point.getArgs().length > 0) {
for (Object arg : point.getArgs()) {
System.out.println("around args:" + JSONObject.toJSON(arg));
}
}
System.out.println(point.getTarget());
System.out.println(point.getThis());
System.out.println(point.getKind());
System.err.println(point.getClass());
System.out.println(point.getSignature()); try {
Object result = point.proceed();
System.out.println("around result:" + JSONObject.toJSONString(result));
System.out.println("around after return ...");
} catch (Throwable e) {
System.out.println("around after throws ...");
} System.out.println("around after ..."); } }

3.运行结果

/**
*
*/
package com.junge.demo.spring; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.junge.demo.spring.dao.DaoConfig;
import com.junge.demo.spring.dao.UserDao;
import com.junge.demo.spring.dao.impl.cc.dd.ff.ee.SubClassTest;
import com.junge.demo.spring.service.IPerface;
import com.junge.demo.spring.service.ServiceConfig; /**
* @author Administrator
*
*/
public class ServiceConfigApp { private static ApplicationContext applicationContext; /**
* @param args
*/
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(ServiceConfig.class, DaoConfig.class);
// System.out.println(applicationContext.getBeansOfType(IPerface.class));
// applicationContext.getBean(UserDao.class); applicationContext.getBean(SubClassTest.class).addTest(3, 1);
} }

Spring 使用javaconfig配置aop的更多相关文章

  1. Spring 使用javaconfig配置

    除了使用xml,spring提供javaconfig配置,下面是简单的例子: 1.声明接口 /** * */ package com.junge.demo.spring.service; /** * ...

  2. Spring 使用xml配置aop

    1.xml文件需要引入aop命名空间 2.xml内容: <?xml version="1.0" encoding="UTF-8"?> <bea ...

  3. Spring、XML配置AOP

    新建一个AOP类: public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 & ...

  4. Spring实战(十一) 在Spring XML中配置AOP

    如果你没有源码,不能为通知类添加注解,又不想将AspectJ注解放入到你的代码中,必须选择XML配置了. 1.Spring XML配置文件 解析参考:http://www.cnblogs.com/bi ...

  5. Spring基于注解配置AOP

    D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\aop.xml <?xml version="1.0" encoding ...

  6. Spring基于XML配置AOP

    目录结构: D:\Java\IdeaProjects\JavaProj\SpringHelloWorld\src\cn\edu\bjut\service\StudentService.java pac ...

  7. spring mvc:练习:javaConfig配置和注解

    Spring4 MVC HelloWorld 注释/JavaConfig为示例,一步一步以简单的方式学习Spring4 MVC 的注解,项目设置,代码,部署和运行. 我们已经使用XML配置开发了一个H ...

  8. Spring学习记录(十三)---基于xml文件配置AOP

    上一篇讲了用注解配置AOP,现在讲用xml怎么配置AOP 其实逻辑是一样的,只是用xml的方法,要把这种逻辑写出来,告诉spring框架去执行. 例子:这里的例子和上一篇的例子一样.换成xml方式 / ...

  9. Spring配置AOP实现定义切入点和织入增强

    XML里的id=””记得全小写 经过AOP的配置后,可以切入日志功能.访问切入.事务管理.性能监测等功能. 首先实现这个织入增强需要的jar包,除了常用的 com.springsource.org.a ...

随机推荐

  1. IntelliJ IDEA 导入多个maven项目

    IntelliJ IDEA的功能十分强大  我们日常开发项目会分多个maven项目 idea单个打开切换很是麻烦 下边是idea可以同时导入多个项目的方法 1.选择 FIle -> NEW -& ...

  2. phpexcel导入导出

    先下载类文件,将类文件放到lib下,然后到入口文件定义一个新的的路径,以便引入文件.    <?php namespace app\index\controller; use think\Con ...

  3. json 异常

    com.google.gson.JsonSyntaxException: 1530842820000 1530842820000 是服务器直接返回的Date值由 Gson 解析后出来的值. 后台发出: ...

  4. web.xml hello1代码分析

    在“Web页”节点下,展开WEB-INF节点,然后双击web.xml文件进行查看. 上下文参数提供Web应用程序所需的配置信息.应用程序可以定义自己的上下文参数.此外,JavaServer Faces ...

  5. cdnbest里如何查看网站是否被缓存

    比如开启了强制缓存,如何查看缓存是否生效 本例以firefox浏览器查看,先打开浏览器,按下F12, 然后在浏览器是输入网址访问 如下图响应头里的 x-cache显示 Miss  from 就是没有缓 ...

  6. Node2.js

    Node.js简单爬虫的爬取,也是跟着慕课网上抄的,网站有一点点改动,粘上来好复习嘛 var http = require('http') var cheerio = require('cheerio ...

  7. php面试题五之nginx如何调用php和php-fpm的作用和工作原理

    nginx如何调用php 采用nginx+php作为webserver的架构模式,在现如今运用相当广泛.然而第一步需要实现的是如何让nginx正确的调用php.由于nginx调用php并不是如同调用一 ...

  8. [leetcode]38. Count and Say数数

    The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...

  9. 获取CNVD的cookie

    def getCookie(self): #获取cookie spiderFirefox = webdriver.Firefox() spiderFirefox.get("http://ww ...

  10. centos 搭建git 服务器

    安装 git yum -y install git 添加git 用户 adduser git 切换到git 用户 su git 在git用户家目录下创建  .ssh文件夹 mkdir .ssh 修改文 ...