当我试图实现一个Aspect,负责捕获和记录某种类型的错误时,我最初认为这可以使用AfterThrowing建议.然而,似乎他的建议没有捕获异常,但只是提供了一个额外的入口点来做异常的事情.
唯一能够解决有关异常的建议就是环境建议 - 无论是我做错了什么.
任何人都可以断言,如果我想要捕获异常,我必须使用AroundAdvice吗?我使用的配置如下:
@Pointcut("execution(* test.simple.OtherService.print*(..))")
public void printOperation() {}
@AfterThrowing(pointcut="printOperation()", throwing="exception")
public void logException(Throwable exception) {
System.out.println(exception.getMessage());
}
@Around("printOperation()")
public void swallowException(ProceedingJoinPoint pjp) throws Throwable {
try {
pjp.proceed();
} catch (Throwable exception) {
System.out.println(exception.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,在此示例中,我捕获了所有异常,因为它只是一个示例.我知道只是吞下所有异常的坏习惯,但对于我当前的用例,我希望只记录一种特殊类型的异常,同时避免重复记录逻辑.
我试图定义一个切入点,它将捕获每个注释的方法(即)@CatchThis.这是我自己的注释.
此外,我想访问该方法的第一个参数,它将是Long类型.可能还有其他争论,但我不关心它们.
编辑
这就是我现在所拥有的.我不知道的是如何传递带注释的方法的第一个参数@CatchThis.
@Aspect
public class MyAspect {
@Pointcut(value = "execution(public * *(..))")
public void anyPublicMethod() {
}
@Around("anyPublicMethod() && @annotation(catchThis)")
public Object logAction(ProceedingJoinPoint pjp, CatchThis catchThis) throws Throwable {
return pjp.proceed();
}
}
Run Code Online (Sandbox Code Playgroud) 我在aspectJ表达式中:
@Pointcut("within(com.param.cpms.dao.impl.ProjectMetaDaoImpl)")
public void daoExceptionHandle() {
}
Run Code Online (Sandbox Code Playgroud)
在Spring 3.0启动时,我收到以下错误:
nested exception is java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
Run Code Online (Sandbox Code Playgroud) 我在Spring(版本2.5.6)中使用切入点定义时遇到了问题.我试图拦截对类的所有方法调用,除了给定的方法(下面的例子中的someMethod).
<aop:config>
<aop:advisor
pointcut="execution(* x.y.z.ClassName.*(..)) AND NOT
execution(* x.y.x.ClassName.someMethod(..))"
/>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
但是,也会为someMethod调用拦截器.
然后我尝试了这个:
<aop:config>
<aop:advisor
pointcut="execution(* x.y.z.ClassName.(* AND NOT someMethod)(..)) )"
/>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
但这不能编译,因为它不是有效的语法(我得到一个BeanCreationException).
任何人都可以提供任何建议吗?
谁能告诉我Joinpoint和 之间有什么区别 Proceedingjoinpoint?
何时使用Joinpoint和 Proceedingjoinpoint方面类的方法?
我用过JoinPoint我AspectJ class喜欢的,
@Pointcut("execution(* com.pointel.aop.test1.AopTest.beforeAspect(..))")
public void adviceChild(){}
@Before("adviceChild()")
public void beforeAdvicing(JoinPoint joinPoint /*,ProceedingJoinPoint pjp - used refer book marks of AOP*/){
//Used to get the parameters of the method !
Object[] arguments = joinPoint.getArgs();
for (Object object : arguments) {
System.out.println("List of parameters : " + object);
}
System.out.println("Method name : " + joinPoint.getSignature().getName());
log.info("beforeAdvicing...........****************...........");
log.info("Method name : " + joinPoint.getSignature().getName());
System.out.println("************************");
}
Run Code Online (Sandbox Code Playgroud)
但我在其他资源中看到的一些是,
@Around("execution(* …Run Code Online (Sandbox Code Playgroud) 问题:
我正在创建一个切入点来执行类中的方法.这个类是一个控制器类,由注释@Controller表示,因此方面不需要bean.我附加了调度程序servlet代码,方面和控制器类.有人可以确定问题是什么.
调度服务器:
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:spring-configured />
<aop:aspectj-autoproxy />
<bean id="LoggerBean" class="com.persistent.eap.aop.LoggerAspect" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="com.persistent.eap.validators.UserRegistrationValidator" />
<bean id="userRegistrationService" class="com.persistent.eap.service.impl.UserRegistrationServiceImpl" />
<bean id="userOperationsService" class="com.persistent.eap.service.impl.UserOperationsServiceImpl" />
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"/>
</bean>
<bean id="tilesViewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" p:definitions="/WEB-INF/tiles-defs.xml" />
<context:component-scan base-package="com.persistent.eap.controllers" />
<context:component-scan base-package="com.persistent.eap.service" />
<context:component-scan base-package="com.persistent.eap.dao" …Run Code Online (Sandbox Code Playgroud) Spring AOP有一个名为的方法级跟踪器CustomizableTraceInterceptor.使用Spring的XML配置方法,可以像这样设置此跟踪器:
<bean id="customizableTraceInterceptor" class="
org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor"
pointcut="execution(public * org.springframework.data.jpa.repository.JpaRepository+.*(..))"/>
</aop:config>
Run Code Online (Sandbox Code Playgroud)
我想使用Spring的JavaConfig样式设置上述配置(即利用Java注释,尤其是@EnableAspectJAutoProxy在JavaConfig中激活AspectJ).
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "some.package" })
@ComponentScan(basePackages = { "some.package2", "some.package3" })
@EnableAspectJAutoProxy
public class FacebookDomainConfiguration {
@Bean someBean() {
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
什么是@EnableAspectJAutoProxy风格的等价物<aop:advisor advice-ref="customizableTraceInterceptor" ...>?
我使用Spring 2.5.6,asm 1.5.3,aspectjrt/aspectjweaver 1.6.1,cglib 2.1_3在我的基于Web的Spring应用程序中,我有以下类:
package uk.co.txttools.aspects;
@Aspect
public class LoggingAspect {
@Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.set*(..))")
public void setLoggingAdvice(){
System.out.println("********************************* Advice run..... set mothod called....");
}
@AfterThrowing("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
public void hadleException(){
System.out.println("================= PreviewMessageController =========== ON SUBMIT Exception Throwen ==================");
}
@Before("execution(* uk.co.txttools.web.controller.compose.PreviewMessageController.onSubmit(..) throws java.lang.Exception)")
public void OnSubmitAspect(){
System.out.println("================= PreviewMessageController =========== ON SUBMIT CALLED ==================");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个Controller:uk.co.txttools.web.controller.compose.PreviewMessageController
which hasonSubmit()method, which get called from web page.
I have separateapplicationContext.xml`文件.
我的springapp-servlet.xml(在web.xml文件中使用org.springframework.web.servlet.DispatcherServlet)文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" …Run Code Online (Sandbox Code Playgroud) 我正在尝试为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();
}
}
Run Code Online (Sandbox Code Playgroud)
因此,只要关节点与切入点匹配,上述方面就会截获.它的工作正常.
但我的问题是如何对该类进行单元测试.我有以下Junit测试:
@Test(expected = MongoTimeoutException.class)
public void TestWithMongoTemplate() {
//MongoDocument class
TestDocument test = new TestDocument();
ApplicationContext ctx = new AnnotationConfigApplicationContext(TestMongoConfigurationMain.class);
MongoTemplate mongoTemplate = ctx.getBean(MongoTemplate.class);
//this call is being intercepted by SampleAspect
mongoTemplate.save(test);
}
Run Code Online (Sandbox Code Playgroud)
因此,我mongoTemplate.save(test)在Junit中被拦截,SampleAspect因为它匹配切入点.但是,我应该如何确保junits(可能通过声明)SampleAspect在调用该关节点时我正在拦截?
我不能断言返回值,intercept()因为除了执行关节点之外没有什么特别之处.因此,我的Junit无法找到任何区别,无论是由方面执行还是基于返回值的常规执行. …
我有一个MyTask实现的类,Runnable并且可以在任何给定时刻实例化许多这样的对象.有些属性我想自动MyTask上课.
但我认为,如果我MyTask用@Component它标记它将成为一个弹簧管理的单身正确吗?这不是我想要的,我需要由TaskExecutor运行这个类的许多独立实例.
所以我的问题:
@Component注释的理解从根本上是错误的吗?它不会MyTask成为一个由春季管理的单身人士吗?@Autowired并注入属性吗?MyTask吗?更新#1 - 这些不起作用:
public class MyTask implements Runnable { // I want this class to be non-singleton
@Autowired
public SomeSpecialSpringConfiguredConnectionClass blah; // this is the singleton bean that should be injected
@Override
public void run() {
// BLAH IS NULL, this shouldn't be NULL, that is not what I want
// which makes sense …Run Code Online (Sandbox Code Playgroud)