我一直在试图用spring实现一个web服务。这个web服务将使用JDBC提供对mySQL数据库的数据访问。我试图不使用任何XML配置文件,所以我遇到了尝试连接到数据库的问题。Spring jdbc配置
我按照教程:http://spring.io/guides/tutorials/rest/,但我一直在改变一些事情。
现在,我正试图实现与数据库的连接,当尝试执行tomcat实例时出现错误,我猜这个问题在配置中。
这里如下一些我的代码:
数据源配置:
@Configuration
@Profile("mySQL")
@PropertySource("classpath:/services.properties")
public class MySQLDataSourceConfiguration implements DataSourceConfiguration{
@Inject
private Environment environment;
@Bean
public DataSource dataSource() throws Exception {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setPassword(environment.getProperty("dataSource.password"));
dataSource.setUrl(environment.getProperty("dataSource.url"));
dataSource.setUsername(environment.getProperty("dataSource.user"));
dataSource.setDriverClassName(environment.getPropertyAsClass("dataSource.driverClass", Driver.class).getName());
return dataSource;
}
}
文件service.properties就是我把我的配置数据库,所以当我渴望更改数据库我会只需要改变4个领域。
为JdbcTemplate的
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:/services.properties")
@Import({ MySQLDataSourceConfiguration.class })
public class JdbcConfiguration {
@Autowired
private DataSourceConfiguration dataSourceConfiguration;
@Inject
private Environment environment;
@Bean
public JdbcTemplate setupJdbcTemplate() throws Exception {
return new JdbcTemplate(dataSourceConfiguration.dataSource());
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) throws Exception {
return new DataSourceTransactionManager(dataSource);
}
}
设置的JDBCConfiguration类再有就是资源库,是临危模板。
private WebApplicationContext createRootContext(ServletContext servletContext) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(CoreConfig.class, SecurityConfig.class, JdbcConfiguration.class);
rootContext.refresh();
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.setInitParameter("defaultHtmlEscape", "true");
return rootContext;
}
然而,Tomcat服务器不会运行,因为它不能自动装配类:
@Transactional
@Repository
@Qualifier("jdbcRepository")
public class JdbcIndividualRepository implements IndividualsRepository{
private static final Logger LOG = LoggerFactory.getLogger(JdbcIndividualRepository.class);
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
public JdbcIndividualRepository(DataSource jdbcDataSource) {
LOG.info("JDBCRepo arg constructor");
this.jdbcTemplate = new JdbcTemplate(jdbcDataSource);
}
@Override
public Individual save(Individual save) {
String sql = "INSERT INTO Individual(idIndividual, Name) VALUES(?,?)";
this.jdbcTemplate.update(sql, save.getId(), save.getName());
return save;
}
@Override
public void delete(String key) {
String sql = "DELETE FROM Individual WHERE idIndividual=?";
jdbcTemplate.update(sql, key);
}
@Override
public Individual findById(String key) {
String sql = "SELECT i.* FROM Individual i WHERE i.idIndividual=?";
return this.jdbcTemplate.queryForObject(sql, new IndividualRowMapper(), key);
}
@Override
public List<Individual> findAll() {
String sql = "SELECT * FROM Individual";
return new LinkedList<Individual>(this.jdbcTemplate.query(sql, new IndividualRowMapper()));
}
}
然后我创建应用程序的根上下文时如下寄存器中初始化类的JDBC配置MySQLDataSourceConfiguration。
任何人都知道问题可能是什么?我可以提供关于代码的更多细节,但问题已经非常大。
感谢任何帮助! 干杯
编辑
解决改变JdbcConfiguration类:
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:/services.properties")
@Import({ MySQLDataSourceConfiguration.class })
public class JdbcConfiguration {
@Autowired
private DataSource dataSource;
@Inject
private Environment environment;
@Bean
public JdbcTemplate setupJdbcTemplate() throws Exception {
return new JdbcTemplate(dataSource);
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) throws Exception {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public IndividualsRepository createRepo(){
return new JdbcIndividualRepository(dataSource);
}
}
可能是你的'JdbcConfiguration'类的层次低于'MySQLDataSourceConfiguration'类。尝试将'JdbcConfiguration'类放置在比MySQLDataSourceConfiguration更高的层次结构中,或者在JdbcConfiguration类中复制'@Bean public DataSource dataSource()'方法。 – Shams
我不喜欢你的进口。我只是在context.register上调用它。但可能你真正的问题是你用@Profile(“mySQL”)标记了MySQLDataSourceConfiguration。尝试添加到您的上下文:rootContext.getEnvironment()。setActiveProfiles(“mySQL”); – Terry
你也可以发布堆栈跟踪(完整的一个)吗?如果您担心帖子的大小,请使用[pastebin.com](http://pastebin.com) –