一、

UserDAO.java:

package com.cy.dao;

import com.cy.model.User;

public interface UserDAO {
public void save(User user);
}

实现UserDAOImpl.java:

package com.cy.dao.impl;

import org.springframework.stereotype.Component;

import com.cy.dao.UserDAO;
import com.cy.model.User; @Component
public class UserDAOImpl implements UserDAO { public void save(User user) {
//Hibernate
//JDBC
//XML
//NetWork
System.out.println("user saved!");
} }

切面类LogInterceptor.java:

package com.cy.aop;

public class LogInterceptor {

    public void before() {
System.out.println("method before");
} }

UserService.java:

package com.cy.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cy.dao.UserDAO;
import com.cy.model.User; @Component("userService")
public class UserService { @Resource
private UserDAO userDAO; public void init() {
System.out.println("init");
} public void add(User user) {
userDAO.save(user);
} public UserDAO getUserDAO() {
return userDAO;
} public void setUserDAO( UserDAO userDAO) {
this.userDAO = userDAO;
} public void destroy() {
System.out.println("destroy");
}
}

配置文件beans.xml:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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"> <context:annotation-config />
<context:component-scan base-package="com.cy"/> <!-- 把切面类在容器中初始化
这是一个切面逻辑类的对象
-->
<bean id="logInteceptor" class="com.cy.aop.LogInterceptor"></bean> <!--
aop:config aop配置
aop:pointcut: 声明一个切面,在service的add方法上面加入我们切进来的各种逻辑;给这个切面起名为servicePointcut
在aop:config下面定义了一个全局的pointCut,可以在所有的aop:aspect上面使用;
(可以添加这种全局的pointCut,也可以在aop:aspect下面添加pointCut,后者只能在这个aspect下面使用) aop:aspect: 声明切面对象;它所参考的切面类的对象是logInteceptor
aop:before: 在切面servicePointcut之前执行logInteceptor的before方法
-->
<aop:config>
<aop:pointcut id="servicePointcut" expression="execution(public * com.cy.service..*.add(..))"/>
<aop:aspect id="logAspect" ref="logInteceptor">
<aop:before method="before" pointcut-ref="servicePointcut"/>
</aop:aspect>
</aop:config> </beans>

测试代码:

UserServiceTest.java:

package com.cy.service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cy.model.User; public class UserServiceTest { @Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
UserService service = (UserService)ctx.getBean("userService");
System.out.println(service.getClass());
service.add(new User());
ctx.destroy();
}
}

console:

二、第2个例子:

pointcut可以直接定义在aop:before/aop:after等这些advice里面:

beans.xml:

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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"> <context:annotation-config />
<context:component-scan base-package="com.cy"/> <bean id="logInteceptor" class="com.cy.aop.LogInterceptor"></bean>
<aop:config>
<aop:aspect id="logAspect" ref="logInteceptor">
<aop:before method="before" pointcut="execution(public * com.cy.service..*.add(..))"/>
</aop:aspect>
</aop:config> </beans>

console同样的运行效果;

三、aop:advisor这个放在声明式事务管理中写;

总结:

