首先引入jar包

aspectjrt.jar

aspectjweaver.jar

1、dao

package com.dao;

public interface OkpDao {
public void save();
public void update();
}

2、impl

package com.dao.impl;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.dao.OkpDao;
@Component("okpDaoImpl")
public class OkpDaoImpl implements OkpDao{ public void save() {
System.out.println("OkpDaoImpl.save()");
}
public void update() {
System.out.println("OkpDaoImpl.update()");
}
}

3、service

package com.service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import com.dao.OkpDao;
@Component
public class OkpService {
private OkpDao okpDao; public OkpDao getOkpDao() {
return okpDao;
}
@Resource(name="okpDaoImpl")
public void setOkpDao(OkpDao okpDao) {
this.okpDao = okpDao;
}
public void save(){
this.okpDao.save();
}
public void update(){
this.okpDao.update();
}
@PostConstruct
public void init(){ System.out.println("容器创建前执行");
} @PreDestroy
public void destory(){
System.out.println("容器销毁后执行");
}
}

 4、切面类

package com.service;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class SaveInter { @Pointcut("execution(* com.service.OkpService.*(..))")
public void method(){}; @Before("method()")
public void before(){
System.out.println("前置通知");
}
@AfterReturning("method()")
public void doAfter(){
System.out.println("后置通知");
}
@After("method()")
public void after(){
System.out.println("最终通知");
}
}

@Aspect切面类

@Component 将该类加载到spring容器

@Poincut 定义一个切面

@Before 当执行切面中的方法前,会执行

@AfterReturning 方法执行完执行

@After 相当于方法中的try{}catch{}finally{}中的finally,执行完方法前会执行 

 5、测试类

package com.service;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class OkpServiceTest { @Test
public void testSave() {
BeanFactory bf=new ClassPathXmlApplicationContext("applicationContext.xml");
OkpService okp=(OkpService) bf.getBean("okpService");
okp.save();
okp.update();
} }

  6、配置文件

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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/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/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

  使用Aop注解,xml配置文件中要加入<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

运行结果:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
容器创建前执行
前置通知
OkpDaoImpl.save()
最终通知
后置通知
前置通知
OkpDaoImpl.update()
最终通知
后置通知

Spring的注解学习(ioc,aop结合)的更多相关文章

  1. java框架之Spring(2)-注解配置IOC&AOP配置

    注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...

  2. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  3. (转)使用Spring的注解方式实现AOP入门

    http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...

  4. Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  5. 使用Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  6. Spring @Autowired 注解 学习资料

    Spring @Autowired 注解 学习资料 网址 Spring @Autowired 注解 https://wiki.jikexueyuan.com/project/spring/annota ...

  7. spring源码学习之AOP(一)

    继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...

  8. spring(一)IOC & AOP

    参考文档: spring详解:http://www.cnblogs.com/ysocean/p/7466191.html(可以说非常详细了) aop源码详解:https://www.cnblogs.c ...

  9. Spring的入门学习笔记 (AOP概念及操作+AspectJ)

    AOP概念 1.aop:面向切面(方面)编程,扩展功能不通过源代码实现 2.采用横向抽取机制,取代了传统的纵向继承重复代码 AOP原理 假设现有 public class User{ //添加用户方法 ...

随机推荐

  1. 字符串在 UNICODE、MBCS编码下面的区别

    1:SBCS (single byte character set)单字节字符集.在这种编码格式下,所有字符都用一个字节表示.ASCII码就是单字节字符.用“0”来表示一个字节的结束.2 :Unico ...

  2. easyui的combobox将得到的数据设定为下拉框默认值和复选框设定默认值

    通过easyui做了一个表,表里是从数据库拿到的数据. 现在双击某一行,通过点击行的id取到这一行的所有数据,现在需要修改这些得到的数据, 其中部分数据是<select>这个选择的, 问题 ...

  3. juqery合成事件toggle方法

    当指定元素被点击时,在两个或多个函数之间轮流切换. 如果规定了两个以上的函数,则 toggle() 方法将切换所有函数.例如,如果存在三个函数,则第一次点击将调用第一个函数,第二次点击调用第二个函数, ...

  4. win7 tomcat

    前提需要有java环境 cmd 1- 下载tomcat http://tomcat.apache.org/ download Tomcat7.0 2- 配置环境变量 CATALINA_HOME C:\ ...

  5. shell脚本练习(短路练习)

    #!/bin/bash #By Spinestars#2013-11-11#This is a lvsnap of auto-create Help(){ echo "Usage: ---d ...

  6. 超大文件上传到Azure Linux虚拟机最佳实践

    客户在实际进行迁移的时候,往往碰到需要将本地数据中心的超大文件,比如单个200GB的文件,或者总共1TB的无数文件上传到Azure上的情况,尤其是传到Azure的Linux虚拟机的场景,这种场景包括: ...

  7. IOS开发UI篇-NavigationController的基本使用

    一.简介 一个iOS的app很少只由一个控制器组成,除非这个app极其简单 当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小v ...

  8. typedef函数指针那些事

    首先来介绍下函数指针: 函数指针是指向函数的指针变量,即本质是一个指针变量. int (*f) (int x); /* 声明一个函数指针 */ f=func; /* 将func函数的首地址赋给指针f ...

  9. Clojure操作mysql

    在Eclipse中新建一个Clojure工程clj01 clojure 操作mysql需要依赖mysql-connector-java.clojure-contrib与java.jdbc三个jar包. ...

  10. DOS命令行 定时关机&取消定时关机

      命令行关机命令----shutdown   Windows XP的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中.   如果你输入"shutd ...