use*_*008 9 java aop spring aspectj spring-aop
以下是我的切入点和建议声明
//PointCut on A method which takes two parameters and is in a DAO
@Pointcut("execution(backend.repository.QuestionsRepository.AnswerQuestion (..))")
public void answerQuestionPointCut() {}
@Around(
value="web.activity.advisors.UserActivityAdvisor.answerQuestionPointCut()",
argNames="question,answer"
)
// Do something
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed: expecting 'name pattern' at character position 65
execution(backend.repository.QuestionsRepository.AnswerQuestion (..))
^
Run Code Online (Sandbox Code Playgroud)
我坚持这个,任何指针
Sea*_*oyd 15
您缺少返回类型:
@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))")
Run Code Online (Sandbox Code Playgroud)
你必须绑定参数名称,如下所示:
@Pointcut("execution(* backend.repository.QuestionsRepository.AnswerQuestion (..))
&& args(question, answer)") // wrapped for readability only
Run Code Online (Sandbox Code Playgroud)
服务接口:
package foo.bar.service;
public interface Service{
void whack(String thing, Integer thang);
}
Run Code Online (Sandbox Code Playgroud)
实施班:
package foo.bar.service;
public class DefaultService implements Service{
@Override
public void whack(final String thing, final Integer thang){
System.out.println(
"Inside thing: " + thing + ", inside thang: " + thang
);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring AOP方面:
@Aspect
public class ServiceAspect{
@Pointcut("execution(* foo.bar.service.*.*(..))")
public void serviceMethodExecution(){
}
@Around(value = "serviceMethodExecution() && args(param1, param2)")
public void aroundServiceMethodExecution(final ProceedingJoinPoint pjp,
final String param1,
final Integer param2) throws Throwable{
System.out.println("Before thing: " + param1 + ", before thang: "
+ param2);
pjp.proceed();
System.out.println("After thing: " + param1 + ", after thang: "
+ param2);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring Context XML:
<aop:aspectj-autoproxy proxy-target-class="false" />
<bean class="foo.bar.service.DefaultService" />
<bean class="foo.bar.aspects.ServiceAspect" />
Run Code Online (Sandbox Code Playgroud)
主要测试类:
现在这是测试整个过程的主要方法.它启动一个没有XML配置的Spring ApplicationContext ,上面的XML定义了服务bean和方面(事实证明,没有XML的解决方案只能运行,因为我打开了AspectJ编织,我不知道我必须包含哪些bean启用aspectj-autoproxy,所以我现在使用ClassPathXmlApplicationContext这个最小的XML):
public static void main(final String[] args){
final ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("classpath:/aspectContext.xml");
final Service service = applicationContext.getBean(Service.class);
service.whack("abc", 123);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Before thing: abc, before thang: 123
Inside thing: abc, inside thang: 123
After thing: abc, after thang: 123
Run Code Online (Sandbox Code Playgroud)
这应该让你开始.基本上:如果使用JDK代理(默认为spring),则需要检查您正在拦截的方法是否由服务接口支持.在这里阅读Spring AOP代理机制.
注意:
如您所见,我将方法参数绑定到方面,而不是切入点,因此可以为具有不同参数签名的方法重用切入点.但是也可以在切入点中绑定它们,如下所示:
@Pointcut(value = "execution(* foo.bar.service.*.*(..)) && args(a,b)",
argNames = "a,b")
public void serviceMethodExecution(final String a, final Integer b){
}
@Around(value = "serviceMethodExecution(param1, param2)",
argNames = "param1,param2")
public void aroundServiceMethodExecution(final String param1,
final Integer param2,
final ProceedingJoinPoint pjp) throws Throwable{
System.out.println("Before thing: " + param1 + ", before thang: "
+ param2);
pjp.proceed();
System.out.println("After thing: " + param1 + ", after thang: "
+ param2);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25884 次 |
| 最近记录: |