【问题标题】:Spring 3 Compile Time Weaving Problems for Objects in jar dependency with @Configurable (using Maven)使用@Configurable(使用Maven)的jar依赖中的对象的Spring 3编译时编织问题
【发布时间】:2012-03-08 02:13:10
【问题描述】:

现在在谷歌上搜索和研究这个问题已经太久了。也浏览过各种 stackoverflow 帖子,但仍然对这里发生的事情感到困惑。

首先,我想要什么:

我有一个持久性 jar,在我的 Web 项目中用作依赖项。在这个持久化 jar 中,使用 web 项目中的 spring 配置可以很好地设置 daos。 我现在想做的是在一个基类(抽象)中我希望能够在 String 上注入一个属性集,但扩展这个抽象类的类不是通过 Spring 直接控制的(例如通过 new MyImp() 实例化。)

从我收集的所有内容中,我需要使用 @Configurable

奇怪的是,代码全部编译(使用 Maven 使用方面插件),我确实认为必须进行一些编织,因为对扩展 @Configurable 抽象类的对象的调用似乎落入了“黑洞” -没有错误,甚至没有任何东西可以通过 old 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 的设置器是什么?这并不能解释为什么对 bar() 的调用只是消失了——所以发生了一些事情。

对于这两个项目,我都设置了 maven,以根据我看到 Spring Roo 的 pom 所做的事情进行编译(很难在网上确定这个 pom 中真正需要的东西,以便用 spring 编织 maven 方面。)

这里是相关的 pom 信息(下面是 spring roo 的 cmets - 它们不是我的):

<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>

非常感谢任何帮助。我即将放弃,只需将我的属性文件加载到静态块中并完成它:)

【问题讨论】:

    标签: spring new-operator configurable aspects compile-time-weaving


    【解决方案1】:

    bean 定义

    <bean class="com.foo.MyAbstractClass" abstract="true" scope="prototype">
        <property name="xlsDir" value="${xlsDir}"/>
    </bean> 
    

    如果不在其他 bean 定义中用作父级,则不执行任何操作。如果您希望 @Configurable bean 自动装配,请使用 @Configurable(autowire=Autowire.BY_NAME) 并声明一个带有 name="xlsDir" 的 String bean

    <bean id="xlsDir" class="java.lang.String" factory-method="valueOf">
        <constructor-arg value="${xlsDir}"/>
     </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2011-11-17
      • 2021-04-06
      • 2018-03-03
      • 2018-01-22
      • 1970-01-01
      相关资源
      最近更新 更多