Spring Boot属性注入的几种方式总结[代码实例]
程序员文章站
2022-05-29 15:13:55
...
文章目录
Java配置
- JdbcConfig.java
/*
@Configuration:声明一个类作为配置类,代替xml文件
@Bean:声明在方法上,将方法的返回值加入Bean容器,代替<bean>标签
@value:属性注入
@PropertySource:指定外部属性文件,
**/
@Configuration
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setDriverClassName(driver);
return druidDataSource;
}
}
- jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
- MyController.java
@RestController
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping("hello")
public String hello(){
System.out.println(dataSource);
return "hello spring boot";
}
}
- Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
- Debug代码
- Spring Boot 对上面的属性注入方式进行了改进,使得代码更加简洁和易于理解。
单独放到一个类中
- JdbcConfig.java
@Configuration
@EnableConfigurationProperties(JdbcProperties.class)
public class JdbcConfig {
@Autowired
private JdbcProperties jdbcProperties;
//或者 接当做参数传入 public DataSource dataSource(JdbcProperties jdbcProperties){}
//或者 以构造方法的形式传入
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(jdbcProperties.getUrl());
druidDataSource.setPassword(jdbcProperties.getPassword());
druidDataSource.setDriverClassName(jdbcProperties.getDriver());
druidDataSource.setUsername(jdbcProperties.getUsername());
return druidDataSource;
}
}
- JdbcProperties.java
@ConfigurationProperties(prefix = "jdbc")
@Data //lombok 的注解,用于实现get和set方法
public class JdbcProperties {
private String url;
private String driver;
private String username;
private String password;
}
- application.properties
(注意这个配置文件的名字不能改,系统默认,不能改变)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
- MyController.java
@RestController
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping("hello")
public String hello(){
System.out.println(dataSource);
return "hello spring boot";
}
}
- Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
- Debug代码
直接注入
- 这种方法只适用于这个属性只需要给当前的对象使用,不适用于公共属性。
- JdbcConfig.java
@Configuration
public class JdbcConfig {
@Bean
@ConfigurationProperties(prefix = "jdbc")
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
}
- application.properties
(注意这个配置文件的名字不能改,系统默认,不能改变) - 配置文件的中必须用 driverClassName ,否则会报错。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
- MyController.java
@RestController
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping("hello")
public String hello(){
System.out.println(dataSource);
return "hello spring boot";
}
}
- Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
- Debug代码
属性文件使用yaml文件方式
- JdbcProperties.java
@Component
@ConfigurationProperties(prefix = "jdbc")
@Data //lombok 的注解,用于实现get和set方法
public class JdbcProperties {
private String url;
private String driver;
private String username;
private String password;
}
- application.yaml
(YAML 配置文件语言)
jdbc:
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
username: root
password: 123456
- MyController.java
@RestController
public class MyController {
@Autowired
private JdbcProperties jdbcProperties;
@RequestMapping("hello")
public String hello(){
System.out.println(jdbcProperties);
return "hello spring boot";
}
}
- Application.java
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
- Debug代码
你知道的越多,你不知道的越多。
有道无术,术尚可求,有术无道,止于术。
如有其它问题,欢迎大家留言,我们一起讨论,一起学习,一起进步
上一篇: Python转码问题的解决方法
下一篇: cookie和session的加密?