These annotations are available in the spring3.0 version and are used very frequently. Here to record
@Configuration
Before, we used xml to configure ioc or aop, but in the future it will be an era of zero configuration, that is, no xml code is needed, what about the xml configuration file? Is declared with this annotation
@Configuration //加上这个注解这个类就相当于是一个xml的配置文件,让spring扫描即可
public class config(){
}
@Bean
Use the @bean annotation to inject beans
@Configuration //加上这个注解这个类就相当于是一个xml的配置文件,让spring扫描即可
public class config(){
@Bean
public A getA(){
return new A(); //这就相当于在xml配置文件中注册一个beanA
}
@Bean
public C getC(){
return new C(getB()); //这就相当于在xml配置文件中注册一个beanC,并且C依赖B
}
@Bean
public B getB(){
return new B(); //这就相当于在xml配置文件中注册一个beanB
}
}
@ComponentScan
This annotation is equivalent to <context : component-scan >
the package specified in the xml file for scanning configuration. If you do not specify a package, spring scans the file below the package of the class where the annotation is located by default
@propertySource与@propertySource
This annotation is a configuration file such as * .properties file
@Configuration //加上这个注解这个类就相当于是一个xml的配置文件,让spring扫描即可
@propertySource("classpath:1.properties") //可以加载多个配置文件
@propertySource("classpath:2.properties")
public class config(){
}
If it is lower than java1.8 version, you can only use this method to load multiple configuration files
@Configuration //加上这个注解这个类就相当于是一个xml的配置文件,让spring扫描即可
@propertySources({
@propertySource("classpath:1.properties") //可以加载多个配置文件
@propertySource("classpath:2.properties")
})
public class config(){
}
@ConfigurationProperties(prefix = “”)
The purpose of this annotation is to map your custom values in the yml file to the fields of the class. such as
package com.imooc.dataobject;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created by 敲代码的卡卡罗特
* on 2018/3/5 23:07.
*/
@Data
@Component
@ConfigurationProperties(prefix = "")
public class Date {
private String xixi;
}
yml file
xixi: lzh
That's it, it's already injected. Okay