2

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

Rick Reumann
  • 383
  • 1
  • 3
  • 10

1 Answers1

0

The bean definition

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

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"

<bean id="xlsDir" class="java.lang.String" factory-method="valueOf">
    <constructor-arg value="${xlsDir}"/>
 </bean>
Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38