标签: spring-aop

将模拟bean注入spring环境进行测试

我知道有类似的问题,例如在这里,但是做了搜索,我找到了一个解决方案,我在这里更开心

但我唯一的问题是,我不确定如何实现这个解决方案.

我希望能够做的是通过HotswappableTargetSource覆盖我的应用程序上下文中的选择bean的bean定义和我的测试版本,然后运行测试.

然后,对于每个测试用例,我想指定哪些bean我想要热插拔,然后每个测试必须能够创建自己的模拟版本并交换它们,并能够再次交换回来.

我能够获得运行测试的应用程序上下文,但我不知道如何配置bean可热插拔.我知道如何在使用xml配置bean时这样做,但我不想回到使用xml来配置bean.

java spring unit-testing junit4 spring-aop

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

访问方面类中的类变量

我正在创建一个带有spring aspectj的方面类,如下所示

@Aspect
public class AspectDemo {
  @Pointcut("execution(* abc.execute(..))")
     public void executeMethods() { }

 @Around("executeMethods()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            System.out.println("Going to call the method.");
            Object output = pjp.proceed();
            System.out.println("Method execution completed.");
            return output;
    }

} 
Run Code Online (Sandbox Code Playgroud)

现在我想访问类abc的属性名称然后如何在方面类中访问它?我想在profile方法中显示abc类的name属性

我的abc课程如下

public class abc{
String name;

public void setName(String n){
name=n;
}
public String getName(){
 return name;
}

public void execute(){
System.out.println("i am executing");
}
}
Run Code Online (Sandbox Code Playgroud)

如何访问方面类中的名称?

java spring aspectj spring-aop java-ee

8
推荐指数
1
解决办法
9142
查看次数

使用aspect作为从应用程序逻辑中删除防御性检查的方法是一个好主意吗?

有点长的头衔,但这通常是个问题.

我想知道您是否认为执行以下操作是个好主意.

代替:

public void buyItem(int itemId, int buyerId) {
    if (itemId <= 0) {

        throw new IlleglArgumentException("itemId must be positive");
    }
    if (buyerId <= 0) {

        throw new IlleglArgumentException("buyerId must be positive");
    }
    // buy logic
}
Run Code Online (Sandbox Code Playgroud)

我希望有类似的东西:

@Defensive("isPositive(#itemId, #buyerId)")
public void buyItem(int itemId, int buyerId) {
    // buy logic
}
Run Code Online (Sandbox Code Playgroud)

你认为这是好/可怕/太花哨/太慢?如果你真的认为它很好我想用SpEL来实现它,那么有没有人有更好/更轻/更快的东西?

谢谢,

java aop coding-style spring-aop

8
推荐指数
1
解决办法
219
查看次数

如何通过XML配置Spring Social

我花了几个小时试图让Twitter集成与Spring Social一起使用XML配置方法.我可以在Web上(以及在stackoverflow上)找到的所有示例始终使用示例中@Config显示的方法

无论出于何种原因,获取twitter API实例的bean定义都会引发AOP异常:

Caused by: java.lang.IllegalStateException: Cannot create scoped proxy for bean 'scopedTarget.twitter': Target type could not be determined at the time of proxy creation.
Run Code Online (Sandbox Code Playgroud)

这是我的完整配置文件:

    <?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:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:cxf="http://cxf.apache.org/core"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
       http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/DefaultDB" />

    <!-- initialize DB required to store …
Run Code Online (Sandbox Code Playgroud)

aop spring spring-aop xml-configuration spring-social

8
推荐指数
1
解决办法
7522
查看次数

面向方面的编程 - 建议在同一个线程中运行?

如果我有一个适用于某个方法的建议(之前,之后或之后),这个建议会在调用该方法的同一个线程中运行吗?这是Spring的AOP.

spring-aop

8
推荐指数
1
解决办法
1114
查看次数

使用spring aop记录方法的返回值

我有一个返回对象的方法.我想使用spring AOP在我的日志中打印该对象的值.我怎样才能做到这一点?

请帮忙!

spring-aop

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

在App Engine上使用Spring AOP会导致StackOverflowError

我们有一个在App Engine上运行并使用Spring框架的应用程序.最近我们添加了一些基于AOP的新功能.我们决定使用@AspectJ样式,因此我们添加<aop:aspectj-autoproxy>到基于XML的配置中并实现了各自的方面.一切都在开发服务器上运行正常,但是,当部署到云环境时,我们java.lang.StackOverflowError每次初始化应用程序时都会得到.

无法创建并导致错误的bean是使用@Configuration注释注释的配置类.看来基本上任何配置bean都可能导致错误.

您可以在下面看到相应的堆栈跟踪.

