Java configuration
@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. driver= com. mysql. jdbc. Driver
jdbc. url= jdbc: mysql: / / localhost: 3306 / springboot? characterEncoding= utf- 8
jdbc. username= root
jdbc. password= 123456
@RestController
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping ( "hello" )
public String hello ( ) {
System. out. println ( dataSource) ;
return "hello spring boot" ;
}
}
@SpringBootApplication
public class Application {
public static void main ( String[ ] args) {
SpringApplication. run ( Application. class ) ;
}
}
Debug Code
Spring Boot injection method of the above properties were improved, it makes the code more compact and easy to understand.
Into a single class
@Configuration
@EnableConfigurationProperties ( JdbcProperties. class )
public class JdbcConfig {
@Autowired
private 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;
}
}
@ConfigurationProperties ( prefix = "jdbc" )
@Data
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
@RestController
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping ( "hello" )
public String hello ( ) {
System. out. println ( dataSource) ;
return "hello spring boot" ;
}
}
@SpringBootApplication
public class Application {
public static void main ( String[ ] args) {
SpringApplication. run ( Application. class ) ;
}
}
Debug Code
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
@RestController
public class MyController {
@Autowired
private DataSource dataSource;
@RequestMapping ( "hello" )
public String hello ( ) {
System. out. println ( dataSource) ;
return "hello spring boot" ;
}
}
@SpringBootApplication
public class Application {
public static void main ( String[ ] args) {
SpringApplication. run ( Application. class ) ;
}
}
Debug Code
Properties files yaml papers
@Component
@ConfigurationProperties ( prefix = "jdbc" )
@Data
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
@RestController
public class MyController {
@Autowired
private JdbcProperties jdbcProperties;
@RequestMapping ( "hello" )
public String hello ( ) {
System. out. println ( jdbcProperties) ;
return "hello spring boot" ;
}
}
@SpringBootApplication
public class Application {
public static void main ( String[ ] args) {
SpringApplication. run ( Application. class ) ;
}
}
Debug code that 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