我用原始的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) 你可以根据几个数组/其他集合中项目的完整迭代,共享任何在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语言的对象功能.