马士兵Spring-AOP-XML配置(2)的更多相关文章

  1. spring 5.x 系列第3篇 —— spring AOP (xml配置方式)

    文章目录 一.说明 1.1 项目结构说明 1.2 依赖说明 二.spring aop 2.1 创建待切入接口及其实现类 2.2 创建自定义切面类 2.3 配置切面 2.4 测试切面 附: 关于切面表达 ...

  2. Spring AOP Xml配置过程及解释

    目录 Spring AOP(基于xml) 专业术语: 基于xml的声明式AspectJ 具体实践 Spring AOP(基于xml) 目前主流的AOP框架有两个,分别是Spring AOP和Aspec ...

  3. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  4. spring+mybaits xml配置解析----转

    一.项目中spring+mybaits xml配置解析 一般我们会在datasource.xml中进行如下配置,但是其中每个配置项原理和用途是什么,并不是那么清楚,如果不清楚的话,在使用时候就很有可能 ...

  5. Spring AOP 不同配置方式产生的冲突问题

    Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...

  6. spring AOP为什么配置了没有效果?

     spring Aop的配置一定要配置在springmvc配置文件中         springMVC.xml 1 <!-- AOP 注解方式 :定义Aspect --> <!-- ...

  7. spring的xml配置声明以及相应的问题处理

    spring的xml配置声明:  xml配置声明 Code 问题处理 问题1 xml报错: cvc-elt.1: Cannot find the declaration of element 'bea ...

  8. spring中用xml配置构造注入的心得

    spring中用xml配置构造注入时,如果 <constructor-arg> 属性都是 ref ,则不用理会参数顺序 <constructor-arg ref="kill ...

  9. 基于XML配置的spring aop增强配置和使用

    在我的另一篇文章中(http://www.cnblogs.com/anivia/p/5687346.html),通过一个例子介绍了基于注解配置spring增强的方式,那么这篇文章,只是简单的说明,如何 ...

  10. Spring 使用AOP——xml配置

    目录 AOP介绍 Spring进行2种实现AOP的方式 导入jar包 基于schema-based方式实现AOP 创建前置通知 创建后置通知 修改Spring配置文件 基于schema-based方式 ...

随机推荐

  1. [笔记] SQL性能优化 - 常用语句(一)

    第一步 DBCC DROPCLEANBUFFERS 清除缓冲区 DBCC FREEPROCCACHE 删除计划高速缓存中的元素 从缓冲池中删除所有清除缓冲区.要求具有 sysadmin 固定服务器角色 ...

  2. cocos2d-js入门二 环境搭建二

    电脑升级到WIN10后,cocos  code IDE安装不成功. 于是搭建新的环境JetBrainsWebStorm+chrome+JetBrains IDE Support,其中JetBrains ...

  3. VS2012 QT程序打包部署详解

    1.设置安装程序集 InstallShield安装完成后,执行以下步骤右键解决方案-->添加-->新建项目-->其他项目类型-->安装和部署,具体操作如下图: 2.发布程序 在 ...

  4. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Flyweight(享元)

    意图: 运用共享技术有效地支持大量细粒度的对象. 适用性: 一个应用程序使用了大量的对象. 完全由于使用大量的对象,造成很大的存储开销. 对象的大多数状态都可变为外部状态. 如果删除对象的外部状态,那 ...

  6. 【测试设计】基于正交法的测试用例设计工具--PICT

    前言 我们都知道成对组合覆盖是一种非常有效的测试用例设计方法,但是实际工作过程中当成对组合量太大,我们往往很难做到有效的用例覆盖. PICT是微软公司出品的一款成对组合命令行生成工具,它很好的解决了上 ...

  7. bzoj1054: [HAOI2008]移动玩具 状压+爆搜即可

    题意:在一个4*4的方框内摆放了若干个相同的玩具,某人想将这些玩具重新摆放成为他心中理想的状态,规定移动时只能将玩具向上下左右四个方向移动,并且移动的位置不能有玩具,请你用最少的移动次数将初的玩具状态 ...

  8. VS2019/VS2017安装源离线下载,更新,清理,企业版与论坛版重复下载

    VS2019 安装器下载 https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel ...

  9. BZOJ2620 [Usaco2012 Mar]Haybale Restacking

    恩,非常好的题...至少思路非常巧妙 首先可以得到性质:对于相邻的两堆A & B,A给B然后B再给A是完全没有意义的...也就是说只能单向传递 然后我们记下每个点要给(被给)多少堆干草a[i] ...

  10. 高可用数据采集平台(如何玩转3门语言php+.net+aauto)

    同类文章:高并发数据采集的架构应用(Redis的应用) 吐槽下:本人主程是PHP,团队里面也没有精通.net的人才,为了解决这个平台方案,还是费了一部分劲. 新年了,希望有个新的开始.技术+团队管理都 ...