spring零配置AOP踩坑指南
今天照着书,试着配了AOP(全注解),结果踩了各种坑,后来参考书附带的源码,终于走出来了,现在总结一下
- 除了spring的jar包以外,还需要导入以下包:

1、Spring核心配置文件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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- 启动@AspectJ支持 -->
<!-- <bean
class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" /> -->
<context:component-scan
base-package="com.sysker.bean,com.sysker.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
<!-- 设置AOP为自动代理 -->
<aop:aspectj-autoproxy />
</beans>
如果全部采用注解的话,bean是不需要在这里配置的,只需要在对应的类上面写上@Component("name")注解即可,前面不清楚,所以我在这个坑里呆了很久,书上也没说,自己也没去查
这里的另外一个坑就是这里:
<context:component-scan
base-package="com.sysker.bean,com.sysker.aspect">
<context:include-filter type="annotation"
expression="org.aspectj.lang.annotation.Aspect" />
</context:component-scan>
- 如果这里路径配置有问题的话,会提示找不到bean,错误信息大概如下:
Loading XML bean definitions from class path resource [beans.xml]
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'hello' availa
ble
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.ja
va:686)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:
1210)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1095)
at com.sysker.test.AspectJTest.main(AspectJTest.java:22)
- base-package里面可以配置多个路径,一定要完整
2、 Aspect类(这里有另外的大坑)
package com.sysker.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AuthAspect {
@Before("execution(* com.sysker.impl.*.*(..))")
public void authority() {
System.out.println("模拟执行权限检查");
}
}
- 这里就有另外一个大坑在等着你,主要会出错的地方还是路径的问题:
@Before("execution(* com.sysker.impl.*.*(..))")
- 路径必须正确,否则是没有效果了,比如我的路径,impl下的实现类要实现AOP,那么我的路径是要配到impl,然后还要有.. ,不然就是各种无效果,而且还不报错;
- 还要提一下,Aspect必须要注解,注解,注解,就这样的@Aspect,然后后面的@Before("execution(* com.sysker.impl..(..))"),就是要执行的操作
- 如果要针对某一个类,比如HelloImpl类,这需要写出具体的类:
@Before("execution(* com.sysker.impl.HelloImpl.*(..))")
- 如果要针对某一个的具体方法,就需要写出具体的类的方法:
@Before("execution(* com.sysker.impl.HelloImpl.foo(..))")
3、service及Impl
package com.sysker.bean;
public interface Hello {
void foo();
int addUser(String name, String pass);
}
package com.sysker.bean;
public interface World {
public void bar();
}
- service是不要注解的,也没必要
package com.sysker.impl;
import org.springframework.stereotype.Component;
import com.sysker.bean.Hello;
@Component("hello")
public class HelloImpl implements Hello {
@Override
public void foo() {
System.out.println("执行Hello组件的foo()方法");
}
@Override
public int addUser(String name, String pass) {
System.out.println("执行Hello组件的addUser添加用户:" + name);
return 20;
}
}
package com.sysker.impl;
import org.springframework.stereotype.Component;
import com.sysker.bean.World;
@Component("world")
public class WorldImpl implements World {
@Override
public void bar() {
System.out.println("执行World组件的bar()方法");
}
}
- 这里需要注意的就是上面提到的,要注解@Component("world"),后面通过Spring容器创建对象要用到
4、测试类
package com.sysker.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sysker.bean.Hello;
import com.sysker.bean.World;
import com.sysker.impl.HelloImpl;
import com.sysker.impl.WorldImpl;
public class AspectJTest {
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
Hello hello = ctx.getBean("hello" , Hello.class);
hello.foo();
hello.addUser("孙悟空" , "7788");
World world = ctx.getBean("world" , World.class);
world.bar();
}
}
正常运行结果:

