我正在尝试使用Spring和AspectJ实现加载时间编织.据我所知,我已经正确配置了所有内容,但是当我尝试运行集成测试时,我不断收到错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1079)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:643)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.boku.risk.service.perisistence.PersistenceTestBase.setupBase(PersistenceTestBase.java:23)
at com.boku.risk.service.dao.CountryLimitDaoTest.setup(CountryLimitDaoTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native …Run Code Online (Sandbox Code Playgroud) 有没有人有使用OSGi 4.3+ Weaving Hook服务的例子?使用AspectJ,ASM,JavaAssist怎么样?有人在使用OSGi WeavingHooks吗?
OSGi Core 5.0.0第56.2节中的示例简单地省略了实际的编织,并说"最后的编织留给了读者".
我的目标是:
我的问题主要是#3.
我目前正在尝试使用AspectJ WeavingAdaptor进行编织,但是我在使用方面库时遇到了问题,因为它期望构造函数中的java.net.URL [] aspectURLs是jar或目录它可以在文件系统上找到,而不是捆绑.另外,我不知道如何通过回调的处理由韦弗产生的任何新类acceptClass(字符串名称,字节[])的方法GeneratedClassHandler.
也许WeavingAdaptor不适合开始编织?或者也许我不应该使用AspectJ?
MyAnnotation.java
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
Run Code Online (Sandbox Code Playgroud)
MyWeavingHook.java
public class MyWeavingHook implements WeavingHook {
public class MyWeavingClassloader implements WeavingClassLoader {
private Bundle b;
public MyWeavingClassLoader(Bundle b) {
this.b = b;
}
void acceptClass(java.lang.String name, byte[] bytes) {
//no way to get this back into the woven classes bundle classloader?
}
URL[] getAspectURLs() …Run Code Online (Sandbox Code Playgroud) Say Service调用需要应用日志方面(注释)的Dao类.我想知道方面实际上是如何应用的.
根据我在服务对象下注入DAO时的理解,spring发现有一些方面(在这种情况下是日志记录)是为DAO配置的,因此它会注入代理对象而不是实际的目标对象.现在,当对DAO中的任何方法进行实际调用时,代理应用方面,然后调用实际的目标对象.那是对的吗 ?另外我相信这叫做跑步编织.
另一方面,可以使用加载时间编织(使用javaagent配置)完成相同的操作,其中对需要应用方面的类执行字节代码操作.所以代理不会在这里出现.
如果我错了,请纠正我,因为这是所有弹簧模块的基础?
我正在尝试使用类似的东西org.springframework.cache.annotation.Cacheable:
自定义注释:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CheckEntity {
String message() default "Check entity msg";
String key() default "";
}
Run Code Online (Sandbox Code Playgroud)
方面:
@Component
@Aspect
public class CheckEntityAspect {
@Before("execution(* *.*(..)) && @annotation(checkEntity)")
public void checkEntity(JoinPoint joinPoint, CheckEntitty checkEntity) {
System.out.println("running entity check: " + joinPoint.getSignature().getName());
}
}
Run Code Online (Sandbox Code Playgroud)
服务:
@Service
@Transactional
public class EntityServiceImpl implements EntityService {
@CheckEntity(key = "#id")
public Entity getEntity(Long id) {
return new Entity(id);
}
}
Run Code Online (Sandbox Code Playgroud)
我的IDE(IntelliJ)没有看到任何与key = "#id"使用有关的特殊情况,与使用Cacheable不同颜色而不是纯文本的类似用法相比.我提到IDE部分只是作为一个提示,如果有帮助,看起来IDE预先知道这些注释或它只是意识到我的例子中不存在的一些连接.
checkEntity.key中的值为"#id"而不是预期的数字.我尝试使用ExpressionParser但可能不是以正确的方式. …
我有一个JAVA Web应用程序,它暴露了RESTful apis.我的要求是记录服务器处理的所有JSON请求和响应.是否有像-Dcom.sun.xml.internal.ws.transport.http.client.HttpTransportPipe.dump=trueJAX-WS 这样的参数?
我也在探索AOP方法.我应该在AOP模式中添加什么方法签名?
我正在使用Tomcat服务器和jersey进行JAX-RS实现.
我有两个方面,每个方面修改方法参数.当两个方面都应用于同一个方法时,我希望将方面的执行链接起来,并且我希望第一个方面中修改的参数可用于第二个方面.joinPoint.getArgs();但是,似乎每个方面只获得原始论点; 第二个方面永远不会看到修改后的值.我设计了一个例子:
测试类:
public class AspectTest extends TestCase {
@Moo
private void foo(String boo, String foo) {
System.out.println(boo + foo);
}
public void testAspect() {
foo("You should", " never see this");
}
}
Run Code Online (Sandbox Code Playgroud)
方法foo()由两个方面建议:
@Aspect
public class MooImpl {
@Pointcut("execution(@Moo * *(..))")
public void methodPointcut() {}
@Around("methodPointcut()")
public Object afterMethodInControllerClass(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("MooImpl is being called");
Object[] args = joinPoint.getArgs();
args[0] = "don't";
return joinPoint.proceed(args);
}
}
Run Code Online (Sandbox Code Playgroud)
和...
@Aspect
public class DoubleMooImpl {
@Pointcut("execution(@Moo * *(..))") …Run Code Online (Sandbox Code Playgroud) 我将Spring Security添加到一个Spring项目中.系统的体系结构是REST,用户可以访问不同的资源.
我想向拥有此信息的管理员和用户提供个人信息访问权限.我已经开始简单:过滤用户配置文件,如下所示:
在我的服务层中,我想使用方法注释并包含方法参数.
@PreAuthorize("hasRole('ROLE_ADMIN') or principal.userId == #id")
public Usuario getUser(int id) throws DAOException {
...
}
Run Code Online (Sandbox Code Playgroud)
但这根本不起作用.当请求此URL时,任何用户都可以看到所有配置文件(管理员和所有用户)(Web层):
@RequestMapping(value="/user/{uid}", method=RequestMethod.GET)
public ModelAndView getUser(@PathVariable int uid) throws DAOException {
userDAO = new UsuarioJPADAO();
userService.setUsuarioDAO(userDAO);
return new ModelAndView("user", "user", userService.getUser(uid));
}
Run Code Online (Sandbox Code Playgroud)
这是我的 security.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<!-- Security Annotations -->
<global-method-security
pre-post-annotations="enabled"/>
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/css/**" access="permitAll" />
<intercept-url pattern="/images/**" access="permitAll" />
<intercept-url pattern="/js/**" access="permitAll" />
<intercept-url …Run Code Online (Sandbox Code Playgroud) 我已经在这里跟踪了几乎所有的JUnit + Maven + AspectJ问题,甚至我很确定我已经正确设置了所有内容,我无法测试它.
我有一个Maven模块只有一个方面:
@Aspect
public class AssertionAspect {
@Pointcut("execution(@org.junit.Test * *())")
public void testMethodEntryPoint() {}
@Before("testMethodEntryPoint()")
public void executeBeforeEnteringTestMethod() {
System.out.println("EXECUTE ACTION BEFORE ENTERING TEST METHOD");
}
@After("testMethodEntryPoint()")
public void executeAfterEnteringTestMethod() {
System.out.println("EXECUTE ACTION AFTER ENTERING TEST METHOD");
}
}
Run Code Online (Sandbox Code Playgroud)
非常简单.我想要做的就是在我的测试项目中每次执行任何测试方法之前和之后做一些事情@Test.
现在,我使用aspectj-maven-plugin我的<build>是这样的:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.4</version>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>my.package</groupId>
<artifactId>with-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
<executions>
<execution>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build> …Run Code Online (Sandbox Code Playgroud) 我有一堆JUnit测试,它们都是单独运行的.每一个都是真正的独立单元测试 - 单个测试类.不需要上下文.我可以在Eclipse中或通过maven/surefire-plugin单独或全部运行它们.
我已经添加了一个新的集成测试,它利用了Spring Context等,并使用了SpringJUnit4ClassRunner.只要我将此测试添加到我的套件中,任何测试用例都会在此类失败后运行.
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = IntegrationTestConfiguration.class)
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
@ActiveProfiles("test")
public class ImportServiceIntegrationTest {
...
}
Run Code Online (Sandbox Code Playgroud)
我不确定这有什么价值,但我也在这里发布我的配置类:
@EnableAutoConfiguration(exclude = { WebMvcAutoConfiguration.class,
DispatcherServletAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class,
WebSocketAutoConfiguration.class })
@ComponentScan(basePackages = "com.rtc.synchronize",
excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern="com\\.rtc\\.synchronize\\.config\\.AppConfig"))
@EnableJpaRepositories("com.util.veracode.rtc.synchronize")
@EntityScan("com.util.veracode.rtc.synchronize")
public class IntegrationTestConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
如果我的实际@Configuration课程有用,我也可以发布这些课程,虽然为了简洁,我已经避免了它们(我不完全确定它们会有多大用处).
我怀疑在测试类终止后,JVM中保留了一些东西(一些静态数据).
我使用以下配置使用Spring Cache注释:
@Configuration
@EnableCaching(mode=AdviceMode.ASPECTJ)
public class CacheConfig extends CachingConfigurerSupport{
/**
* EhCache configuration. Used to minimize calls to Veracode
*
* @return
*/
@Bean(destroyMethod="shutdown")
public net.sf.ehcache.CacheManager ehCacheManager() {
...
...
}
...
}
Run Code Online (Sandbox Code Playgroud)
一旦我的集成测试类完成,我的后续测试就会抛出以下错误: …
我正在尝试在Android Studio中配置AspectJ.
但经过一切试验和错误,它不起作用!令人惊讶的是,我能够使用Eclipse Kepler版本
.我遵循Android Studio的步骤
Android项目AspectJ在插件部分搜索
compile 'org.aspectj:aspectjrt:1.8.1'到build.gradle(模块:app)Analytics_onBackPressed.aj 为后退按钮检测而设计Analytics_OnClick.aj为点击事件检测而创建Analytics_onCreate.aj为组件创建事件检测创建*.aj类将在内部调用的必要依赖类我的问题是使AspectJ与Android Studio一起工作所需的更多问题
Eclipse中遵循的步骤使AspectJ正常工作
EclipseKepler版本AspectJ Development Tools (Required).Analytics_onBackPressed.aj 为后退按钮检测而设计Analytics_OnClick.aj为点击事件检测而创建Analytics_onCreate.aj为组件创建事件检测创建
使用环境
Android Studio:2.1.2
JRE:1.8.0
Windows 7企业版
任何帮助都非常感谢! …
aspectj ×10
java ×7
aop ×4
spring ×4
annotations ×2
junit ×2
android ×1
jax-rs ×1
jersey ×1
maven ×1
osgi ×1
spring-aop ×1
spring-el ×1
spring-test ×1