标签: spring-aop

如何使用基于java的注释启用<aop:aspectj-autoproxy>

我试图在没有任何XML的情况下设置Spring AOP.我想<aop:aspectj-autoproxy>在一个带注释的类中启用@Configuration.

这是在XML文件中定义的方式:

<aop:aspectj-autoproxy>
<aop:include name="msgHandlingAspect" />
</aop:aspectj-autoproxy>
Run Code Online (Sandbox Code Playgroud)

我试着用我的课程注释@Configuration,@EnableAspectJAutoProxy 但没有发生任何事情.

java spring spring-aop

37
推荐指数
1
解决办法
3万
查看次数

Spring AOP - 为什么我需要aspectjweaver?

我用Spring AOP写了一个非常简单的Aspect.它有效,但我在理解真正发生的事情时遇到了一些问题.我不明白为什么我要添加aspectjweaver.jar?Spring-AOP文档明确指出,只要我使用Spring-AOP,我就不需要aspectj编译器或weaver:

AOP运行时仍然是纯Spring AOP,并且不依赖于AspectJ编译器或weaver.

我的配置如下所示:

<aop:aspectj-autoproxy />

@Aspect
@Service
public class RemoteInvocationAspect {

    @Before("execution(* at.test.mypackage.*.*(..))")
    public void test() {
        System.out.println("test");
    }
    ...
Run Code Online (Sandbox Code Playgroud)

我也尝试过XML配置,虽然没有改变任何东西.也许我可以放手,但我真的想了解为什么使用aspectj-weaver?如果我没有在maven中添加依赖项,我会得到 java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException

java aop spring aspectj spring-aop

35
推荐指数
2
解决办法
3万
查看次数

@Aspect方面的Spring autowired bean为null

我有以下弹簧配置:

<context:component-scan base-package="uk.co.mysite.googlecontactsync.aop"/>

<bean name="simpleEmailSender" class="uk.co.mysite.util.email.simple.SimpleEmailSenderImplementation"/>

<aop:aspectj-autoproxy/>
Run Code Online (Sandbox Code Playgroud)

然后我有一个方面:

@Aspect
public class SyncLoggingAspect {
    @Autowired
    private SimpleEmailSender simpleEmailSender

    @AfterReturning(value="execution(* uk.co.mysite.datasync.polling.Poller+.doPoll())", returning="pusher")
    public void afterPoll(Pusher pusher) {      
        simpleEmailSender.send(new PusherEmail(pusher));
    }
}
Run Code Online (Sandbox Code Playgroud)

这个方面有效(我可以在afterPoll上找到断点)但simpleEmailSender为null.不幸的是,我找不到明确的文件说明原因.(为了记录,我的simpleEmailSender bean存在并正确连接到其他类)以下事情使我感到困惑:

  1. 上下文:组件扫描应该是@Aspect?如果它肯定是一个弹簧管理bean,那么自动装配应该工作吗?
  2. 如果context:component-scan不是用于创建方面,那么我的方面是如何创建的?我认为aop:aspectj-autoproxy只是创建一个beanPostProcessor来代理我的@Aspect类?如果它不是一个Spring托管bean,它会如何做到这一点?

显然你可以告诉我,我不了解事情应该如何从头开始.

java aop spring aspectj spring-aop

34
推荐指数
4
解决办法
3万
查看次数

使用@Configurable进行Spring自动装配

我正在尝试使用Spring @Configurable并将@AutowireDAO注入域对象,这样他们就不需要直接了解持久层了.

我正在尝试关注http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable,但我的代码似乎没有效果.

基本上,我有:

@Configurable
public class Artist {

    @Autowired
    private ArtistDAO artistDao;

    public void setArtistDao(ArtistDAO artistDao) {
        this.artistDao = artistDao;
    }

    public void save() {
        artistDao.save(this);
    }

}
Run Code Online (Sandbox Code Playgroud)

和:

public interface ArtistDAO {

