@Configurable-Beans not working with JPA-EntityListeners in Spring Boot
Asked Answered
E

0

6

I am having a strange problem with a custom jpa-entity listener I've created in a spring boot application. I'm trying to use Springs @Configurable mechanism to configure the EntityListener (as seen in Springs AuditingEntityListener) but Spring refuses to recognize my Listener as soon as it is used in the @EntityListeners-Annotation on a jpa entity. if it is not configured on a jpa entity, the Listener gets wired/configured by Spring as it should.

I've created an example project with a junit-test to demonstrate the problem: https://github.com/chrisi/aopconfig/find/master

@SpringBootApplication
@EnableSpringConfigured
@EnableLoadTimeWeaving
public class Application {

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }
}

The EntityListener:

/**
 * This bean will NOT be instanciated by Spring but it should be configured by Spring
 * because of the {@link Configurable}-Annotation.
 * <p>
 * The configuration only works if the <code>UnmanagedBean</code> is not used as an <code>EntityListener</code>
 * via the {@link javax.persistence.EntityListeners}-Annotation.
 *
 * @see FooEntity
 */
@Configurable
public class UnmanagedBean {

  @Autowired
  private ManagedBean bean;

  public int getValue() {
    return bean.getValue();
  }
}

The Bean I want to be injected in the EntityListener/UnmanagedBean:

/**
 * This bean will be instanciated/managed by Spring and will be injected into the
 * {@link UnmanagedBean} in the case the <code>UnmanagedBean</code> is not used as an JPA-EntityListener.
 */
@Component
@Data
public class ManagedBean {
  private int value = 42;
}

The Entity where the Listener should be used:

/**
 * This simple entity's only purpose is to demonstrate that as soon as
 * it is annotated with <code>@EntityListeners({UnmanagedBean.class})</code>
 * springs configurable mechanism will not longer work on the {@link UnmanagedBean}
 * and therefore the <code>ConfigurableTest.testConfigureUnmanagedBean()</code> fails.
 */
@Entity
@EntityListeners({UnmanagedBean.class}) // uncomment to make the test fail
public class FooEntity {

  @Id
  private Long id;

  private String bar;
}

And finally the test that shows that the wiring is not working as soon as the Listener is used:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class ConfigurableTest {

  /**
   * This test checks if the ManagedBean was injected into the UnmanagedBean
   * by Spring after it was created with <code>new</code>
   */
  @Test
  public void testConfigureUnmanagedBean() {
    UnmanagedBean edo = new UnmanagedBean();
    int val = edo.getValue();
    Assert.assertEquals(42, val);
  }
}

The junit-test (the wiring of the EntityListener/ManagedBean) fails as soon as the annotation @EntityListeners({UnmanagedBean.class}) in FooEntity is activated.

Is this a bug or did I miss something else?

In order to run the test you have to use -javaagent:spring-instrument-4.1.6.RELEASE.jar on the commandline an provide the jar file in the working directory.

This is the "condensed" version of a question I asked earlier: @Configurable not recognized in SpringBoot Application

Encephalogram answered 3/7, 2015 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.