Method 1: Java form
Use the comment
1. @Configuration // annotation indicates that the class is a class configuration
2. @ PropertySource ( "classpath: jdbc.properties") // reads the configuration file attributes
3. @ Bean // The statement notes on the method, the method's return value added bean container
4. @ value injection properties
Step 1: Configure class
@Configuration // This class represents an annotation configuration class @PropertySource ( "CLASSPATH: the jdbc.properties") // read the profile property public class JdbcConfig { @Value ( "jdbc.driverClassName $ {}") // injection attribute String driverClassName; @Value("${jdbc.url}") String url; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean // of the annotations in the method declaration, the return value is added bean container public the DataSource getDataSource () { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
Step Two: Profile
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test
jdbc.username=root
jdbc.password=root