小编izh*_*sin的帖子

为静态方法设置AspectJ建议

我用原始的Pointcut和Advise方法编写了简单的Aspect:

@Aspect
public class MyAspect {

  @Pointcut("execution(static * com.mtag.util.SomeUtil.someMethod(..))")
  public void someMethodInvoke() { }

  @AfterReturning(value = "someMethodInvoke())", returning = "comparisonResult")
  public void decrementProductCount(List<String> comparisonResult) {
    //some actions
  }
}
Run Code Online (Sandbox Code Playgroud)

我有以下基于Spring注释的应用程序配置:

@Configuration
@EnableAspectJAutoProxy
public class AppConfig { 
  //...
}
Run Code Online (Sandbox Code Playgroud)

和com.mtag.util包中的实用程序类:

public class SomeUtil {
  static List<String> someMethod(List<String> oldList, List<String> newList) {
    //...
  } 
}
Run Code Online (Sandbox Code Playgroud)

但是当我打电话的时候

SomeUtil.someMethod(arg1, arg2);
Run Code Online (Sandbox Code Playgroud)

在单元测试中我可以看到方法调用没有被截获,我的@AfterReturning建议不起作用.

但是如果我将someMethod()类型更改为实例(非静态)方法,则切入到

@Pointcut("execution(* com.mtag.util.SomeUtil.someMethod(..))")
Run Code Online (Sandbox Code Playgroud)

并通过添加@Component注释来管理SomeUtil bean并调用目标metod,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class}, loader = AnnotationConfigContextLoader.class)
public class SomeUtilTest {

    @Autowired
    private SomeUtil someUtil;

    @Test
    public void …
Run Code Online (Sandbox Code Playgroud)

java aspectj spring-aop

7
推荐指数
1
解决办法
8239
查看次数

如何基于其他集合的所有元素的笛卡尔积构建集合?

你可以根据几个数组/其他集合中项目的完整迭代,共享任何在Scala中创建不可变集合的好解决方案吗?

例如,在Java中,您可以使用:

List<String> signals = ...;
List<SignalState> states = ...;

List<SignalAndState> result = new ArrayList<~>(signals.size() * states.size());

for (String signal: signals) {
  for (SignalState state: states) {
    // some if() condition or process() function can be here 
    result.add(new SignalAndState(signal, state))
  }
}
Run Code Online (Sandbox Code Playgroud)

使用Scala构建smth的最佳实践是什么?我认为,相同的方法(在for()中使用for())是个坏主意,并且根本不兼容Scala语言的对象功能.

java collections scala

4
推荐指数
1
解决办法
227
查看次数

标签 统计

java ×2

aspectj ×1

collections ×1

scala ×1

spring-aop ×1