更改@Resource
(来自javax.annotation)并使其例如:@Autowired
public
@Configuration
@PropertySource("classpath:database.properties")
public class HibernateConfigurer {
@Resource
public Environment env;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("database.driverClassName"));
dataSource.setUrl(env.getProperty("database.url"));
dataSource.setUsername(env.getProperty("database.username"));
dataSource.setPassword(env.getProperty("database.password"));
dataSource.setValidationQuery(env.getProperty("database.validationQuery"));
return dataSource;
}
}
而且您必须以这种方式在 WebApplicationInitializer 中注册您的配置器类
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(ApplicationConfigurer.class); //ApplicationConfigurer imports HibernateConfigurer
它为我工作!你可能想检查我做的一个测试项目。