    public void save(Artist artist);

}
Run Code Online (Sandbox Code Playgroud)

@Component
public class ArtistDAOImpl implements ArtistDAO {

    @Override
    public void save(Artist artist) {
        System.out.println("saving");
    }

}
Run Code Online (Sandbox Code Playgroud)

在application-context.xml中,我有:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springsource.org/dtd/spring-beans-2.0.dtd">
<beans>

    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
    <bean class="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect" factory-method="aspectOf"/>

</beans>
Run Code Online (Sandbox Code Playgroud)

类路径扫描和初始化由弹簧模块执行Play!框架,虽然其他autowired …

java spring aspectj spring-aop

32
推荐指数
3
解决办法
7万
查看次数

使用Spring AOP记录好主意吗?

我在春天读了目前和用于使用AOP的测井方法调用的开始和结束的一个例子.

我还读到使用AOP会影响性能.

对于这种类型的日志记录,使用Spring AOP是个好主意吗?我的理解是Spring使用Dynamic AOP会更好地将静态AOP(如AspectJ)用于这种类型的AOP.

Curently在我工作的公司的编码策略需要记录的一个可笑的量,我想,以减少日志代码的ammount的我必须写,并提高我的代码的可读性.

我吠叫错了树吗?

java logging aop spring spring-aop

30
推荐指数
2
解决办法
2万
查看次数

为什么AspectJ编译时不编织Spring的@Configurable工作?

更新5:我已经下载了基于最新Eclipse的最新Spring ToolsSuite IDE.当我将项目导入为Maven项目时,Eclipse/STS似乎使用Maven目标来构建我的项目.这意味着AspectJ最终在Eclipse中正常工作.

更新4:我最终只是使用Maven + AspectJ插件进行编译时编织,有效地绕过了Eclipse的机制.

更新3:似乎AspectJ的Eclipse插件破坏了Eclipse正确发布到Tomcat的能力.只有删除项目中的AspectJ功能,才能让它再次正确发布.很烦人.

更新2:我现在在Eclipse中工作了.这让我感到非常不舒服,但我不知道我是如何使用Eclipse或Maven构建的.它似乎是一个编译问题而不是运行时问题.

更新1:看来我已经通过Maven构建工作了,但我不知道如何.Eclipse仍然无法正常工作.我在pom.xml中唯一改变的是添加这些(无关紧要的?)配置参数:

<source>1.6</source>
<complianceLevel>1.6</complianceLevel>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>
Run Code Online (Sandbox Code Playgroud)

我实际上担心我会重复这个问题,一切都不一致.随着我了解更多信息,我会更新此问题.

关于Eclipse,我通过采用我想编织的二进制方面取得了一些进展 - 在本例中是spring-aspects.jar - 并将其复制出我的类路径.然后我将这个外部jar添加到我的Aspect Path中.执行此操作后,Eclipse在我的代码中正确显示了AspectJ标记.令人讨厌的是,我不能将spring-aspects.jar留在我的Java Build Path中,这是由Maven通过Maven插件为我维护的.但是,出于某种原因,除非将AspectJ插件显式添加到Aspect Path,否则AspectJ插件不会看到二进制方面.


原始帖子: @Configurable是一个Spring注释,它允许将依赖项注入到Spring外部实例化的对象中(例如,通过Hibernate或某些Factory类).

我以前使用这个注释与加载时编织,它主要工作.偶尔我会启动,没有任何东西会被注入.这个问题催生了这个StackOverflow问题.答案并不多,但大多数人建议我尝试编译时编织,因为可靠性更高.

我为Eclipse和Maven安装了AspectJ插件.这两个产生的似乎是正确编译的类.我在AspectJ编译之前在文本编辑器中打开了其中一个类,但未发现对AspectJ的引用.我在AspectJ编译后打开它,Eclipse和Maven生成的版本都引用了org.aspectj.weaver.MethodDeclarationLineNumber.这就是我认为它被正确编译的原因.问题是,一旦部署,就不会注入依赖项.

My Spring applicationContext.xml确实包含以下内容:

    <context:spring-configured />

