更新5:我已经根据最新的ToolsSuite下载了最新的Spring。当我将项目作为Maven项目导入时,Eclipse/STS似乎使用Maven目标来构建我的项目。这意味着AspectJ最终在Eclipse中正确工作。
更新4:我最终只使用Maven + AspectJ插件进行编译时编织,有效地绕过了的机制。
更新3:,看来AspectJ的Eclipse插件破坏了Eclipse正确发布到Tomcat的能力。只有通过删除项目上的AspectJ功能,我才能使它再次正确地发布。很烦人。
更新2: --我现在可以在中使用它了。这样说让我非常不舒服,但我不知道如何让它在Eclipse或Maven构建中工作。这似乎是一个编译问题,而不是运行时问题。
更新1:似乎是通过Maven构建实现的,但我不知道如何实现。月食还是不起作用。我在pom.xml中唯一改变的就是添加这些(无关紧要吗?)配置参数:
<source>1.6</source>
<complianceLevel>1.6</complianceLevel>
<verbose>true</verbose>
<showWeaveInfo>true</showWeaveInfo>
<outxml>true</outxml>
实际上,我担心的是,我重复了这个问题,在这里,一切都不一致。我会不断更新这个问题,因为我了解更多。
关于Eclipse,我取得了一些进展,我希望编织的二进制方面--在这里是spring--aspects.jar--并从我的类路径中复制它。然后,我将这个外部jar添加到我的方面路径中。完成此操作后,Eclipse在代码中正确地向我展示了AspectJ标记。令人讨厌的是,我不能把spring-aspects.jar留在我的Java构建路径中,这是Maven通过Maven插件为我维护的。但是,由于某些原因,AspectJ插件不会看到二进制方面,除非它们显式地添加到方面路径中。
原始Post: @Configurable是一个Spring注释,它允许将依赖项注入到Spring外部实例化的对象中(例如,Hibernate或某些工厂类)。
我以前在加载时编织时使用了这个注释,它主要起作用。偶尔,我会启动,什么都不会被注射。这个问题催生了这个StackOverflow问题。答案不多,但大多数建议我尝试编译时编织,因为更高的可靠性。
我为Eclipse安装了AspectJ插件。这两种方法都产生了看起来是正确编译的类。在AspectJ编译之前,我在文本编辑器中打开了其中一个类,没有发现对AspectJ的引用。我在org.aspectj.weaver.MethodDeclarationLineNumber.编译之后打开了它,AspectJ和Maven生成的版本都引用了AspectJ这就是为什么我认为它被正确地编译了。问题是,一旦部署,就不会注入任何依赖项。
我的Spring applicationContext.xml确实包括以下内容:
<context:spring-configured />
<context:component-scan base-package="com.myapp" />
要完成DI,是否需要标记为@Configurable的类所需的所有这些?在从加载时编织到编译时编织的转换过程中,我删除了META/aop.xml,,从我的context.xml中删除了Spring的Tomcat编织器。
我如何进一步调查这个问题?可能的原因是什么?
发布于 2009-05-28 12:49:45
它适用于我们使用编译时编织的maven,尝试添加以下插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerVersion>1.6</compilerVersion>
<fork>true</fork>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<configuration>
<source>1.6</source>
<target>1.6</target>
<verbose>false</verbose>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<configuration>
<source>1.6</source>
<target>1.6</target>
<verbose>false</verbose>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
</plugin>
它作为两个单独的执行步骤完成,允许您为单元测试和编译添加不同的方面库。
您还需要为spring-方面库添加以下依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<scope>compile</scope>
</dependency>
发布于 2009-05-28 08:59:13
我成功地在我的应用程序中配置了加载时编织,如果这是您的另一个选择的话。
我的环境:
配置详细信息:
Spring配置:
<context:annotation-config/>
<context:spring-configured/>
<context:load-time-weaver/>
<bean id="baseEntity" class="package.name.BaseEntity" scope="prototype">
<property name="historyHandler" ref="historyHandler" />
</bean>
<bean id="historyHandler" class="package.name.HistoryJpaHandler" scope="prototype">
<property name="historyDao" ref="historyDao" />
</bean>
<bean id="historyDao" class="package.name.HistoryJpaDao">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Spring注解
@Configurable("baseEntity")
public abstract class BaseEntity
@Configurable("historyHandler")
public class HistoryJpaHandler extends SessionEventAdapter implements HistoryHandler
Java参数
<JAVA_HOME>/bin/java -javaagent:/full/path/to/spring-agent-2.5.6.jar
historyHandler和baseEntitty的实例是由ecliselink创建的。historyHandler在baseEntitty中,historyDao在historyHandler中是通过负载时间编织来设置的.
您可以在Eclipse运行配置中或Tomcatcatalina.sh/bat中设置VM参数。
发布于 2014-03-07 23:09:03
如果您没有为这个注释正确地配置spring,那么生成一个@可配置类的字段就会抛出NullPointerException。按照以下步骤使@可配置注释正常工作
此方法称为AspectJ构建时编织,用于向非spring类注入spring。
第一步是在eclipse中安装这些插件:
从这两个更新站点安装eclipse建议的任何内容:
http://download.eclipse.org/tools/ajdt/43/update
http://dist.springsource.org/release/AJDT/configurator/
安装后,右键单击项目并执行:
Configure > Convert to Aspectj
Maven > Update
接下来,您需要将这些添加到您的pom.xml中:
依赖项下的添加:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
插件下的添加:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.5</version>
<configuration>
<showWeaveInfo>true</showWeaveInfo>
<source>1.7</source>
<target>1.7</target>
<Xlint>ignore</Xlint>
<complianceLevel>1.7</complianceLevel>
<encoding>UTF-8</encoding>
<verbose>false</verbose>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
</plugin>
重要:不使用<build>
标记下的任何<pluginManagment>
标记。您的pom.xml需要如下所示:
<project ....>
....
<dependencies>
<dependency>
....
</dependency>
....
</dependencies>
<build>
<plugins>
<plugin>
....
</plugin>
....
</plugins>
</build>
</project>
最后,将<context:spring-configured />
添加到春季应用程序上下文配置文件中。
现在,您可以将POJO类注释为@Configurable
,并使用@Autowired
注释在其中注入spring。这样,每当您创建POJO的新实例时,就会自动配置它(例如,注入依赖项)。
https://stackoverflow.com/questions/901632
复制相似问题