Spring 3 使用 @Configurable 解决 jar 依赖中对象的编译时编织问题(使用 Maven)
现在已经在谷歌上搜索并致力于此太久了。也看过各种 stackoverflow 帖子,但仍然对这里发生的事情感到困惑。
首先,我想要什么:
我有一个持久性 jar,用作我的 Web 项目中的依赖项。在此持久性 jar 中,使用 Web 项目中的 spring 配置可以很好地设置 daos。 但是,我现在想做的是在基类(抽象)中,我希望能够注入在 String 上设置的属性,但扩展此抽象类的类不能通过 Spring 直接控制(例如通过 new MyImp() 实例化。)
从我收集到的所有信息来看,我需要利用 @Configurable。
奇怪的是,代码全部编译(使用 Maven 使用方面插件),我确实认为必须进行一些编织,因为对扩展 @Configurable 抽象类的对象的调用似乎陷入了“黑洞” - 还没有错误甚至没有任何东西可以通过旧的 skool System.out.print 语句打印到系统???真的很奇怪。
下面我认为是我如何设置的相关信息...(显然没有显示所有内容):
Web 项目 spring config:
<util:properties id="props" location="classpath:application.properties"/>
<context:annotation-config />
<context:spring-configured/>
<context:component-scan base-package="com.foo" />
<bean class="com.foo.MyAbstractClass" abstract="true" scope="prototype">
<property name="xlsDir" value="${xlsDir}"/>
</bean>
//some DAOs are injected with datasources..not shown. Props being set just fine for the
//datasources from application.properties, and the DAOs will work fine
上述 Web 项目使用的 jar (包含 MyAbstractClass 及其后代)没有任何 XML 。各种文件扩展 MyAbstractClass 并通过 new 在应用程序中创建: MyImp imp = new MyImp(); imp.bar();
MyAbstractClass 相关信息:
@Configurable
public abstract class MyAbstractClass {
private String xlsDir;
public void setXlsDir(String xlsDir) {
this.xlsDir = xlsDir;
}
public void bar() {
System.out.println("this won't even get printed, yet no errors!");
System.out.println("xlsDir is "+xlsDir);
}
}
我可以稍后使用 @Autowiring 并使用 @Value (这是我第一次尝试的),但现在我什至不确定编织是否正常工作。问题是否可能是持久性 jar 首先通过 maven (带有编织)编译 - 但它不知道 xlsDir 的 setter 是什么,直到稍后基于 Web 项目?但这并不能解释为什么对 bar() 的调用会消失——所以有些事情正在发生。
对于这两个项目,我都根据我看到的 Spring Roo 的 pom 行为设置了 Maven 进行编译(在网上确定这个 pom 中真正需要的内容是非常困难的,以便与 spring 进行 Maven 方面的编织。)
这里是相关的 pom 信息(左)下面是 spring roo 的评论 - 它们不是我的):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.2</version> <!-- NB: do use 1.3 or 1.3.x due to MASPECTJ-90 - wait for 1.4 -->
<dependencies>
<!-- NB: You must use Maven 2.0.9 or above or these are ignored (see
MNG-2972) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
非常感谢任何帮助。我很快就会放弃,只需将我的属性文件加载到静态块中即可完成:)
Been googling and working on this for way too long now. Been over various stackoverflow posts as well, but still stumped on what's going on here.
First off, what I want:
I have a persistence jar that is used as dependency in my web project. Within this persistence jar, daos are set up just fine using the spring config from the web project. What I want to do now, though, is in a base class (abstract) I want to be able to inject a property set on a String yet the classes that extends this abstract class aren't directly controlled via Spring (eg instantiated via new MyImp().)
From everything I gather, I'll need to leverage using @Configurable.
The odd thing is, the code all compiles (with Maven using aspects plugin) and I do think some weaving must be taking place because calls to the objects extending the @Configurable abstract class seem to fall into a "black hole" - no errors yet nothing even can be printed to the system via old skool System.out.print statements??? Really odd.
Below I think is the relevant information of how I have things set up...(obviously not showing everything):
Web project spring config:
<util:properties id="props" location="classpath:application.properties"/>
<context:annotation-config />
<context:spring-configured/>
<context:component-scan base-package="com.foo" />
<bean class="com.foo.MyAbstractClass" abstract="true" scope="prototype">
<property name="xlsDir" value="${xlsDir}"/>
</bean>
//some DAOs are injected with datasources..not shown. Props being set just fine for the
//datasources from application.properties, and the DAOs will work fine
The jar used by the above web project (that holds MyAbstractClass and its descendants) does not have any XML. Various files extends MyAbstractClass and are created within the application via new:
MyImp imp = new MyImp();
imp.bar();
MyAbstractClass relevant info:
@Configurable
public abstract class MyAbstractClass {
private String xlsDir;
public void setXlsDir(String xlsDir) {
this.xlsDir = xlsDir;
}
public void bar() {
System.out.println("this won't even get printed, yet no errors!");
System.out.println("xlsDir is "+xlsDir);
}
}
I can play around with @Autowiring later and using @Value (which is what I first tried anyway), but right now I'm not even sure weaving is working correctly. Is the issue maybe that the persistence jar is compiled up first via maven (with weaving) - yet it doesn't know what the setter for xlsDir is until later on based on the web project ? That wouldn't explain though why calls to bar() just disappear though - so something's going on.
For both projects I've set up maven to compile based on what I saw Spring Roo's pom doing (It's extremely difficult nailing down online what really needs to be in this pom for maven aspect weaving with spring .)
Here is relevant pom info (left spring roo's comments in below - they aren't mine):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.2</version> <!-- NB: do use 1.3 or 1.3.x due to MASPECTJ-90 - wait for 1.4 -->
<dependencies>
<!-- NB: You must use Maven 2.0.9 or above or these are ignored (see
MNG-2972) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
Any help much appreciated. I'm about to give up soon and just load my properties file in a static block and be done with it:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该 bean 定义
如果没有在其他 bean 定义中用作父代,则 不会执行任何操作。如果您希望 @Configurable bean 成为自动装配的,请使用 @Configurable(autowire=Autowire.BY_NAME) 并声明一个 name="xlsDir" 的 String bean
The bean definition
does nothing if not used as parent in other beans definitions. If you want that the @Configurable beans become autowired use @Configurable(autowire=Autowire.BY_NAME) and declare a String bean with name="xlsDir"