    <context:component-scan base-package="com.myapp" />
Run Code Online (Sandbox Code Playgroud)

以上所有标记为@Configurable的类都需要完成DI吗?在从加载时编织到编译时编织的转换过程中,我从applicationContext.xml中删除了META-INF/aop.xml,<context:load-time-weaver />,从我的context.xml中删除了Spring的Tomcat编织器.

我该如何进一步调查此问题?可能的原因是什么?

java aop spring-aop compile-time-weaving maven

25
推荐指数
2
解决办法
3万
查看次数

获取Spring bean的真正Class对象

我使用Spring注入bean.我正在使用一些注释来注释bean方法(Security,TransactionManagement,ExceptionHanling,Logging).问题是:

我想创建JUnit测试来检查我是否忘记注释一些方法.但Spring返回$ ProxyXXX类,没有任何方法注释.

Method[] methods = logic.getClass().getMethods();

  for (Method method : methods) {
     Annotation[] annotations = method.getAnnotations();   // empty array!
Run Code Online (Sandbox Code Playgroud)

如何获取方法的注释或获取真正的类对象?

PS Spring 2.5.6,JDKDynamicProxy(不是CGLib)

java spring spring-aop

23
推荐指数
4
解决办法
2万
查看次数

在实际Web请求之外使用请求范围的bean

我有一个Web应用程序,它在一个独立的线程中运行Spring Integration逻辑.问题是,在某些时候,我的Spring Integration逻辑尝试使用请求范围的bean,然后我得到以下错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.tenantContext': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still …
Run Code Online (Sandbox Code Playgroud)

spring spring-integration spring-aop

23
推荐指数
4
解决办法
5万
查看次数

从对象本身获取AOP代理

有可能在Spring中获取给定对象的代理吗?我需要调用子类的函数.但是,显然,当我直接打电话时,方面不适用.这是一个例子:

public class Parent {

    public doSomething() {
        Parent proxyOfMe = Spring.getProxyOfMe(this); // (please)
        Method method = this.class.getMethod("sayHello");
        method.invoke(proxyOfMe);
    }
}

public class Child extends Parent {

    @Secured("president")
    public void sayHello() {
        System.out.println("Hello Mr. President");
    }
}
Run Code Online (Sandbox Code Playgroud)

我找到了实现这一目标的方法.它有效,但我认为不是很优雅:

public class Parent implements BeanNameAware {

    @Autowired private ApplicationContext applicationContext;
    private String beanName; // Getter

    public doSomething() {
        Parent proxyOfMe = applicationContext.getBean(beanName, Parent.class);
        Method method = this.class.getMethod("sayHello");
        method.invoke(proxyOfMe);
    }
}
Run Code Online (Sandbox Code Playgroud)

aop spring spring-aop

21
推荐指数
3
解决办法
2万
查看次数

@AspectJ切入包内的所有方法

我有一个特定包的工作代码,但我想为所有控制器,服务dao包配置它

  • com.abc.xyz.content.controller
  • com.abc.xyz.content.service
  • com.abc.xyz.content.dao
  • com.abc.xyz.category.controller
  • com.abc.xyz.category.service
  • com.abc.xyz.category.dao

等等...这是我的项目的基础包,有人可以帮助我如何做到这一点,以便它适用于我的网络项目的所有类,包括控制器,提前感谢...

package com.abc.xyz.utilities;

import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect
{
    private Log log = LogFactory.getLog(this.getClass());

    @Pointcut("execution(* com.abc.xyz.content.service..*(..))")
    protected void loggingOperation()
    {
    }

    @Before("loggingOperation()")
    @Order(1)
    public void logJoinPoint(JoinPoint joinPoint)
    {
    log.info("Signature declaring type : " + joinPoint.getSignature().getDeclaringTypeName());
    log.info("Signature name : " + joinPoint.getSignature().getName()); …
Run Code Online (Sandbox Code Playgroud)

java aop spring aspectj spring-aop

21
推荐指数
2
解决办法
3万
查看次数