简体   繁体   中英

Spring-boot - How to load resources/datasources?

I've been using Spring Boot and deploying the application via a .jar file. I want to load multiple datasources . When I was using local tomcat I used to add them to context.xml , but now I'm using the embedded tomcat and was wondering if it's possible to load them via some external file or some way which I would put on the server (UAT/PROD).

I've found a sample of how to do it via Java Class, but I was wondering if it's possible to do it other way as I'd like to leave the user/password out of the .jar and the configurations on a local file on the server. (looks easier/faster for future developments). Maybe using a " datasource.yml " or something similar..

Does anyone know how to achieve that?

Thanks!

My context.xml :

<?xml version='1.0' encoding='utf-8'?>
<Context>

    <!-- ABC QA -->
    <Resource
        name="jdbc/abc"
        type="javax.sql.DataSource"
        maxActive="5"
        maxIdle="2"
        username="xxx"
        maxWait="1000"
        validationQuery="select 1"         
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        password="xxx"     
        url="jdbc:sqlserver://server-qa.com:1234;databaseName=myDB"/>

    <!-- DEF QA -->
    <Resource
        name="jdbc/def"
        type="javax.sql.DataSource"
        maxActive="5"
        maxIdle="2"
        username="xxx"
        maxWait="1000"
        validationQuery="select 1"         
        driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
        password="xxx"     
        url="jdbc:sqlserver://server2-qa.com:1234;databaseName=myDB"/>

</Context>

Ps.: In case anyone wants to check the java I refer to, the link is: https://github.com/wilkinsona/spring-boot-sample-tomcat-jndi/blob/master/src/main/java/sample/tomcat/jndi/SampleTomcatJndiApplication.java

Update:

after a bit of changes this code below now works in case anyone needs it! :)

application.yml

server:
  port: 9998

logging.level.org.springframework.web: DEBUG

spring.ds_db1:
  url: jdbc:sqlserver://mydb1.com:1433;databaseName=mydb1
  username: xxx
  password: xxx
  driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver

spring.ds_db2:
  url: jdbc:sqlserver://mydb2.com:1433;databaseName=mydb2
  username: xxx
  password: xxx
  driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver

DatabaseConfig

@Configuration
@ConfigurationProperties(prefix = "spring.ds_db1")
public class DatabaseDb1Config {

    @Bean(name="dsDb1")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getRequiredProperty("spring.ds_db1.driverClassName"));
        dataSource.setUrl(env.getRequiredProperty("spring.ds_db1.url"));
        dataSource.setUsername(env.getRequiredProperty("spring.ds_db1.username"));
        dataSource.setPassword(env.getRequiredProperty("spring.ds_db1.password"));

        return dataSource;
    }

    @Bean(name="jdbcDb1")
    public JdbcTemplate jdbcTemplate(DataSource dsChange) {
        return new JdbcTemplate(dsChange);
    }

}

Repository

@Service
public class ContractRepositoryImpl implements ContractRepository {

    @Autowired
    @Qualifier("jdbcDb1")
    protected JdbcTemplate jdbc;

    public Contract getContract(long id) {
        return jdbc.queryForObject("SELECT ...", contractMapper, id);
    }

    private static final RowMapper<Contract> contractMapper = new RowMapper<Contract>() {
        public Contract mapRow(ResultSet rs, int rowNum) throws SQLException {
            Contract contract = new Contract(rs.getLong("contract"), rs.getString("name"));
            return contract;
        }
    };

}

Relevant part from pom.xml

...
<spring.version>4.2.0.RELEASE</spring.version>
...

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!-- SQL SERVER DRIVER FOR LEGACY CONNECTIONS -->
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>2.0</version>
    </dependency>

You can configure the datasources with application.properties (or .yml) files. See the list of available properties .

Spring Boot will read configuration files from different sources , one would be the directory in which the JAR file is stored, or you provide the values with System Properties or environment variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM