spring AOP 可以切引入的jar包当中的函数吗?

柳叁 2017-01-04 04:54:57
spring AOP 可以切引入的jar包当中的函数吗?
如果可以的话,需要进行什么配置吗?还是有什么特殊要求么?

在用IDEA 使用maven管理项目。 AOP是使用注解的形式来做的。
...全文
1212 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
解开者 2017-01-12
  • 打赏
  • 举报
回复
引用 10 楼 asalka 的回复:
[quote=引用 9 楼 windowsoahil 的回复:] springaop有个大前提,被增强对象必须是ioc容器的bean 所以,可以增强不是自己写的类,但得把它配置为bean springaop分为传统的MethodInterceptor和后来的AspectJ,都有这个限制 基本上就是(AspectJ方式): 1、把被增强类配一个对象作为bean; 2、@Aspect,写切点表达式和增强逻辑,注意它也得配置成bean; 3、如果你用Intellij,它本身就能动态提示出你的aop写对了没有
嗯。。。。。不用那种方式了费劲不讨好。请问一个其他问题,就是我的AOP切不到Controller,能切到Service 这是我的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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    <!-- 注解开启 -->
    <context:component-scan base-package="com.abcd.aa.device.admin,com.abcd.aa.device.core">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <!-- aop 开启 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 引入其他Spring配置 -->
    <import resource="spring-mybatis-skygBaseDevice.xml"/>

    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!-- 这里定义freemarker.properties文件的加载 -->
    <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:freemarker.properties"/>
    </bean>

    <!-- 配置Freemarker -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/view/"/>
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape"/>
            </map>
        </property>
        <property name="freemarkerSettings" ref="freemarkerConfiguration"/>
    </bean>
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>

    <!-- freemarker页面解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="true"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".ftl"/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="requestContextAttribute" value="request"/>
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8">
        <property name="maxUploadSize">
            <value>524288000</value>
        </property>
        <property name="maxInMemorySize">
            <value>40960</value>
        </property>
    </bean>
</beans>
上面引入的另一个配置文件是数据库的连接设置等内容,这里就不贴了 另外我是用的 Spring + SpringMVC + Mybatis , controller部分的注解开启是在springMVC的配置文件当中的,是不是因为这个原因导致AOP不能切到Controller?[/quote] 是的 ioc和aop都用注解扫描的方式,context命名空间和aop命名空间要在一个xml文件里,也就是被某个aop命名空间创建的增强只能作用于同一个xml文件内创建的bean 一般aop命名空间也需要两个,然后把对应的类放在相应的包里
柳叁 2017-01-11
  • 打赏
  • 举报
回复
引用 9 楼 windowsoahil 的回复:
springaop有个大前提,被增强对象必须是ioc容器的bean 所以,可以增强不是自己写的类,但得把它配置为bean springaop分为传统的MethodInterceptor和后来的AspectJ,都有这个限制 基本上就是(AspectJ方式): 1、把被增强类配一个对象作为bean; 2、@Aspect,写切点表达式和增强逻辑,注意它也得配置成bean; 3、如果你用Intellij,它本身就能动态提示出你的aop写对了没有
嗯。。。。。不用那种方式了费劲不讨好。请问一个其他问题,就是我的AOP切不到Controller,能切到Service 这是我的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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
    <!-- 注解开启 -->
    <context:component-scan base-package="com.abcd.aa.device.admin,com.abcd.aa.device.core">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
    <!-- aop 开启 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 引入其他Spring配置 -->
    <import resource="spring-mybatis-skygBaseDevice.xml"/>

    <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!-- 这里定义freemarker.properties文件的加载 -->
    <bean id="freemarkerConfiguration" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:freemarker.properties"/>
    </bean>

    <!-- 配置Freemarker -->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath" value="/WEB-INF/view/"/>
        <property name="freemarkerVariables">
            <map>
                <entry key="xml_escape" value-ref="fmXmlEscape"/>
            </map>
        </property>
        <property name="freemarkerSettings" ref="freemarkerConfiguration"/>
    </bean>
    <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>

    <!-- freemarker页面解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="cache" value="true"/>
        <property name="prefix" value=""/>
        <property name="suffix" value=".ftl"/>
        <property name="contentType" value="text/html;charset=UTF-8"/>
        <property name="requestContextAttribute" value="request"/>
        <property name="exposeSpringMacroHelpers" value="true"/>
        <property name="exposeRequestAttributes" value="true"/>
        <property name="exposeSessionAttributes" value="true"/>
    </bean>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8">
        <property name="maxUploadSize">
            <value>524288000</value>
        </property>
        <property name="maxInMemorySize">
            <value>40960</value>
        </property>
    </bean>
</beans>
上面引入的另一个配置文件是数据库的连接设置等内容,这里就不贴了 另外我是用的 Spring + SpringMVC + Mybatis , controller部分的注解开启是在springMVC的配置文件当中的,是不是因为这个原因导致AOP不能切到Controller?
解开者 2017-01-07
  • 打赏
  • 举报
回复
springaop有个大前提,被增强对象必须是ioc容器的bean 所以,可以增强不是自己写的类,但得把它配置为bean springaop分为传统的MethodInterceptor和后来的AspectJ,都有这个限制 基本上就是(AspectJ方式): 1、把被增强类配一个对象作为bean; 2、@Aspect,写切点表达式和增强逻辑,注意它也得配置成bean; 3、如果你用Intellij,它本身就能动态提示出你的aop写对了没有
鬼善 2017-01-06
  • 打赏
  • 举报
回复
[quote=引用 3 楼 qq_33458228 的回复:] 如果你项目启动是没错的话,把sping的配置粘出下,看看
yuqi_hz 2017-01-05
  • 打赏
  • 举报
回复
你这个可以用mybatis的Interceptor接口,重写intercept方法
bcsflilong 2017-01-05
  • 打赏
  • 举报
回复
我认为是没有问题的 但是前提是 mybatis 交给spring管理
柳叁 2017-01-05
  • 打赏
  • 举报
回复
引用 3 楼 qq_33458228 的回复:
可以!用注解或者bean管理,maven只是用来更好的管理jra包!与sping 怎么切入没关系
貌似我配置的有问题,无论切哪里都切不进去 orz aop配置有哪里是比较容易忽略的坑吗?我在网上查了好多东西,还是没有弄好 根据idea的提示,语法上应该是没问题的。前面有那个 m 的那个图标,而且点击的时候可以跳转到我想要切的地方,但是tomcat起来之后,请求触发那个函数的时候并没有进入advice里面
柳叁 2017-01-05
  • 打赏
  • 举报
回复
引用 2 楼 m2200 的回复:
这个方法所在的类没有受到spring容器的管理吧,应该不行。如果是被spring容器管理的,那应该可以。
貌似我配置的有问题,无论切哪里都切不进去 orz aop配置有哪里是比较容易忽略的坑吗?我在网上查了好多东西,还是没有弄好
鬼善 2017-01-04
  • 打赏
  • 举报
回复
可以!用注解或者bean管理,maven只是用来更好的管理jra包!与sping 怎么切入没关系
爱睡觉的阿狸 2017-01-04
  • 打赏
  • 举报
回复
这个方法所在的类没有受到spring容器的管理吧,应该不行。如果是被spring容器管理的,那应该可以。
柳叁 2017-01-04
  • 打赏
  • 举报
回复
我现在想实现的是 在 mybatis 的 execute() 处进行切点。 如果可以直接解决这个问题,也希望您能帮忙回答

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