在其他地方,所有@Autowired things
都可以正常工作(dao
中的services
,service
中的controller
,但在一个组件中则不然。 service package
有Abstract class
和package
实施。内部实现类@Autowired service
为空,但@Autowired service
中的controller
工作正常。
对于测试,我将class
与Abstract class
分开,但问题并没有消失。始终创建所有bean。
package com.mp3.tagger.service.middleware;
@Component(value = "localMatadataMiddleware")
public class LocalMatadataMiddleware extends Middleware/*Abstract class*/ {
@Autowired
private ArtistService artistService;
@Override
public Artist findMetadata(String artistName, String trackName) {
List<Artist> artist = artistService.findAll();//is null
//another code...
}
}
配置:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "com.mp3.tagger" })
@EnableJpaRepositories(basePackages = "com.mp3.tagger.persistence.dao")
public class PersistenceJPAConfig {
@Autowired
private Environment env;
public PersistenceJPAConfig() {
super();
}
// beans
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan(new String[] { "com.mp3.tagger.persistence.entity" });
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
return hibernateProperties;
}
我使用Intellij Idea 2017.3.3
UPD:
我尝试实施模式 - 责任链并创建实例
Middleware middleware = new LocalMatadataMiddleware();
middleware.findMetadata("Radiohead", "Fake Plastic Trees");
抽象类:
public abstract class Middleware {
private Middleware next;
public Middleware linkWith(Middleware next) {
this.next = next;
return next;
}
//Main executable method of chain of responsibility
public abstract Artist findMetadata(String artistName, String songName);
public boolean checkIsCorrectAnswer(Artist artist, String artistName, String songName) {
if (artist.getName().equals(artistName) || artist.getTrack().stream().anyMatch(e -> e.getTitle().equals(songName))) {
return true;
}
return false;
}
protected Artist checkNext(String artistName, String songName) {
if (next == null) {
return new Artist();
}
return next.findMetadata(artistName, songName);
}
}
服务:
@Service
@Transactional
public class ArtistService {
@Autowired
private ArtistDao artistDao;
public ArtistService() {
super();
}
public void createArtist(final Artist entity){
artistDao.create(entity);
}
public void updateArtist(Artist entity){
artistDao.update(entity);
}
public Artist findOne(long id){
return artistDao.findOne(id);
}
public List<Artist> findAll(){
return artistDao.findAll();
}
public Artist findTrackById(long id){
return artistDao.findOne(id);
}
public void delete(Artist entity){
artistDao.delete(entity);
}
public void deleteById(long id){
artistDao.deleteById(id);
}
}