现在谷歌上搜索这个问题已经太久了。已经过了各种stackoverflow帖子,但仍然难以理解这里发生的事情。
首先,我想要的是什么:
我有一个持久性jar,在我的web项目中用作依赖项。在这个持久性jar中,使用web项目中的spring配置设置了daos。 我现在要做的是在基类(抽象)中我希望能够在String上注入属性集但是扩展此抽象类的类不是通过Spring直接控制的(例如通过新的MyImp()实例化。)
从我收集的所有内容中,我需要使用 @Configurable 。
奇怪的是,代码全部编译(使用方面插件Maven)我确实认为必须进行一些编织,因为调用扩展@Configurable抽象类的对象似乎陷入了“黑洞” - 没有错误,甚至没有任何东西甚至可以通过旧的skool System.out.print语句打印到系统???真奇怪。
下面我想是有关我如何设置事物的相关信息......(显然没有显示所有内容):
Web项目spring配置:
<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项目(包含MyAbstractClass及其后代)使用的jar没有任何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编译(编织) - 但是直到后来基于web项目它才知道xlsDir的setter是什么?这并不能解释为什么对bar()的调用只是消失了 - 所以有些事情正在发生。
对于这两个项目,我已经根据我看到的Spring Roo的pom做了设置maven进行编译(这非常难以在网上确定真正需要在这个pom中使用spring编织的maven方面。)
这是相关的pom信息(左下方春天的评论如下 - 他们不是我的):
<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>
任何帮助非常感谢。我即将放弃,只需在静态块中加载我的属性文件,然后用它完成:)
答案 0 :(得分:0)
bean定义
<bean class="com.foo.MyAbstractClass" abstract="true" scope="prototype">
<property name="xlsDir" value="${xlsDir}"/>
</bean>
如果没有在其他bean定义中用作父项,则不执行任何操作。如果你想让@Configurable bean成为自动装配的,可以使用@Configurable(autowire = Autowire.BY_NAME)并声明一个名为=&#34; xlsDir&#34;的String bean。
<bean id="xlsDir" class="java.lang.String" factory-method="valueOf">
<constructor-arg value="${xlsDir}"/>
</bean>