位置: 编程技术 - 正文

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

  • 个人所得税的标准计算
  • 企业所得税怎么算出来
  • 车船税保险公司会不会多收
  • 进项税额转出会影响利润吗
  • 代加工可靠吗
  • 个人与公司交的税怎么算
  • 发票代码和发票号码是什么
  • 用现金暂付职工差旅费3000元
  • 不能税前扣除的费用有哪些
  • 企业开出增值税专用发票后注销
  • 个体户个人所得税怎么交
  • 建筑企业差额征收
  • 园林绿化公司前景如何
  • 待转销项税额如何列报
  • 哪些进项税额不允许从销售额中抵扣?
  • 垫资的利息的合法区间
  • 哪些出口业务不能做
  • 开手续费发票开多了怎么做账
  • 金税盘年费如何做账
  • 子公司和母公司可以一起投标吗
  • 投资收益企业所得税预缴
  • 工资五险一金缴费标准是多少
  • 实发工资和报税工资
  • 如何解决win7系统不稳定
  • 筹建期无形资产土地要摊销吗
  • 单位银行结算账户属于活期存款账户
  • vue数据实时更新
  • 前端打印语句
  • vue 实战
  • php redis常用命令
  • php css教程下载
  • 税费滞纳金计入增值税吗
  • linux mput命令
  • python中time模块的用法
  • 公司租赁个人车辆账务处理
  • 自营与代理
  • python time. time
  • windows 和 linux
  • 单位上社保需要个人提供什么资料
  • 可变现净值属于会计计量属性吗
  • 增值税减免税在贷方
  • sqlserver2008r2怎么使用
  • sql2016异地备份
  • 个人股东和法人股东分红纳税
  • 办公装修费用多少
  • 如果返利冲抵货款怎么办
  • 其它应付款是否可以抵扣
  • 商贸企业增值税税负率低于1%则存在涉税问题
  • 资产减值准备怎么提
  • 利润分配未分配利润是净利润吗
  • 融资方式股权融资
  • 一次性收取一年的租金如何做分录
  • 报销需要发票吗?
  • 会计的凭证怎么做账
  • 会计结转是什么意思
  • 坏账准备是什么类科目
  • 新公司成立建账流程
  • 商业零售企业商品进销差价
  • MySQL使用xtrabackup进行备份还原操作
  • Linux/Mac MySQL忘记密码命令行修改密码的方法
  • 盗版win10可以用win10商店吗
  • 任务管理器打不开怎么强制关闭电脑程序
  • 如何配置sendmail
  • 免安装版怎么用
  • 怎么从win8装回win7
  • opengl环境光
  • nodejs执行cmd命令
  • linux awk命令使用实例
  • 恶意软件清理
  • linux与windows
  • opengl颜色代码表
  • 置顶在哪里
  • CalledFromWrongThreadException: Only the original thread that created a view
  • 表单元素的属性
  • javascript面向对象精要pdf下载
  • Android alertDialog 动态添加edittext无法弹出键盘解决方案
  • 12366纳税服务热线坐席人员
  • 在电脑上怎样做word的文档
  • 南通房屋登记系统
  • 增值税普通发票和电子普通发票的区别
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设