使用@PropertySource读取配置文件信息。
当Spring框架读取了配置文件中的信息后,会将读取的数据封装在内置的Environment对象,之后任何需要这些配置信息的的组件都可以从Environment中读取到配置的数据。
1.通过@Value注解获取配置文件信息。
代码如下:
jabc.properties
spring.jdbc.url=jdbc:mysql://localhost:3306
spring.jdbc.driver=com.mysql.jdbc.Driver
spring.jdbc.username=root
spring.jdbc.password=1234
SpringConfig
package spring;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan("spring")
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {
}
JdbcConfig
package spring;
import org.springframework.beans.fa