@ Configurable-Beans doesn't work with JPA-EntityListeners in Spring Boot

I am having a weird problem with a custom jpa-entity listener that I created in a spring boot application. I'm trying to use the Springs mechanism @Configurable

to set up an EntityListener (as shown in Springs AuditingEntityListener

), but spring refuses to recognize my listener as soon as it is used in @EntityListeners

-Annotation on the jpa object, if it is not configured on the jpa object, the Listener gets wired / set up with spring. as it should be.

I created an example project with 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);
  }
}

      

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();
  }
}

      

Bean I want to be injected into an 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 object in which the receiver should be used:

/**
 * This simple entity 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, a test that shows that the wiring doesn't work 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);
  }
}

      

Junit test (EntityListener / ManagedBean wiring) fails abstract @EntityListeners({UnmanagedBean.class})

in FooEntity

activated.

Is this a bug or am I missing something else?

To run the test, you must use -javaagent:spring-instrument-4.1.6.RELEASE.jar

the command line and provide a jar file in your working directory.

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

+3
spring spring-data-jpa jpa entitylisteners


source to share


No one has answered this question yet

See similar questions:

2
@Configurable not recognized in SpringBoot application

or similar:

708
How to configure port for Spring Boot application
565
What is the difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
272
Injecting Mockito mocks Spring bean
42
Spring dependency injection into EntityListener JPA
6
How to configure JPA EntityListener via Spring XML config file?
3
Add JPA EntityListener / Spring Data AuditingEntityListener Programmatically
1
Injecting Spring Managed Beans into JPA entity listeners like Spring Data JPA with its own AuditingEntityListener
1
Spring bean injection in JPA entity receiver
0
JPA @EntityListener not working as expected
-1
@EntityListeners configured from container



All Articles
Loading...
X
Show
Funny
Dev
Pics