位置: 编程技术 - 正文

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")

  • 跨区域涉税事项报验
  • 工会经费应该怎么做账
  • 企业上缴税费总额怎么算
  • 视同销售的八种情况
  • 企业所得税税率有几个档次
  • 所得税汇算清缴表在哪里打印
  • 零售不要发票如何报税
  • 外汇资本金入账要求
  • 建安企业增值税预缴
  • 销售退回 账务处理
  • 高新研发费的账务处理怎么做?
  • 物业费能开公司名称吗
  • 国税局网上申报
  • 餐饮装修费用计入什么科目
  • 没有产权的房子可以公证吗
  • 建筑行业暂估成本的账务处理
  • 建筑行业没有库存要做暂估成本怎么做?
  • 委托加工存货要交什么税
  • 增加间接费用科目的方法
  • 稽查补交的税款怎么处理
  • regsync.exe - regsync是什么进程 有什么用
  • 利息支出属于生产成本吗
  • 安装prophet
  • Yii使用技巧大汇总
  • 票据贴现业务如何核算
  • 最小全画幅的数值是多少
  • 补提坏账准备为什么不调整应纳税
  • thinkphp5上传图片
  • web渗透实战
  • 库存股属于什么会计科目
  • Yii2使用swiftmailer发送邮件的方法
  • 前端框架源码
  • 软件和信息技术服务业发展趋势
  • 我一定要用自己的双手拼出来
  • 前端get请求传数组
  • elinks --dump
  • 职工福利费开支超过准予扣除标准的金额为1.2
  • wordpress建立数据库错误
  • 外贸进出口企业注册资金
  • 工会保障工作的主要任务有哪些
  • 航天信息服务费的会计分录
  • MongoDB db.serverStatus()输出内容中文注释
  • 计提怎么理解
  • 购买方已抵扣怎么做分录
  • 小规模纳税人利润率一般是多少
  • 押金和租金
  • 当月进项票没有开进来
  • 无形资产账面价值计算公式
  • 增值税代扣代缴范围
  • 利润表里公允价值变动损益怎么算
  • 小规模企业所得税优惠政策最新2023
  • sum(case when then)(判断男女生的个数)
  • sql储存过程
  • centos7rpm安装命令
  • win10注册表主键
  • 如何删除windows.old
  • 惠普台式笔记本一体机电脑bios中文
  • centos 安装
  • 在windows xp
  • centosgui
  • 苹果Mac系统怎么恢复出厂系统
  • linux xim
  • window八
  • win10系统谷歌浏览器为什么打不开
  • linux ifconfig命令详解
  • windows万能wifi
  • windows alt r
  • Win10 Build 14267截图欣赏:贴心功能大展示
  • android通知消息
  • python 管理内存
  • js随机生成数
  • jquery购物车商品价格计算
  • javascript学习指南
  • 广东省税务局官网 国家税务总局
  • 个体税务申报时间怎么填
  • 湖北省电子税务局操作指南
  • 北京市朝阳区地图
  • 南京地税局上班时间
  • 云南国税申报
  • 国税工作人员工资标准
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设