2014-06-24 75 views
0

我一直在试图用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); 
    } 
} 
+0

可能是你的'JdbcConfiguration'类的层次低于'MySQLDataSourceConfiguration'类。尝试将'JdbcConfiguration'类放置在比MySQLDataSourceConfiguration更高的层次结构中,或者在JdbcConfiguration类中复制'@Bean public DataSource dataSource()'方法。 – Shams

+0

我不喜欢你的进口。我只是在context.register上调用它。但可能你真正的问题是你用@Profile(“mySQL”)标记了MySQLDataSourceConfiguration。尝试添加到您的上下文:rootContext.getEnvironment()。setActiveProfiles(“mySQL”); – Terry

+0

你也可以发布堆栈跟踪(完整的一个)吗?如果您担心帖子的大小,请使用[pastebin.com](http://pastebin.com) –

回答

1

删除

@Autowired 
private DataSourceConfiguration dataSourceConfiguration; 

因为这不是它应该如何使用。相反,添加到同一类中的以下内容:

@Autowired DataSource dataSource; 

,并使用它是这样的:new JdbcTemplate(dataSource);

同时,尽量增加@ComponentScan到JdbcConfiguration类。从我在代码中看到的JdbcIndividualRepository类没有被任何东西拾取。

+0

最重要的是,我还缺少一个包含存储库的Bean。感谢您的帮助! – Noronha

+0

很高兴能有所帮助。 –

0

在你JdbcConfiguration类,你想自动装配DataSourceConfiguration。我不太确定这是否可能 - 通常您应该尝试联系DataSource,而不是DataSourceConfiguration

@Import({ MySQLDataSourceConfiguration.class }) 
public class JdbcConfiguration { 

    @Autowired 
    private DataSource dataSource; 

    @Bean 
    public JdbcTemplate setupJdbcTemplate() throws Exception { 
     return new JdbcTemplate(dataSource); 
    } 

此外,如果你有几个DataSource S和你使用Spring配置文件将它们分开,很容易提供的所有DataSource豆在一个文件中,并用不同的配置文件注释每个bean:

@Configuration 
public class DataSourceConfig { 

    @Bean 
    @Profile("Test") 
    public DataSource devDataSource() { 
     .... configure data source 
    } 

    @Bean 
    @Profile("Prod") 
    public DataSource prodDataSource() { 
     ... configure data source 
    } 
+0

是的,我想使用配置文件,因为我最终会有几个数据源,但我现在可以删除它们。 但是,导入配置类和AAutowiring DataSource并不能解决问题。感谢您的答复! – Noronha