0
我試圖在DB瑪添加一些實體爲什麼在Spring容器中不會調用@PostConstruct?
配置:
@Configuration
@ComponentScan(ApplicationConfig.basePackage)
public class ApplicationConfig {
public final static String basePackage = "test"
}
spring容器調用:
public class StartApp {
public static void main(String... args) throws Exception{
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
TestEntityRepository repository = (TestEntityRepository) context.getBean("testEntityRepository");
repository.save(new TestEntity("test"));
}
}
目標類註釋:
public class PersistenceService {
@Autowired
TestEntityRepository testEntityRepository;
@PostConstruct
public void initialize(){
//repository.deleteAll();
testEntityRepository.save(new TestEntity("test1"));
testEntityRepository.save(new TestEntity("test2"));
testEntityRepository.save(new TestEntity("test3"));
}
}
爲導致表中只有一條記錄 - 「測試」。在Tomcat中一切正常。
https://github.com/GlebSa/TestSpringJPA
由於您似乎正在使用內存中的嵌入式數據庫,您如何知道數據庫只包含一條記錄? –