Spring学习2_AOP通过XML配置简单实现

Spring在实际运用中可通过注解或者XML配置来实现AOP功能,这里在上一篇的基础上通过Demo来模拟XML配置实现AOP的过程。

代码结构如下

1、Spring配置如下,在<aop:config>中配置好对应的切点pointCut, 然后在切面aspect中引用对应的切点即可。

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

   <bean id = "dao" class="com.dao.UserDaoImpl"/> 

   <bean id="userService" class="com.service.UserService">
       <property name="dao" ref="dao"></property>
   </bean> 

   <bean id = "logInter" class="com.handler.LogInterceptor"/>
   <aop:config>

       <aop:pointcut expression="execution(* com.service.UserService.*(..))" id="servicePointCut" />
       <aop:aspect id="LogAop" ref="logInter">
           <aop:before method="beforeExcute" pointcut-ref="servicePointCut"/>
       </aop:aspect>

   </aop:config>

</beans>

2、拦截类LogInterceptor

package com.handler;

public class LogInterceptor {
    public void beforeExcute(){
        System.out.println("aspect execute.");
    }

}

3、在UserService中通过Spring注入Dao层,完成接口调用。

测试类如下:

package com.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.service.UserService;

public class TestAopAnnotation {
    @Test
    public void testAopAnnotation(){

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

        UserService service = (UserService) context.getBean("userService");
        service.save();
    }
}

测试结果:

时间: 2024-08-28 02:48:18

Spring学习2_AOP通过XML配置简单实现的相关文章

spring管理SessionFactory中XML配置

1. 综合练习目标 2. 综合练习需求 3.模块划分 1. 综合练习目标 <1>复习 Java 基本语法 <2>熟悉掌握Java开发常用API <3>尝试建立面向对象思想 2. 综合练习需求 <1>接收用户的命令行输入 <2>以文件为基础完成数据的增删改查操作          3.模块划分 UI模块:(Java存在文本中都是以字符型式) 数据验证模块:验证用户输入是否合法 spring管理SessionFactory中XML配置,布布扣,bub

Spring装配Bean---使用xml配置

声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Beans命名空间,Spring的核心框架总共自带了10个命名空间配置:  命名空间 用途  aop     为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置元素  beans     支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间  context 为配置Sprin

Spring的配置文件ApplicationContext.xml配置头文件解析

Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applicationContext.xml配置头文件解析 <?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans"

Spring MVC的web.xml配置详解(转)

出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(listener-class) ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextListener这个接口,在web.

Spring Aop实例之xml配置

上篇博文<3幅图让你了解Spring AOP>中介绍了aop通知类型,AOP的配置方式有2种方式:xml配置和AspectJ注解方式.今天我们就来实践一下xml配置方式. http://blog.csdn.net/xiaoxian8023/article/details/17258933 我采用的jdk代理,所以首先将接口和实现类代码附上 [java] view plaincopy package com.tgb.aop; public interface UserManager { publ

spring注解机制和XML配置机制之间的比较

XML配置的优缺点: 优点有:1. XML配置方式进一步降低了耦合,使得应用更加容易扩展,即使对配置文件进一步修改也不需要工程进行修改和重新编译.2. 在处理大的业务量的时候,用XML配置应该更加好一些.因为XML更加清晰的表明了各个对象之间的关系,各个业务类之间的调用.同时spring的相关配置也能一目了然.当然,有人会说,用XML配置,在大的业务量时候会使得XML文件过大,不容易查看.这一点我们完全可以利用业务分解书写多个XML配置文件就可以了. 3.xml,在乎:整体业务关系:维护性. 缺

尚硅谷Spring整合Hibernate基于xml配置

描述:这是一个最简单网上书城demo. 下载地址:http://download.csdn.net/detail/u013488580/8370899 1. Spring 整合 Hibernate 整合什么 ? 1). 有 IOC 容器来管理 Hibernate 的 SessionFactory 2). 让 Hibernate 使用上 Spring 的声明式事务 2. 整合步骤: 1). 加入 hibernate ①. jar 包 ②. 添加 hibernate 的配置文件: hibernate

spring mvc 中web.xml配置信息解释

在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出的结论是:listener -> filter -> servlet 同时还存在着这样一种配置节:context-param,它用于向 Servlet

spring事务管理,xml配置aop事务和注解配置aop事务

xml配置和注解配合共同代码 AccountService.java public interface AccountService { //转账方法 void transfer(Integer from,Integer to,Double money); } AccountServiceImpl.java xml配置aop事务的AccountServiceImpl.java public class AccountServiceImpl implements AccountService {