首先引入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. JDBC学习入门

    一.JDBC相关概念介绍 1.1.数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...

  2. mac上访问samba服务器

    打开safari输入smb://ip,回车后出现输入用户名密码对话框,若是匿名则选择作为“客人”选项 例子 smb://192.168.2.3

  3. gulp压缩js

    1.安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 2.查看nodejs的版本号 3.npm ...

  4. 如何让EcStore和微博同步来推广网站

    EcStore是创建B2C商城的首选PHP系统,它功能强大.操作方便,安装后马上就能建立起一个自己的B2C商城,但建好后如何推广运营商城却不是件容易的事. 新浪微博用户数量大.传播速度快,互联网上拥有 ...

  5. CI框架微信开发-自定义菜单

    在CI框架下面实现了自定义菜单功能.写了一个model,一个类库.顺便附带access_token的实现方式 <?php class Makemenu{ public $menustr; pub ...

  6. DIRECTORY_SEPARATOR的作用

    IRECTORY_SEPARATOR是php的内部常量,用于显示系统分隔符的命令,不需要任何定义与包含即可直接使用. 在windows下路径分隔符是/(当然/在部分系统上也是可以正常运行的),在lin ...

  7. SQL Server 查看空间使用情况的 5 种方法

    解决方法: 方法 1.sp_spaceused 方法 2.dbcc sqlperf 方法 3.dbcc showfilestats 方法 4.dbcc showcontig 方法 5.sys.dm_d ...

  8. Typecho 代码阅读笔记(一) - 页面渲染及路由机制

    转载请注明出处:http://blog.csdn.net/jh_zzz 从 index.php 开始看, /** 初始化组件 */ Typecho_Widget:: widget('Widget_In ...

  9. Xcopy参数介绍

    DOS批处理命令,永远是不朽的命令,不仅功能强大,同时,速度也是最快的!但是,很多新手学习计算机,都已经遗忘了本不该忘记的批处理命令. 我们不可数典忘祖,该学习的还是要学习,不该忘记的还是不能忘记,尤 ...

  10. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...