2017-03-07 79 views
0

我把數據庫配置的配置類中:什麼是配置數據庫連接的好方法?

@Configuration 
@ComponentScan("com.ambre.pta") 
@EnableTransactionManagement 
@PropertySources({ 
    @PropertySource("classpath:fr/referentiel.properties") 
}) 
public class ApplicationContextConfig { 

    @Autowired 
    private Environment env; 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer properties() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 

    @Bean(name = "viewResolver") 
    public InternalResourceViewResolver getViewResolver() { 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setPrefix("/WEB-INF/views/"); 
     viewResolver.setSuffix(".jsp"); 
     return viewResolver; 
    } 

    @Bean(name = "dataSource") 
    public DataSource getDataSource() { 

     BasicDataSource dataSource = new BasicDataSource(); 

     dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); 
     dataSource.setUrl("jdbc:oracle:thin:@192.168.1.123:1521:xe"); 
     dataSource.setUsername("pta"); 
     dataSource.setPassword("pta"); 

     return dataSource; 

    } 

    ... 

} 

的問題是,每次該項目將被交付給不同的客戶,則開發人員必須修改這些CONFIGS和重建項目,並最終regenrate戰爭文件。

那麼是否有簡單的程序來更改數據庫配置而不重建或重新生成war文件?

+0

使用該 – Jens

+0

您可以使用屬性春天輪廓。您可以爲「dev」設置配置文件,爲「生產」設置另一個配置文件。 https://spring.io/blog/2011/02/14/spring-3-1-m1-introducing-profile/ –

回答

0

您可以將數據庫配置移動到外部屬性中。

添加這樣的事情

@PropertySources({ 
    @PropertySource("classpath:fr/referentiel.properties") 
    @PropertySource("${db.properties}") 
}) 

,然後當你運行你的應用程序只需添加此參數(通知「文件:」前綴)

-Ddb.properties=file:/path-to.properties 

您可以在Access數據庫的屬性您像這樣的配置

@Autowired 
private Environment env; 

... 

dataSource.setDriverClassName(env.getProperty("db.driver")); 
dataSource.setUrl(env.getProperty("db.url")); 
dataSource.setUsername(env.getProperty("db.username")); 
dataSource.setPassword(env.getProperty("db.password")); 

或與@Value註釋,例如

@Value("${db.driver}") 
private String dbDriver; 

屬性文件path-to.properties應該只是一個常規的屬性文件。所以

db.driver=oracle.jdbc.driver.OracleDriver 
db.url=jdbc:oracle:thin:@192.168.1.123:1521:xe 
db.username=pta 
db.password=pta 
0

我建議你使用Spring開機,它提供了開箱即用的模板項目。另外,在Spring啓動應用程序中設置數據庫連接非常簡單。這是我目前使用的兩種方法。

  1. 如果您在內存數據庫要的,你可以只包括H2/HSQLDB/DERBY依賴於你的pom.xml文件,你是好去。 Spring Boot會在您的pom中看到這些依賴關係,併爲您創建一個開箱即用的數據源。這些數據庫的一個缺點是它們不提供持久性存儲。

  2. 如果你想要一個持久性存儲,比如PostgreSQL,你可以在你的pom.xml文件中包含這個依賴關係,並在你的/ src/main/resources文件夾中維護一個application.properties文件。 例如,如果你有一個PostgreSQL數據庫track-courier端口5432本地機器上運行,該文件的內容將類似於下面

    spring.datasource.url=jdbc:postgresql://localhost:5432/track-courier 
    spring.datasource.username=postgres 
    spring.datasource.password=postgres 
    spring.jpa.hibernate.ddl-auto=update 
    

    而且你也不需要手動創建的任何數據源豆在配置類中。春季開機照顧那個開箱即用。您可以查看this鏈接以供參考。

0

您可以使用數據源連接到數據庫。 下面是使用數據源連接數據庫的示例代碼。

@Bean 
    public DataSource dataSource_aw_es() { 
     DataSource dataSource = null; 
     Context ctx = null; 
     try { 
      ctx = new InitialContext(); 
      dataSource = (DataSource) ctx.lookup("java:/comp/env/jdbc/DataSourcename"); 
     } catch (NamingException e) { 
     } 
     return dataSource; 
    } 

您只需要在服務器中配置數據源。 最重要的是,不需要共享數據庫憑證以連接到數據庫

下面是在服務器上的數據源配置。(下面configuartion是爲Tomcat server.We需要在Tomcat服務器的context.xml配置如下片段。)

<Resource name="jdbc/DataSourceName" auth="Container" type="javax.sql.DataSource" 
       maxActive="-1" maxIdle="-1" maxWait="-1" 
       username="user" password="password" 
       driverClassName="com.mysql.jdbc.Driver" 
       url="jdbc:mysql://IP_DB:7011/Schema_name"/> 
相關問題