首页 技术 正文
技术 2022年11月22日
0 收藏 428 点赞 4,363 浏览 3810 个字

一、

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:

马士兵Spring-AOP-XML配置(2)

二、第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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845