在社区的帮助下,我设法针对这两个问题提出了解决方案。离开这里。
第一部分-部署中的Aspectjrt.jar
首先,添加
Shrinkwrap到我的依赖项:
<dependency> <groupId>org.jboss.shrinkwrap.resolver</groupId> <artifactId>shrinkwrap-resolver-api-maven</artifactId> <scope>test</scope></dependency><dependency> <groupId>org.jboss.shrinkwrap.resolver</groupId> <artifactId>shrinkwrap-resolver-impl-maven</artifactId> <scope>test</scope></dependency><dependency> <groupId>org.jboss.shrinkwrap.resolver</groupId> <artifactId>shrinkwrap-resolver-impl-maven-archive</artifactId> <scope>test</scope></dependency>
<version>不需要:Arquillian的
BOM-已经包括了-会解决的。
然后添加
aspectj到部署类路径:
@RunWith(Arquillian.class)public class ArquillianTest { private static final String[] DEPENDENCIES = { "org.aspectj:aspectjrt:1.8.7" }; @Deployment public static JavaArchive createEnvironement() { JavaArchive lib = ShrinkWrap.create(JavaArchive.class, "libs.jar"); for (String dependency : DEPENDENCIES) { lib.merge(Maven.resolver().resolve(dependency).withTransitivity().asSingle(JavaArchive.class)); } JavaArchive jar = ShrinkWrap.create(JavaArchive.class) // create you deployment here .as(JavaArchive.class); JavaArchive toBeDeployed = jar.merge(lib); return toBeDeployed; } // other stuff, like tests}
第二部分:将一个bean注入一个方面
经过进一步的查询后,我认为simas_ch是正确的,因为它说CDI不会将bean注入方面。
解决办法:
@Inject通过方面将ed成员添加到bean中。
public interface Advised { String buildGreeting(String name);}public class AdvisedImpl implements Advised { String buildGreeting(String name) { return "ADVISED"; }}public class Greeter { public String greet(String name) { return "Hello, " + name + "."; }}...public aspect GreeterAspect { @Inject private Greeter Advised.greeter; // adding the member to the interface / class. No need for getters / setters private pointcut pc() : execution(* x.y.z.Advised.buildGreeting(String)); String around(Advised adv, String name) : pc() && target(adv) && args(name) { log.debug("Aspect is about to say something..."); String result = proceed(adv, name) + " - " + adv.greeter.greet(name); log.debug("Aspect said: '" + result + "'"); return result; }}
通过测试
@Testpublic void test() { assertThat(advised, not(is(nullValue()))); assertThat(advised.buildGreeting("Stefano"), equalToIgnoringCase("advised - hello, stefano."));}
成功了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)