2016-04-25 17 views
0

Ich benutze Spring Boot zum ersten Mal und benutze auch XML less Config.Spring-Eigenschaften werden nicht geladen

Versuch Eigenschaften Datei Wert in meiner Bean-Klasse hinzufügen, aber null Werte zu bekommen. Hier ist mein Code:

config.java:

import java.sql.SQLException; 

import javax.annotation.PostConstruct; 
import javax.sql.DataSource; 

import oracle.jdbc.pool.OracleDataSource; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.context.properties.EnableConfigurationProperties; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 
import org.apache.commons.dbcp2.BasicDataSource; 
import org.springframework.stereotype.Component; 

@Configuration 
@ComponentScan 
@PropertySource("classpath:application.properties") 
public class Config { 

    @Autowired 
    private static SpringAppProp springAppProp; 





    @Value("${url}") 
    private static String url; 

    @Value("${driverClassName}") 
    private static String driverClassName; 

    @Value("${username}") 
    private static String username; 

    @Value("${password}") 
    private static String password; 

    @Value("${initialSize}") 
    private static int initialSize; 

    @Value("${maxActive}") 
    private static int maxActive; 

    @Value("${dbPort}") 
    private static int dbPort; 

    @Value("${dbServiceName}") 
    private static String dbServiceName; 

    @Value("${dbServer}") 
    private static String dbServer; 



    @Bean 
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() { 
     return new PropertySourcesPlaceholderConfigurer(); 
    } 
    @Bean(name = "DataSource") 
    public static BasicDataSource dataSource() throws SQLException { 
     BasicDataSource basicDataSource = new BasicDataSource(); 
     basicDataSource.setUrl(url); 
     basicDataSource.setDriverClassName(driverClassName); 
     basicDataSource.setInitialSize(initialSize); 
     basicDataSource.setMaxTotal(maxActive); 
     basicDataSource.setMaxIdle(5); 
     basicDataSource.setMinIdle(0); 
     basicDataSource.setMaxWaitMillis(15000); 
     return basicDataSource; 
     } 

} 

application.properties:

server.port=8080 
driverClassName=oracle.jdbc.driver.OracleDriver 
username=XXXX 
password=XXXX 
initialSize=10 
maxActive=20 
dbPort=XXX 
dbServiceName=xxxx 
dbServer=xxxxxx 
url=jdbc:oracle:[email protected]//xxxxx 

Kann jemand sehen und lassen Sie mich wissen, was ich falsch hier tue ..

+2

Zuerst fallen die statische, dann entfernen Sie Ihre ganze Klasse. Sie verwenden Spring Boot, das bereits eine 'DataSource' konfiguriert und bereits' application.properties' liest. Arbeite mit dem Framework anstatt um es herum. –

+0

Wie mache ich das .. Kannst du mir einen Arbeitscode geben .. Ich würde auch brauchen, um andere Eigenschaften außer Datenquelle zu lesen –

Antwort

2

Löschen Sie die static s aus den Feldern, die Sie mit Value:

annotiert haben

Und da Sie Spring Boot verwenden, ist @PropertySource nicht erforderlich.

aktualisieren Als M. Deinum sagte, es ist besser, Ihre Konfigurationsklasse ganz fallen zu lassen und spring.datasource.* Eigenschaften in Ihrem application.properties verwenden.

+0

server.port = 8080 spring.datasource.driverClassName = oracle.jdbc.driver.OracleDriver spring.datasource.username = xxx spring.datasource.password = xxxx # removeAbandoned = true spring.datasource.initialSize = 10 spring.datasource.maxActive = 20 spring.datasource.dbPort = xxxx spring.datasource.dbServiceName = xxxxxx spring.datasource.dbServer = xxxxxxx spring.datasource.url = xxxxx # management.port = 9080 Und Code wurde so geändert @Value ("$ {spring.datasource.url}") \t öffentliche Zeichenfolge DBurl; \t @Value ("$ {spring.datasource.driverClassName}") \t public Zeichenfolge driverClassName1; Aber immer noch Wert kommt wie null –

+0

Nach der Verwendung von 'spring.datasource. *' Eigenschaften, gibt es keine Notwendigkeit, sie in eine andere Klasse zu injizieren, wird Spring eine Instanz von 'DataSource' für Sie ,, –

+1

Danke für die Antwort –

Verwandte Themen