@ Configurable-Beans не работает с JPA-EntityListeners в Spring Boot
У меня странная проблема с пользовательским слушателем jpa-entity, который я создал в приложении весенней загрузки. Я пытаюсь использовать пружины@Configurable
механизм для настройки EntityListener (как видно из SpringsAuditingEntityListener
) но Spring отказывается узнавать моего Слушателя, как только он используется в@EntityListeners
-Аннотация на jpa сущности. если он не настроен на объекте jpa, прослушиватель подключается / настраивается Spring как следует.
Я создал пример проекта с тестом junit для демонстрации проблемы: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();
}
}
Бин, который я хочу добавить в 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;
}
Сущность, в которой должен использоваться Слушатель:
/**
* 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;
}
И, наконец, тест, который показывает, что проводка не работает, как только прослушиватель используется:
@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 (проводка EntityListener / ManagedBean) завершается неудачно, как только аннотация@EntityListeners({UnmanagedBean.class})
вFooEntity
активирован.
Это ошибка или я что-то упустил?
Для запуска теста вы должны использовать-javaagent:spring-instrument-4.1.6.RELEASE.jar
в командной строке укажите файл JAR в рабочем каталоге.
Это «сжатая» версия вопроса, который я задавал ранее:@Configurable не распознается в приложении SpringBoot