本文用于知识点的简单总结。
对于Spring中AOP的配置,一种方式是基于Schema,也就是在xml文件中进行配置,相关配置项在aop命名空间下定义。以下主要通过代码说明基本配置流程。
(1)定义需要实现的业务接口:
package com.aop.service;
public interface MyService {
public void sayHello(String word);
}
(2)编写实现接口的业务类:
package com.aop.service;
public class MyServiceImpl implements MyService {
@Override
public void sayHello(String word) {
System.out.println("Say :" + word);
}
}
(3)定义切面支持类,包含所需的通知方法:
package com.aop.aspect;
import org.aspectj.lang.JoinPoint;
public class MyAspect {
//前置通知
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("--before advice,参数:" + joinPoint.getArgs()[0]);
}
//后置最终通知
public void afterFinallyAdvice() {
System.out.println("------after finally advice");
}
}
(4)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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 接口实现业务类 -->
<bean id="MyService" class="com.aop.service.MyServiceImpl" />
<!-- 切面支持类,包含所需的通知方法 -->
<bean id="aspect" class="com.aop.aspect.MyAspect" />
<aop:config>
<!-- 切点配置,匹配com.aop.service包及子包下的任何方法执行 -->
<aop:pointcut id="pointcut" expression="execution(* com.aop.service..*.*(..))" />
<!-- 切面配置, ref 属性用来引用切面支持类 -->
<aop:aspect ref="aspect">
<!-- 前置通知,pointcut-ref 引用切点配置bean,method指定通知方法 -->
<aop:before pointcut-ref="pointcut" method="beforeAdvice" />
<aop:after pointcut-ref="pointcut" method="afterFinallyAdvice" />
</aop:aspect>
</aop:config>
</beans>
(5)测试类:
package com.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.aop.service.MyService;
public class TestAop {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("aopBeans.xml");
MyService service = (MyService) context.getBean("MyService");
service.sayHello("Spring aop");
}
}
(6)输出结果:
--before advice,参数:Spring aop
Say :Spring aop
------after finally advice
分享到:
相关推荐
Spring AOP之基于Schema配置总结与案例 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45242505
NULL 博文链接:https://quicker.iteye.com/blog/673889
@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....
<?xml version="1.0" encoding="UTF-8"?><beansxmlns=...aop-2.5.xsd">
第三种实现方法—通过注解... http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="userService" class="cn.sxt.service.impl.UserServiceImpl"></bean> <aop:aspectj-autoproxy/> </beans>
NULL 博文链接:https://snowolf.iteye.com/blog/236264
在使用Spring,写核心配置文件的时候都引入了不一样的约束,这里总结关于spring的XML约束+Schema约束。以后不管是仅使用ioc功能还是aop,把全部的约束引入即可。至于说如何找到这些约束的代码的过程,也在这个文档...
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop] Offending resource: class path resource [beans.xml] at org....
Spring2.5和Hibernate3集成 采用声明式事务 1.声明式事务的配置 * 配置sessionFactory ... 2.http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
NULL 博文链接:https://bijian1013.iteye.com/blog/2136297
Spring AOP(面向切面编程)是Java高级编程中的重要...Spring AOP的配置方式多样,包括基于接口的配置、schema-based配置和@AspectJ注解配置。通过这些配置方式,开发者可以灵活地实现AOP功能,满足不同场景下的需求。
spring声明式事务的配置 3. spring2.0配置事务 a) 将spring 1.2升级到spring2.0 i. 去掉spring1.2相关的包 ii. 添加spring2.0的jar包:spring.jar,aspecjrt.jar,aspectjweaver.jar 和cglib-nodep-2.1.3,jar iii. ...
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 配置视图...
主要介绍了Spring AOP的实现原理详解及实例的相关资料,需要的朋友可以参考下
Spring AOP 基于注解详解及实例代码 1.启用spring对@AspectJ注解的支持: <beans xmlns:aop=http://www.springframework.org/schema/aop...> <!--启动支持--> <aop> 也可以配置...
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> ============================================= 解决整合问题: 1、Spring 3.x 对 Hibernate 4.x 不提供 HibernateDaoSupport,所以在dao的实现...
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
Spring AOP:以loC为基础讲解Spring下的AOP开发,讲解了3种AOP的开发方式,即使用@Aspect注释符、基于Schema的配置的开发方式和Spring API的开发方式,最后在实际的Java EE项目中实现5种Spring AOP功能(日志记录器...
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 开启classpath扫描 --> <context:component-scan base-package="com.haijian" />