位置: 编程技术 - 正文

Java Spring AOP的两种配置方式(spring中aop实现)

编辑:rootadmin
第一种:注解配置AOP

推荐整理分享Java Spring AOP的两种配置方式(spring中aop实现),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:spring中aop,spring aop实例讲解,spring aop两种实现方式,spring中aop实现,spring,aop,spring aop两种实现方式,spring,aop,spring,aop,内容如对您有帮助,希望把文章链接给更多的朋友!

java中注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:

1. 使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).2. 开发需要被拦截的类。3. 将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AoP容器管理。

另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

实例:

User.javapackage com.bjsxt.model;public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}/***接口类*/package com.bjsxt.dao;import com.bjsxt.model.User;public interface UserDAO { public void save(User user);}复制代码实现接口:package com.bjsxt.dao.impl;import org.springframework.stereotype.Component;import com.bjsxt.dao.UserDAO;import com.bjsxt.model.User;@Component("u")public class UserDAOImpl implements UserDAO { public void save(User user) { System.out.println("user saved!"); /*throw new RuntimeException("exception");*/ //抛异常 }}复制代码操作类:package com.bjsxt.service;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;import com.bjsxt.dao.UserDAO;import com.bjsxt.model.User;@Component("userService")public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } @Resource(name="u") public void setUserDAO( UserDAO userDAO) { this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); }}复制代码加入aoppackage com.bjsxt.aop;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Aspect@Componentpublic class LogInterceptor { @Pointcut("execution(public * com.bjsxt.service..*.add(..))") public void myMethod(){}; /*@Before("execution(public void com.bjsxt.dao.impl.UserDAOImpl.save(com.bjsxt.model.User))")*/ @Before("myMethod()") public void before() { System.out.println("method staet"); } @After("myMethod()") public void after() { System.out.println("method after"); } @AfterReturning("execution(public * com.bjsxt.dao..*.*(..))") public void AfterReturning() { System.out.println("method AfterReturning"); } @AfterThrowing("execution(public * com.bjsxt.dao..*.*(..))") public void AfterThrowing() { System.out.println("method AfterThrowing"); } }复制代码配置文件<?xml version="1.0" encoding="UTF-8"?><beans xmlns=" xmlns:xsi=" xmlns:context=" xmlns:aop=" xsi:schemaLocation=" "><!-- 要添加最后2行 --> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <!-- 自动扫描 --> <aop:aspectj-autoproxy/> <!-- 要添加本行 --></beans>复制代码测试类:package com.bjsxt.service;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.bjsxt.model.User;//Dependency Injection//Inverse of Controlpublic class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService service = (UserService)ctx.getBean("userService"); System.out.println(service.getClass()); service.add(new User()); System.out.println("###"); ctx.destroy(); }}复制代码结果:class com.bjsxt.service.UserService$EnhancerByCGLIB$7bmethod staetuser saved!method AfterReturningmethod after###复制代码Java Spring AOP的两种配置方式(spring中aop实现)

注意:

@Aspect:意思是这个类为切面类@Componet:因为作为切面类需要 Spring 管理起来,所以在初始化时就需要将这个类初始化加入 Spring 的管理;@Befoe:切入点的逻辑(Advice)execution…:切入点语法第二种:xml配置aop

实例同上:只是配置文件不同

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=" xmlns:xsi=" xmlns:context=" xmlns:aop=" xsi:schemaLocation=" "><!-- 要添加最后2行 --> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> <aop:config> <aop:pointcut expression="execution(public * com.bjsxt.service..*.add(..))" id="servicePointcut"/> <aop:aspect id="logAspect" ref="logInterceptor"> <aop:before method="before" pointcut-ref="servicePointcut" /> </aop:aspect> </aop:config></beans>复制代码

下面的<beans>是Spring的配置标签,beans里面几个重要的属性:

xmlns:

是默认的xml文档解析&#;式,即spring的beans。地址是

AndroidのScrollView中嵌套ListView时显示一行解决方法 在ScrollView中嵌套ListView时,ListView只能显示一行多一点.原因在ScrollView中嵌套ListView控件,无法正确的计算ListView的大小,故可以通过代码,根据当前的Lis

Android中的GET和POST请求 packagecom.xuexi.getposttest;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.net.MalformedURLException;importjava.net.URL;im

Android显示WIFI列表功能实现 最近在做Android连接Wifi打印机的功能,无意间看到这个列表功能,比较简单,就实现了一下,没有DEMO。其实主要的就是使用WifiManager这个对象来进行操作

标签: spring中aop实现

本文链接地址:https://www.jiuchutong.com/biancheng/376675.html 转载请保留说明!

上一篇:Android的GPU过度绘制的优化(手机gpu过度绘制)

下一篇:AndroidのScrollView中嵌套ListView时显示一行解决方法(android:scaletype="centercrop")

  • 销售二手车需要什么资质
  • 公司有食品流通证能卖保健品吗
  • 销售费用的进项税额转出会计分录
  • 营业收入管理也是企业财务管理的基本内容
  • 进口商品不提供发票合法吗
  • 收到预付账款的分录
  • 信用贷的基本准入要求
  • 开增值税专票交多少税费
  • 允许列支的捐赠比例
  • 金税四期进展
  • 公司的差旅费
  • 有进项无销项会计分录
  • 应收补贴款贷方余额
  • 购进原材料直接销售应该怎样结转成本呢
  • 企业收入代扣税金应该怎么做会计处理?
  • 设备定金计入哪个科目
  • 没有签订合同需要申报印花税吗
  • 未摊销金额怎么算
  • 个税在发工资的时候直接扣下来吗
  • 企业所得税固定资产折旧计算
  • 政府返还的资金如何处理
  • win10专业版如何激活
  • 政府补贴专项资金使用要求
  • php如何运行脚本
  • intempt
  • php配置文件的名字是
  • php_fileinfo作用
  • 如何分清福利性劳动
  • php100 jquery教程
  • 企业合并支付的资产评估费计入
  • 劳务公司给包工头打款备注写什么
  • 顺流交易合并抵消
  • 电子税务局增值税申报流程
  • 企业其他应付款太多怎么办
  • 视觉slam ba
  • vue路由跳转携带参数怎么接收
  • php使用oci8扩展连接oracle
  • Yii2实现同时搜索多个字段的方法
  • 旅行社差额纳税的规定
  • 应收账款属于哪个会计要素
  • 施工企业的内部往来
  • 中小企业估值
  • 预提工资的会计处理
  • 员工持股平台合伙企业如何设立
  • 小规模纳税人可以做进出口贸易吗
  • 个体工商户怎么开发票
  • 辞退补偿金的标准
  • sqlserver经典书籍
  • 核定征收的企业需要汇算清缴吗
  • 工会经费如何支出
  • 业务招待费包含样品费吗
  • 负数发票跨月怎么作废
  • 企业会计账簿设计的原则
  • win10安装mysql5.6
  • 统计得到的一组数据有80个
  • mysql添加外键约束的sql语句
  • 提高系统能力
  • win10不能显示桌面
  • win7系统计算机名称在哪看
  • mac怎么格式
  • win8 设置
  • win7任务栏变小图标
  • linux小技巧
  • css教程实例
  • es6字符串模版
  • 输入法不跳出中文
  • 使用node.js实现用IP地址查询天气情况
  • 详细说明js的执行过程
  • js函数调用常用字符串
  • JQuery Ajax WebService传递参数的简单实例
  • node用什么写的
  • android 属性动画原理
  • android入门基础知识
  • springmvc接收json需要配
  • 酒类包装物押金的消费税处理规定
  • 加拿大移民知乎
  • 北京地税局上班时间查询
  • 课税对象与征税对象一样吗
  • 房屋设备租赁费
  • 火车票是否出票查询
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设