一 @Value赋值和@propertySource加载外部配置文件
1、@Value 一般用在属性和setter方法上,当该类注册成bean时,会自动为其属性或方法的参数赋值。注意:一定不能用在静态方法上,否则会失效
2、用法:
@Value("placeholder") //赋予指定值
@Value("${placeholder}") //赋予配置文件中指定key为placeholder的值
3、@PropertySource("classpath:application.properties") //导入指定的配置文件,一般写在主配置类上
4、示例:
public class Desk{
private String name;
@Value("1")
private int hight;
private String owner;
public Desk() {
}
public Desk(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Value("${name}")
public void setName(String name) {this.name = name;
}
public int getHight() {
return hight;
}
public void setHight(int hight) {
this.hight = hight;
}
public String getOwner() {
return owner;
}
@Value("${owner}")
public void setOwner(String owner) {
this.owner = owner;
}
}
5、创建bean示例:
@PropertySource({"classpath:application.properties"})
@Configuration
@ComponentScan(value="com.dj")
public class MainConfig {
@Bean
public Desk getDesk() {
return new Desk();
}
}
二 @Autowired和@Qualifier和@Primary
1、@Autowired(required=boolean) // 默认byType注入,如果找到多个相同类型的组件,再将属性的名称byName去注入,required设置是否必须注入,默认true,可用于属性、方法、构造器、参数
2、@Qualifier("name") //与@Autowired搭配使用让@Autowired变成byName去注入
3、@Primary //当多个bean是同一个类型时,@Autowired会首选@Primary 的bean去装配
三 @Resource和@Inject
1、@Resource(name="name") //可以和@Autowired一样实现自动注入功能,默认byName进行装配;但不能支持 @Primary 和 @Autowired(required=false)
2、@Inject //需要导入javax.inject包才有这个注解,和@Autowired功能一样,但是没有 @Autowired(required=false) 功能
3、一般spring自动注入推荐@Autowired注入
四、Aware注入,spring底层注入原理:
1、实现了Aware家族接口的bean可以获取到当前bean的一些属性,比如:
ApplicationContextAware //获取到当前bean的ApplicationContext
BeanNameAware //获取到当前bean的name
EmbeddedValueResolverAware //获取到@Value的解析器
。。。
利用这种方法也能对bean进行属性赋值,注入
2、示例:
@Component
public class User implements ApplicationContextAware,BeanNameAware,EmbeddedValueResolverAware{
private String name;
private Dog myDog;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getMyDog() {
return myDog;
}
public void setMyDog(Dog myDog) {
this.myDog = myDog;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("传入的ioc:"+applicationContext);
this.myDog = applicationContext.getBean(Dog.class);
}
@Override
public void setBeanName(String name) {
this.name = name;
System.out.println("beanName="+name);
}
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.age = Integer.parseInt(resolver.resolveStringValue("#{20-10}"));
System.out.println(this.age);
}
}
3、创建测试类
public static void main(String[] args) {
ApplicationContext application = new AnnotationConfigApplicationContext(MainConfig.class);
User user = application.getBean(User.class);
System.out.println(JSON.toJSONString(user));
}
这样就能打印出创建的bean
4、原理:每一个Aware都有一个对应的Processor来处理他,xxxAware 对应 xxxProcessor
四、@Profile环境搭建
1、@Profile("profileName") //spring在启动时,标注了@Profile的bean如果其profileName与启动时设置的profileName不一样,那么就不会注册该bean,@Profile默认为@Profile("default"),没有标注@Profile的bean任何时候都会被加载
2、设置命令行参数选择 profileName:
选择 Run As --> Run Configurations --> Arguments,在VM Arguments框中输入:Dspring.profiles.active=profileName ,选择 Apply-->Run 即可指定运行环境
3、使用无参的 AnnotationConfigApplicationContext 自定义 spring 的启动方式来选择 profileName:
示例:
public static void main(String[] args) {
//1.启动一个无参的applicationContext
AnnotationConfigApplicationContext application = new AnnotationConfigApplicationContext();
//2.设置的applicationContext的运行环境
application.getEnvironment().setActiveProfiles("test");
//3.注册主配置类
application.register(MainConfig.class);
//4.启动刷新容器
application.refresh();
}
这样也可以选择运行时环境。
来源:https://www.cnblogs.com/programmlover/p/10146343.html