Tag: spring aop

JUnit测试AspectJ

我正在尝试为Custom Aspect编写Junit测试。 这是Aspect Class Snippet: @Aspect @Component public class SampleAspect { private static Logger log = LoggerFactory.getLogger(SampleAspect.class); @Around(“execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))”) public Object intercept(final ProceedingJoinPoint point) throws Throwable { logger.info(“invoked Cutom aspect”); return point.proceed(); } } 因此,只要关节点与切入点匹配,上述方面就会截获。 它的工作正常。 但我的问题是如何对该类进行unit testing。 我有以下Junit测试: @Test(expected = MongoTimeoutException.class) public void TestWithMongoTemplate() { //MongoDocument class TestDocument test = new […]

Spring – 拦截bean创建和注入自定义代理

我有一个带有@Autowired字段和处理程序方法的@Controller我想用自定义注释进行注释。 例如, @Controller public class MyController{ @Autowired public MyDao myDao; @RequestMapping(“/home”) @OnlyIfXYZ public String onlyForXYZ() { // do something return “xyz”; } } 其中@OnlyIfXYZ是自定义注释的示例。 我以为我会拦截Controller bean的创建,传递我自己的CGLIB代理,Spring可以在其上设置属性,比如autowired字段。 我尝试使用InstantiationAwareBeanPostProcessor但该解决方案不能很好地工作,因为postProcessBeforeInstantiation()会使进程的其余部分短路。 我尝试使用postProcessAfterInitialization() ,如下所示 public class MyProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // Here the bean autowired fields are already set return […]

Spring Batch @StepScope无法生成CGLIB子类

编辑 我创建了一个复制问题的测试项目。 它可以在https://github.com/tomverelst/test-batch找到。 首先运行maven命令exec:java来启动HSQL数据库。 然后,您可以运行JUnit测试MigrationJobConfigurationTest来加载Spring应用程序上下文。 原始问题 启动Spring Batch应用程序时,Spring加载我的作业配置时出现以下exception: Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.sun.proxy.$Proxy34]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.sun.proxy.$Proxy34 这是由我的作业配置中的@StepScope注释引起的。 它试图用已经用JDK代理代理的CGLIB代理一个类,我不知道这个JDK代理的来源。 我也尝试过使用@Scope(value = “step”, proxyMode = ScopedProxyMode.NO) ,但是在调用JDK代理时会出现堆栈溢出错误,而JDK代理一直在调用自己。 如果删除@StepScope注释,应用程序将正确启动,但我需要能够将它们用于我的工作。 Spring配置 […]

使用Spring AOP的BeanNotOfRequiredTypeException

我正在springaop和spring配置文件下面尝试我的手: STRUM STRUM STRUM CRASH CRASH CRASH CRASH HUM HUM HUM 当我运行代码我得到错误说: 线程“main”中的exceptionorg.springframework.beans.factory.BeanNotOfRequiredTypeException:名为“eddie”的Bean必须是[com.springinaction.Instrumentalist]类型,但实际上是org.springframework.beans.factory类型为[$ Proxy4]的类型.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:348)org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java) :1008)在com.springinaction.Main.main(Main.java:12) 如果我在spring配置文件中注释元素,它运行完美.. 为什么会这样?

Log4j和AOP,如何获取实际的类名

我正在使用Spring AOP和Log4J实现一个记录器作为一个方面,但我注意到日志文件中的类名始终是LoggerAspect类名,所以…有没有办法跟踪我的实际类名登录?

访问方面类中的类变量

我正在创建一个带有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; } } 现在我想访问类abc的属性名称,然后如何在方面类中访问它? 我想在profile方法中显示abc类的name属性 我的abc课程如下 public class abc{ String name; public void setName(String n){ name=n; } public String getName(){ […]

无法将字段设置为com.sun.proxy。$ Proxy

对于文件上传,我试图在我的Spring Controller中注入并使用Validator,如下所示: @RestController @RequestMapping(“/api”) public class FileController{ @Autowired private MessageSource messageSource; @Autowired FileValidator validator; @InitBinder(“file”) public void initBinderFile(WebDataBinder binder) { binder.setValidator(validator); } @RequestMapping(value = “/fileUpload2”, method = RequestMethod.POST, produces = {“text/plain”}) @PreAuthorize(“hasAuthority(‘ADMINISTRATOR’)”) public String singleFileUpload2(@Valid @ModelAttribute(“file”) File file, BindingResult result) throws IOException { if (result.hasErrors()) { String errorString = “”; for (ObjectError error : result.getAllErrors()) […]

java.lang.ClassCastException:com.sun.proxy。$ Proxy0无法强制转换为org.andrea.myexample.myDeclarativeTransactionSpring.StudentJDBCTemplate

我试图在Spring Framework应用程序中实现关于声明式事务的本教程但是不起作用,因为当我尝试执行MainApp类来测试应用程序行为时,我得到一个错误: http://www.tutorialspoint.com/spring/declarative_management.htm 所以我有StudentDAO接口我只定义了我想要的CRUD方法: package org.andrea.myexample.myDeclarativeTransactionSpring; import java.util.List; import javax.sql.DataSource; /** Interfaccia che definisce i metodi che implementano le operazioni di CRUD * che vogliamo implementare nel nostro DAO: */ public interface StudentDAO { /** * Questo metodo viene usato per inizializzare le risorse del database cioè * la connessione al database: */ public void […]

通过Spring AOP + Aspectj进行exception处理

在我的项目中,我有一个域层,它基本上是POJO和一个位于域层顶部的Spring控制器/服务层。 我还有一个位于服务和域之间的AOP层。 我的域层正在抛出业务exception,这些exception现在正在服务层中处理。 但是我想更改它,以便在AOP层中处理从域层抛出的exception。 AOP层会出现某种错误响应并将其发送回Spring控制器/ Web服务层。 我可以创建一个IBizResponse并创建它的两个子类/接口,也许是SuccessResponse和ErrorResponse,并使我的域层方法返回IBizResponse。 但是,我无法弄清楚如何使AOP将ErrorResponse对象返回到服务层。

启用S​​pring AOP或AspectJ

这是从这个问题继续: @Aspect方面的Spring autowired bean为null 我最初的理解是,当使用Spring AOP时,使用@Aspect注释的类被创建为spring managed beans,因此dependency injection将正常工作。 但是,似乎带有@Aspect注释的对象被创建为弹簧容器外的单例,因此我必须像XML一样配置它,以便将其作为spring托管bean启用: 这已经让我完全糊涂了。 我认为以下配置将使用spring AOP: 因此,它将使用组件扫描创建方面bean来扫描@Aspect注释,然后autoproxy将创建一个beanPostProcessor,它使用适当的建议代理我的上下文中的所有bean。 然后我想要启用aspectJ我需要一个完全不同的XML配置(顺便说一下,我似乎无法在文档中找到一个例子)。 正是这种配置使用aspectJ来创建将在我的容器之外的方面,或者通过操纵字节码而不是代理来工作。 注意 这不是关于spring AOP和J方面之间差异的问题,这里有很好的阐述: Spring AOP vs AspectJ