External Properties in Spring

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

Fetching property values from the environment

@Configuration
public class DbConfig {
    Environment env;

    @Autowired
    public DbConfig(Environment env) {
        this.env = env;
    }

    @Bean
    public JdbcTemplate dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.url"));
        dataSource.setUsername(env.getProperty("db.user"));
        dataSource.setPassword(env.getProperty("db.password"));
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
}

Enter fullscreen mode Exit fullscreen mode

app.properties file

db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/cas
db.user=root
db.password=123

Enter fullscreen mode Exit fullscreen mode

@PropertySource and @Value

@PropertySource annotation adds properties from other files in addition to environment variables and system properties. You can use classpath, file or http prefixes.

@Configuration
@PropertySource("classpath:/com/org/config/app.properties")
public class DbConfig {

    @Bean
    public JdbcTemplate dataSource(
            @Value("${db.driver}") String driver, 
            @Value("${db.url}") String url,
            @Value("${db.user}") String user, 
            @Value("${db.password}") String password) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(user);
        dataSource.setPassword(password);
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
}

Enter fullscreen mode Exit fullscreen mode

Spring Framework (24 Part Series)

1 Spring Configuration
2 External Properties in Spring
20 more parts…
3 Profiles in Spring
4 Spring Expression Language (SpEL)
5 How Spring implements Singleton Beans
6 Converting explicit into implicit configuration in Spring
7 Autowiring in Spring
8 Lazy Beans in Spring
9 @PostConstruct and @PreDestroy in Spring
10 Stereotype and Meta Annotations in Spring
11 Spring’s FactoryBean Interface
12 The Spring Bean Lifecycle
13 Spring AOP
14 Caching in Spring
15 Spring JDBC
16 Spring Transaction Management
17 JPA with Spring
18 JPA with Spring Boot
19 Spring Web MVC
20 Spring Boot for war
21 Spring MVC REST
22 Reactive Spring Applications
23 Spring Security
24 Spring Injection Types

原文链接:External Properties in Spring

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容