Spring实战4.4 注解切面

目录

Aspect cannot be resolved to a type

注解切面方式实现aop编程

(1) 先引入aop相关的jar文件

(2) 配置spring-idol.xml中引入aop名称空间

(3):配置spring-idol.xml中开启aop注解扫描,如下配置所示:


Aspect cannot be resolved to a type

注解切面方式实现aop编程

开发步骤

(1) 先引入aop相关的jar文件

AspectJ项目地址为:http://www.eclipse.org/aspectj/,打开项目后,点击其中的Download链接,然后选择合适的版本,下载后得到为一个Jar包,例如aspectj-1.8.5.jar。将jar包解开,可以发现其中有lib目录,其中包含aspectjrt.jar、aspectjtools.jar、aspectjweaver.jar和org.aspectj.matcher.jar几个jar包文件,将它们加入到项目的eclipse的build path中,就可以进行面向切面的项目开发了。

Aopalliance包可以到Jboss的软件库:https://repository.jboss.org/nexus/content/repositories/root_repository/aopalliance/进行下载,下载后得到aopalliance包,直接添加到项目的build path中就可以了。其实上面的AspectJ支持包也可以在https://repository.jboss.org中找到

jar文件(aspectj-1.9.2.jar)的百度网盘连接:链接: https://pan.baidu.com/s/1VbCFam-_Of19b4S5qoUmYA 提取码:n6xu 

(2) 配置spring-idol.xml中引入aop名称空间

技巧:找到文件spring-framework-3.2.5.RELEASE/docs/spring-framework-reference/htmlsingle打开index.html搜索xmlns:aop然后找到下面红色三句话,分别拷贝到bean.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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"   
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"   
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
</bean>

(3):配置spring-idol.xml中开启aop注解扫描,如下配置所示:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:p="http://www.springframework.org/schema/p"   
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"   
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- 开启注解扫描 -->
	<context:component-scan base-package = "com.springinaction.springidol"></context:component-scan>
	
	<!-- 开启aop注解方式,默认为false -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</bean>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值