使用java配置时,我接触到了许多陌生的注解,所以专门开一章来记录新接触的这些注解
@Configuration
1、表明该类时配置类 2、将该类纳入spring容器中
@Bean
通过该注解表明注入一个Bean对象,相当于xml中的
通常这样使用(可以用来写配置)
public DriverManagerDataSource driverManagerDataSource(){
DriverManagerDataSource driverManagerDataSource =new DriverManagerDataSource ();
//设置属性
return driverManagerDataSource;
}
@ComponentScan("com.yidu")
扫描类,将com.yidu下的文件都扫描
@SpringBootApplication
表明它是spring boot的主配置类,需要注意的如果没用@ComponentScan注解,那么它默认扫描该类的同级和子级文件
@PropertySource(value={"classpath:mysql.properties"})
读取mysql.properties外部文件,@PropertySource注解只能加载properties类型的文件不能加载yml
@Value("${jdbc.driverclass}")
private String driverClassName;
这样得到外部文件中的值
@ImportResource(locations={"applicationContext.xml"})
加载applicationContext.xml配置文件
springboot默认自动配置,即使你手写了配置文件spring boot也是默认不识别的,如果想要识别,必须在主配置类上加上注解@ImportResource,但现在已经不推荐使用xml的方式来配置了,而一般通过java配置的方式。
2019-01-04