一。配置服务类组件
@Component
public class UserService {
private UserDAO userDAO;
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
@Resource(name="userDAOImpl")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
@Resource自动装配
二。配置applicationContext.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.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">
<!-- 配置自动装配 AutowiredAnnotationBeanPostProcessor等接口,实现注入等功能 -->
<context:annotation-config/>
<!-- 配置扫描包路径 -->
<context:component-scan base-package="com.job"/>
<!-- 声明一个类 -->
<bean id="LogInteceptor" class="com.job.aop.LogInteceptor"></bean>
<aop:config>
<!-- 声明一个全局切点 ,id为切点id,expression作用到类的什么方法-->
<aop:pointcut id="servicePointcut" expression="execution (public * com.job..*.add(..))"/>
<!-- 声明一个切面,id为切面id,ref表示这个类为切面类 -->
<aop:aspect id="logAspect" ref="LogInteceptor">
<!-- pointcut-ref切点执行,method作用切面类中的方法 -->
<aop:before method="beforeMethod" pointcut-ref="servicePointcut"/>
</aop:aspect>
<!-- 另一种写法,声明一个私有切点-->
<!-- id切面id,ref声明切面类 -->
<aop:aspect id="logaspect2" ref="LogInteceptor">
<!-- method切面类中方法 pointcut作用到类的什么方法 -->
<aop:before method="beforeMethod" pointcut="execution (public * com.job..*.add(..))" />
</aop:aspect>
</aop:config>
</beans>
三。编写切面类
public class LogInteceptor {
public void beforeMethod(){
System.out.println("Method start!");
}
}
四。测试
public class UserServiceTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Service service = (Service)ac.getBean("userService");
User u = new User();
u.setUsername("me haha ");
service.add(u);
}
}
五。测试结果
Method start!
me haha ---haha!
分享到:
相关推荐
基于xml实现动态动态代理 aop 请查看
NULL 博文链接:https://312788172.iteye.com/blog/634921
spring-aop实践项目,基于xml方式的面向切面实践小项目
Spring-Aop 一、Aop的概述 1.1、什么是Aop(摘自百度) 1.2、Aop的相关术语 二、基于XML的AOP配置 三、基于注解的AOP配置
SpringAop xml方式配置通知
所谓连接点是指那些被拦截到的点。在spring中,这些点指的是方法,因为spring只支持方法类型的连接点
使用xml配置方法实现aop
此文档介绍了Spring Aop编程的两种方法: 1、基于XML配置方式进行AOP开发。2、基于注解方式进行AOP开发。 文档中有详细的例子。
采用注解模式,和XML模式对AOP面向切面编程进行测试。
演示了spring对aop的支持,包括注解方式、基于xml方式。
aop入门。
Spring通过XML实现AOP,Spring通过XML实现AOP,Spring通过XML实现AOP
springAOP_XML
Spring主要有两大思想,一个是IoC,另一个就是AOP,对于IoC,AOP的原理就是java的动态代理机制
│ Maven__org_springframework_spring_aop_5_2_5_RELEASE.xml │ Maven__org_springframework_spring_beans_5_2_5_RELEASE.xml │ Maven__org_springframework_spring_context_5_2_5_RELEASE.xml │ Maven__org_...
spring框架使用xml配置文件实现aop
NULL 博文链接:https://ysj5125094.iteye.com/blog/2055563
先将nhibernate-configuration.xsd,nhibernate-mapping.xsd,spring-aop-1.1.xsd,spring-database-1.3.xsd和你能知道所有的xsd格式的spring相关文件放入到C:\Program Files\Microsoft Visual Studio 10.0\Xml\...
基于xml代码的SpringAOP实例详解,适合初步学习springAOP的开发人员了解该功能的使用。