总结
今天虽然在这个问题上快纠结了一天,但总算功夫没白费,终于把这个坑填平了,收获满满,继续加油,不说了,我要继续去写bug了!!!
spring零配置AOP踩坑指南的更多相关文章
- Spring WebSocket踩坑指南
Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...
- C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式
C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...
- SpringBoot整合log4j2进行日志配置及防坑指南
写在前面 最近项目经理要求将原先项目中的日志配置logBack,修改为log4j2,据说是log4j2性能更优于logback,具体快多少,网上有说快10多倍,看来还是很快的,于是新的一波挑战又开始了 ...
- Nuxt.js的踩坑指南(常见问题汇总)
本文会不定期更新在nuxt.js中遇到的问题进行汇总.转发请注明出处,尊重作者,谢谢! 强烈推荐作者文档版踩坑指南,点击跳转踩坑指南 在Nuxt的官方文档中,中文文档和英文文档都存在着不小的差异. 1 ...
- Java 热更新 Groovy 实践及踩坑指南
Groovy 是什么? Apache的Groovy是Java平台上设计的面向对象编程语言.这门动态语言拥有类似Python.Ruby和Smalltalk中的一些特性,可以作为Java平台的脚本语言使用 ...
- 树莓派4B踩坑指南 - (15)搭建在线python IDE
今天想在树莓派上自己搭一个在线的python IDE,于是找到了一篇教程--Fred913大神的从头开始制作OJ-在线IDE的搭建 自己尝试动手做了一下, 还是发现不少细节需要注意, 记录在此 如果不 ...
- 正则表达式 test 踩坑指南
正则表达式 test 踩坑指南 test 只能使用一次,第二次返回的是错误结果! reg = /edg|edge/g; /edg|edge/g reg.test(`edg`) true reg.tes ...
- Taro 开发踩坑指南 (小程序,H5, RN)
Taro 开发踩坑指南 (小程序,H5, RN) css taro 如何展示多行文本省略号 https://www.cnblogs.com/xgqfrms/p/12569057.html UI 设计稿 ...
- 小程序 & taro 踩坑指南
小程序 & taro 踩坑指南 微信开发者工具, 不支持 react bug https://github.com/NervJS/taro/issues/5042 solution just ...
随机推荐
- DBGrid1
A.ShowMessage(IntToStr(Column.Index)); B.ShowMessage(IntToStr(dbgrid1.SelectedField.Index)); .得到当前 ...
- DbHelperSQL 事务写法!
try { DBUtility.CommandInfo dbcom = new DBUtility.CommandInfo(); List<DBUtility.CommandInfo> s ...
- python遍历并获取对象属性--dir(),__dict__,getattr,setattr
一.遍历对象的属性: 1.dir(obj) :返回对象的所以属性名称字符串列表(包括属性和方法). for attr in dir(obj): print(attr) 2.obj.__dict__:返 ...
- 关于com组件中idl文件的理解
IDL文件: IDL文件主要定义两大类内容:一是定义接口:二是定义类型库. 定义接口的关键字是interface.每个接口定义前面方括号里面的内容是该接口的属性,最重要的是uuid的定义.该部分经过M ...
- torch7 安装中Missing dependencies for nn:moses >= 1错误解决办法
Torch7.0安装步骤(默认安装路径是在home下): git clone https://github.com/torch/distro.git ~/torch --recursive cd ~/ ...
- 串行总线 —— I2C、UART、SPI
I2C,也叫 IIC,是一种常见的串行总线,它只需要两根线即可在连接于总线上的器件之间传送信息. 0. 电气知识 开漏输出:Open drain output,不输出电压,低电平时接地,高电平时不接地 ...
- python suds 调用webservice 缓存
在linux系统中 如果webservice更新了字段 suds调用有可能缓存以前的字段或方法,对新的字段报找不到类型 TypeNotFound,或者对 新加的方法找不到该方法的错误. 当更新或添加w ...
- ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)
Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...
- GCC生成动态库
main.c #include <stdio.h> void hello(void); int main(int argc, char ** argv) { printf("Th ...
- INT 21H 指令说明及使用方法
很多初学汇编语言的同学可能会对INT 21H这条指令感到困惑,不知道是什么意思,下面就以一段简单的程序为大家讲解: 例如:需要键盘输入,并且回显. AH的值需要查表取得,表在下面 指令: M ...