org.springframework.web.context.ContextLoader initWebApplicationContext: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objectifyConfig' defined in URL [jar:file:/base/data/home/apps/{app-id}/8.372375422460842231/WEB-INF/lib/{app-name}-1.0-SNAPSHOT.jar!/{path-to-class}/ObjectifyConfig.class]: Initialization of bean failed; nested exception is java.lang.StackOverflowError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548)
at org.mortbay.jetty.servlet.Context.startContext(Context.java:136)
at org.mortbay.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1250)
at org.mortbay.jetty.handler.ContextHandler.doStart(ContextHandler.java:517)
at org.mortbay.jetty.webapp.WebAppContext.doStart(WebAppContext.java:467)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.createHandler(AppVersionHandlerMap.java:219)
at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.getHandler(AppVersionHandlerMap.java:194)
at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:134)
at com.google.apphosting.runtime.JavaRuntime$RequestRunnable.run(JavaRuntime.java:446)
at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:435) …
Run Code Online (Sandbox Code Playgroud)

java google-app-engine spring spring-aop

8
推荐指数
1
解决办法
828
查看次数

Spring - 带注释的构造函数对象的AspectJ切入点

我正在使用Spring框架(4.0.5)和AspectJ进行AOP Logging开发一个java(JDK1.6)应用程序.

我的Aspect类工作正常,但我无法为构造函数对象创建切入点.

这是我的目标:

@Controller
public class ApplicationController {
    public ApplicationController(String myString, MyObject myObject) {
        ...
    }
    ...
    ..
    .
}
Run Code Online (Sandbox Code Playgroud)

这是我的Aspect类:

@Aspect
@Component
public class CommonLogAspect implements ILogAspect {
    Logger log = Logger.getLogger(CommonLogAspect.class);

    // @Before("execution(my.package.Class.new(..)))
    @Before("execution(* *.new(..))")
    public void constructorAnnotatedWithInject() {
        log.info("CONSTRUCTOR");
    }
}
Run Code Online (Sandbox Code Playgroud)

如何为构造函数对象创建切入点?


谢谢

java aop spring aspectj spring-aop

8
推荐指数
1
解决办法
7340
查看次数

为什么在Spring AOP中对象被包装到实现接口的JDK代理中?

我正在学习春天,我有跟随

考虑以下bean定义:

<bean id="clientService" class="com.myapp.service.ClientServiceImpl" />
Run Code Online (Sandbox Code Playgroud)

现在考虑一下它被声明为切入点*的情况,目标是**clientService bean中的所有方法.

还要考虑ClientServiceImpl实现3个接口

现在我知道,使用AOPclientService bean是代理和该代理实现了所有的3个接口.

但是实现所有这三个接口的确切原因是什么?

所以在我看来,存在两种代理(如果我说错误的断言,请纠正我):

  1. JDK代理:默认使用Spring(是真的吗?),我有一个接口,用于定义我想要代理的对象的方法.所以这个接口的具体实现是由代理包装的.所以当我在我的对象上调用一个方法时,我在它的代理上调用它.调用由最终执行方面的方法拦截器识别,然后执行调用的方法.

  2. CGLIB代理:在我看来,代理扩展了包装对象的实现,为它添加了额外的逻辑功能

像这样的东西:

在此输入图像描述

所以在我看来,Spring使用第一种基于接口实现的代理(是不是?):

在此输入图像描述

我认为在AOP中,额外的逻辑由方法拦截器的实现来表示(是真的吗?),标准逻辑由定义在接口中的方法的实现来表示.

但是,如果之前的推理是正确的,我的疑问是:为什么我需要定义这些接口,并且由对象包装的对象实现这些接口?(我无法理解代理本身是否实现了这些接口).

为什么?究竟如何运作?

TNX

java aop spring design-patterns spring-aop

8
推荐指数
1
解决办法
3306
查看次数

Spring AOP使用AspectJ工作还是什么?

我正在研究Spring AOP,我有以下疑问.

据我所知,有两种方法可以将AOP行为实现到Java应用程序中:

  1. AspectJ:这是第一个使用字节码修改进行方面编织的原始AOP技术.

  2. Spring AOP:基于Java的AOP框架,使用AspectJ集成,使用动态代理进行方面编织.

我的疑问是:究竟什么意味着Spring AOP是一个带有AspectJ集成的AOP框架?那么它依次使用AspectJ?或者是什么?

第二个疑问与Spring AOP的Spring配置有关,我知道我可以这样做:

1)使用Java配置类:

@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages=“com.example”)
public class AspectConfig {
    ...
}
Run Code Online (Sandbox Code Playgroud)

2)使用XML:

<beans>
    <aop:aspectj-autoproxy />
    <context:component-scan base-package=“com.example” />
</beans>
Run Code Online (Sandbox Code Playgroud)

因此,在两种配置中,Spring AOP似乎都使用AspectJ,因为在这些配置中我有:@EnableAspectJAutoProxy

它究竟意味着什么?

java aop spring aspectj spring-aop

8
推荐指数
1
解决办法
845
查看次数