首先引入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. codeforces 339C Xenia and Weights(dp或暴搜)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Weights Xenia has a set of weig ...

  2. C++程序设计实践指导1.15找出回文数改写要求实现

    改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode ...

  3. maven使用笔记一 下载json-lib引发的问题

    一.问题描述(IDEA中): 1,在pom.xml中配置了 <dependency> <groupId>net.sf.json-lib</groupId> < ...

  4. Android Studio Linking an external C++ project 时候 报Invalid file name. Expected: CMakeLists.txt

    Android Studio 右键Linking an external C++ project 时候 报Invalid file name. Expected: CMakeLists.txt错误 查 ...

  5. ZendFramework 环境部署

    查看源码 int_autoloader.php 文件中,发现应用了一个 AutoloaderFactory 的命名空间,路径写得是相对路径,所以需要在 php.ini 中定义一个 inclde_pat ...

  6. css显示省略号

    white-space:nowrap;overflow:hidden;text-overflow:ellipsis;  <!-- 就这三句,,,嘿嘿....->

  7. 直接地址跳转C实现

    <C缺陷和陷阱>讲过的一种方法: ( *( void (*)() ) 0 )(); //跳转到0地址执行 解析: 1.void (*p_fun)(void); //声明函数指针 2.voi ...

  8. SqlCommand类

    一.常用属性 CommandText 获取或设置要对数据源执行的 Transact-SQL 语句.表名或存储过程. CommandTimeout 获取或设置在终止执行命令的尝试并生成错误之前的等待时间 ...

  9. jQuery 序列化表单数据 serialize() serializeArray()

    1.serialize()方法 格式:var data = $("form").serialize(); 功能:将表单内容序列化成一个字符串. 这样在ajax提交表单数据时,就不用 ...

  10. 2014第一周五开发问题记URL传参乱码等

    今天修改了页面中URL传中文参数乱码问题,本来远离通过在tomcat中配置URIEncoder是可以解决所有乱码问题的,但怕以后有人下载一个新的tomcat然后直接把程序放里面运行然后再发现乱码问题而 ...