@ Beans configuráveis não funcionam com JPA-EntityListeners no Spring Boot
Estou tendo um problema estranho com um ouvinte jpa-entity personalizado que criei em um aplicativo de inicialização do Spring. Estou tentando usar molas@Configurable
mecanismo para configurar o EntityListener (como visto em SpringsAuditingEntityListener
), mas o Spring se recusa a reconhecer meu ouvinte assim que é usado no@EntityListeners
-Anotação em uma entidade jpa. se não estiver configurado em uma entidade jpa, o Ouvinte será conectado / configurado pelo Spring como deveria.
Eu criei um projeto de exemplo com um teste de junção para demonstrar o problema: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);
}
}
O 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();
}
}
O Bean que eu quero que seja injetado no 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;
}
A entidade em que o ouvinte deve ser usado:
/**
* 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;
}
E, finalmente, o teste que mostra que a fiação não está funcionando assim que o Ouvinte é usado:
@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);
}
}
O teste de junção (a fiação do EntityListener / ManagedBean) falha assim que a anotação@EntityListeners({UnmanagedBean.class})
noFooEntity
está ativado.
Isso é um bug ou eu perdi outra coisa?
Para executar o teste, você precisa usar-javaagent:spring-instrument-4.1.6.RELEASE.jar
na linha de comando e forneça o arquivo jar no diretório de trabalho.
Esta é a versão "condensada" de uma pergunta que fiz anteriormente:@Configurable não reconhecido no aplicativo SpringBoot