Spring Boot injection properties are summarized in several ways [Code Example]

Java configuration

  • 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 Code
    Here Insert Picture Description

  • Spring Boot injection method of the above properties were improved, it makes the code more compact and easy to understand.

Into a single class

  • 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
    (note the name of the configuration file can not be changed, the system default, can not be changed)
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 Code
    Here Insert Picture Description

Direct injection

  • This method is only applicable to this property just need to give the current object use, does not apply to public property.
  • JdbcConfig.java
@Configuration
public class JdbcConfig {
    @Bean
    @ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }
}
  • application.properties
    (note the name of the configuration file can not be changed, the system default, can not be changed)
  • Configuration file must driverClassName, otherwise it will error.
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 Code
    Here Insert Picture Description

Properties files yaml papers

  • 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;
}
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 code that
    Here Insert Picture Description
    you know, the more, the more you do not know.
    Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
    If you have other questions, welcome message, we can discuss, learn together and progress together
He published 193 original articles · won praise 116 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/104994072
Recommended