一、

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. 实时刷新winform中的某一个控件上的文字

    需要注意的是,必须从UI线程,另外启动一个线程才可以. 在新线程调用异步刷新就OK了 Thread thread; private void button1_Click(object sender, ...

  2. UVa 10534 波浪子序列(快速求LIS)

    https://vjudge.net/problem/UVA-10534 题意:给定一个长度为n的整数序列,求一个最长子序列(不一定连续),使得该序列的长度为2k+1,前k+1个数严格递增,后k+1个 ...

  3. python 输出两个日期之间的天数

    from datetime import date f_date = date(, , ) l_date = date(, , ) delta = l_date - f_date print(delt ...

  4. Postman模拟json传参

    首先在headers中,设置Content-Type为applicationon/json,如图: 然后再body中,选择raw,写入json数据结构,如图:

  5. Android-----------广告图片轮播控件

    Banner广告图片轮播控件,支持无限循环和多种主题,可以灵活设置轮播样式.动画.轮播和切换时间.位置.图片加载框架等! 很多Android APP中都有广告栏,我也用过很多次了,特来写一篇博文. 先 ...

  6. hdu 6144 Arithmetic of Bomb

    Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. translate函数

    translate函数: select translate('ab23cd1', '.0123456789' || 'ab23cd1', '.0123456789') from dual 截图:

  8. STL标准库-容器-vector

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性. 向量容器vector是一个动态数组,内存连续,它是动态分配内存,且每次扩张的原来的二倍. 他的结构如下 一 定义 vector ...

  9. New Concept English Two 17 43

    $课文41  你把那个叫帽子吗? 422. 'Do you call that a hat?' I said to my wife. “你把那个叫帽子吗?”我对妻子说. 423. 'You needn ...

  10. tornado多路由示例

    main.py代码: # encoding: utf-8 """ @version: ?? @author: andu99 @contact: andux@qq.com ...