Results 1 to 2 of 2

Thread: Using @Configurable on a domain object not working

  1. #1
    Join Date
    Dec 2012
    Posts
    11

    Default Using @Configurable on a domain object not working

    I'm trying to use @Configurable with AspectJ at compile time in Maven to inject dependencies into JPA entities created by Hibernate in a JBoss environment. Dependency injection on these works perfectly with the new operator but anything created by Hibernate does not get injected.

    I have a parent entity.

    Code:
    @Entity
    @Inheritance(strategy = JOINED)
    @Configurable
    public abstract class Foo {
    
        @Id
        @GeneratedValue
        private Long id;
        private String aField;
    
    }
    A subclass with a dependency.

    Code:
    @Entity
    @Configurable
    public class Bar extends Foo {
    
       private String anotherField;
    
       @Transient
       private TaskScheduler dependency;
    
       @Autowired
       public void setDependency(final TaskScheduler dependency) {
            this.dependency = dependency;
        }
    
    }
    I have an application context with component-scan and spring-configured.

    Code:
        <!-- Scan for annotated beans -->
        <context:component-scan base-package="my.foo.bar"/>
    
        <!-- Scan for non-managed classes with @Configurable -->
        <context:spring-configured/>
    The JPA implementation is Hibernate 4 and comes with JBoss AS 7 which we lookup through JNDI.

    Code:
        <!-- Lookup a container managed EMF -->
        <jee:jndi-lookup id="entityManagerFactory"
                         jndi-name="java:jboss/entityManagerFactory"
                         expected-type="javax.persistence.EntityManagerFactory"/>
    And I have Maven setup to compile with AspectJ. Java.version is 1.7.

    Code:
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.4</version>
                    <configuration>
                        <complianceLevel>${java.version}</complianceLevel>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <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.1</version>
                        </dependency>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>1.7.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
    Again, this works perfectly with the new operator, but does not work at all with entities created by Hibernate. So the dependency is there and can be configured, Spring is picking up the @Configurable and working fine, there must be something different with how Hibernate instantiates that isn't getting picked up.

    Any ideas?

  2. #2
    Join Date
    Dec 2012
    Posts
    11

    Default

    I've discovered the issue. The particular entities in question were being loaded as part of a 'startup' job that was not waiting for the application context to finish initializing. Thus they were being created before the AnnotationBeanConfigurerAspect had been initialized and configured. Forcing the entire process to wait until after the context has finished loading fixes the issue.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •