2017-07-07 81 views
2

這裏是我的基本的DAO實現類:如何在Spring Boot中手動配置JdbcTemplate?

@Repository 
public class MeetingDaoImpl implements MeetingDao { 

    @Autowired 
    JdbcTemplate jdbcTemplate; 

    public boolean insertNewMeeting(String initials, String meetingId, int numYears) { 

    int numRowsAffected = jdbcTemplate.update(SQLConstants.INSERT_NEW_MEETING, 
     new Object[] {initials.toLowerCase(), meetingId, numYears}); 

    return numRowsAffected > 0; 

    } 
} 

jdbcTemplate自動從我的application.properties文件,這是偉大的讀取性能spring.datasource,但是它包含了我的數據庫密碼,這是我不想犯。相反,我想從本地server.properties文件中讀取它,而不是從Java類中輕鬆讀取。

有沒有辦法用jdbcTemplate配置Java?我已經看到了多個使用bean和XML的例子,但沒有使用Java。

+1

然後不要犯它......而不是圍繞框架工作,與框架一起工作。在啓動應用程序時提供密碼作爲參數,或者在jar旁邊放置一個'application.properties',或者set作爲一個環境變量。請參閱[參考指南](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html),瞭解如何以及從何處讀取屬性。 –

回答

2

只是聲明瞭一個JdbcTemplate豆:

@Bean 
JdbcTemplate jdbcTemplate() throws IllegalAccessException, InvocationTargetException, InstantiationException { 
    // extract this 4 parameters using your own logic 
    final String driverClassName = "org.h2.Driver"; 
    final String jdbcUrl = "jdbc:h2:mem:test"; 
    final String username = "sa"; 
    final String password = ""; 
    // Build dataSource manually: 
    final Class<?> driverClass = ClassUtils.resolveClassName(driverClassName, this.getClass().getClassLoader()); 
    final Driver driver = (Driver) ClassUtils.getConstructorIfAvailable(driverClass).newInstance(); 
    final DataSource dataSource = new SimpleDriverDataSource(driver, jdbcUrl, username, password); 
    // or using DataSourceBuilder: 
    final DataSource dataSource = DataSourceBuilder.create().driverClassName(driverClassName).url(jdbcUrl).username(username).password(password).build(); 
    // and make the jdbcTemplate 
    return new JdbcTemplate(dataSource); 
} 

另一種方式是不是在你的application.properties文件設置數據源參數,但宣佈它在運行時代替。當您運行應用程序時,您可以覆蓋在application.properties中定義的任何屬性或定義一個新的屬性。

例如:

java -jar my-spring-boot-app.jar --spring.datasource.url=jdbc:h2:mem:test --spring.datasource.username=sa --spring.datasource.password=secret

更爲複雜的方法是使用spring-cloud-config-serverConsul管理您的設置。

+0

我把這個函數放在一個新的類'DatabaseConfig'中,但是如何在我的DAO中實現它? – nicholas79171

+0

在DAO中,只是像之前一樣注入'JdbcTemplate',因爲它應該像以前一樣工作 –

+0

我不得不向主應用程序類添加'@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})'以防止自動配置JDBC模板,但它的工作!謝謝! – nicholas79171

0

你可以給性質象下面這樣:

datasource.local.url= datasource.local.driver-class-name= datasource.local.username= datasource.local.password=

創建一個配置類:

@Configuration 
    @ConfigurationProperties("datasource.local") 
    @EnableJpaRepositories(
     basePackages = "com...", 
     transactionManagerRef = "localTransactionManager", 
     entityManagerFactoryRef = "localEntityManagerFactory" 
    ) 
    public class OracleConfiguration { 
    @NotNull 
    private String username; 
    @NotNull 
    private String password; 

    @NotNull 
    private String url; 

    private String driverClassName; 


    public void setUsername(String username) { 
     this.username = username; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    @Bean 
    DataSource localDataSource() throws SQLException { 

     return DataSourceBuilder 
       .create() 
       .url(this.url) 
       .username(this.username) 
       .password(this.password) 
       .driverClassName(this.driverClassName) 
       .build(); 
    } 

    public String getDriverClassName() { 
     return driverClassName; 
    } 

    public void setDriverClassName(String driverClassName) { 
     this.driverClassName = driverClassName; 
    } 

    @Bean 
    @Autowired 
    public PlatformTransactionManager localTransactionManager(@Qualifier("localEntityManagerFactory") 
                      EntityManagerFactory entityManagerFactory) { 
     return new JpaTransactionManager(entityManagerFactory); 
    } 

    @Bean 
    @Autowired 
    public LocalContainerEntityManagerFactoryBean localEntityManagerFactory(@Qualifier("localDataSource")DataSource dataSource) { 

     HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); 

     LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); 

     factoryBean.setDataSource(dataSource); 
     factoryBean.setJpaVendorAdapter(jpaVendorAdapter); 
     factoryBean.setPackagesToScan("com..."); 

     return factoryBean; 
    } 
    @Bean 
    @Autowired 
    public JdbcTemplate localJdbcTemplate(@Qualifier("localDataSource")DataSource dataSource) { 
      return new JdbcTemplate(dataSource); 
     }} 

在你的主應用程序類,包括這個配置文件:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, 
     DataSourceTransactionManagerAutoConfiguration.class }) 
@EnableTransactionManagement 
@Import({ OracleConfiguration.class}) 
public class MyApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MyApplication.class, args); 
    }} 

然後在你的班級裏你需要注射模板:

@Autowired 
    private JdbcTemplate localJdbcTemplate; 
+0

對於配置數據源來說,這似乎是一項非常多的工作,但沒有更簡單的方法? – nicholas79171

相關問題