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 ..
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. –
Wie mache ich das .. Kannst du mir einen Arbeitscode geben .. Ich würde auch brauchen, um andere Eigenschaften außer Datenquelle zu lesen –