Spring注解开发03--------属性赋值与自动装配
属性赋值
概述
在Spring中,我们可以使用@Value
来为我们的属性赋值。
使用@Value
赋值,可以:
- 基本参数
- 可以写Spel表达式:#{}
- 可以使用${},取出配置文件(.properties)中的值(即运行环境中的值)
测试
1.编写配置文件person.properties,配置文件中设置人物的昵称
person.nickName=张三蛋
2.编写person类:
package com.xdw.pojo;
import org.springframework.beans.factory.annotation.Value;
public class Person {
// 使用@Value赋值:
// 1.基本参数
// 2.可以写SpEl: #{}
// 3.可以写${}; 取出配置文件中【properties】的值(在运行环境变量里的值)
@Value("张三")
private String name;
@Value("#{20-2*3}")
private Integer age;
@Value("${person.nickName}")
private String nickName;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", nickName='" + nickName + '\'' +
'}';
}
}
3.编写配置类
package com.xdw.config;
import com.xdw.pojo.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
// @PropertySource将指定路径下的配置文件加载进程序运行环境中
@PropertySource(value="classpath:person.properties")
@Configuration
public class MainConfigOfValueProperty {
@Bean
public Person person() {
return new Person();
}
}
4.编写测试类
import com.xdw.config.MainConfigOfValueProperty;
import com.xdw.pojo.Person;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestProperty {
@Test
public void test01() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfValueProperty.class);
Person person = (Person)applicationContext.getBean("person");
System.out.println(person);
}
}
5.运行测试
总结
@Value注解有以下三种使方式:
- 基本参数
- 可以写Spel表达式:
#{}
- 可以使用
${}
,取出配置文件(.properties)中的值(即运行环境中的值) - 我们可以使用
@PropertySource
注解来将我们的配置文件加载到运行环境中
自动装配
概述
自动装配: Spring利用依赖注入(DI),完成对IOC容器中各个组件依赖关系赋值
方式一:Spring自带注解
@Autowried
- 该注解默认优先按照类型去容器中找到对应组件,找到就直接赋值
- 如果找到多个相同类型的组件,就将属性的名称作为组件的id去容器中查找
- 可以使用
@Qualifier
注解来指定需要装配的组件的id - 自动装配默认一定要将组件装配好,否则就会报错!可以使用
@Autowired(required = false)
修改这一默认配置 @Primary
注解,放在bean或者创建bean的方法上,让Spring进行默认装配的时候,首选!
方式二:@Resource(JSR250)和@Inject(JSR330)[java规范的注解]
@Resource:
- 可以和@Autowried一样,实现自动装配功能:默认是按照组件名称进行装配的
- 没有支持@Primary注解功能,也没有@Autowired(required = false)这一配置
@Inject:
- 需要导入javax.inject依赖,和@Autowried功能一样,但是没有required=false的功能
<!-- 支持JSR330规范 -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
@Autowried补充说明
@Autowried
注解可以标注在构造器、参数、方法、属性上,不管放在哪,都是从容器中获取组件的值。
-
标注在方法上: a.标注在set方法上 b.@Bean + 方法参数,省略@Autowried可以从容器中直接获取参数(@Bean标注的方法创建对象的时候,方法参数的值从容器中获取)!
@Component public class Boss { private Car car; public Car getCar() { return car; } @Autowired //标注在方法上,Spring容器创建当前对象,就会调用方法,完成赋值 //方法使用的参数,自定义类型的值从ioc容器中获取 public void setCar(Car car) { this.car = car; } @Override public String toString() { return "Boss{" + "car=" + car + '}'; } }
/** * @Bean标注的方法创建对象的时候,方法参数的值从容器中获取 * 这里的@Autowried可以省略 * @param car * @return */ @Bean public Color color(@Autowried Car car) { Color color = new Color(); color.setCar(car); return color; }
-
标在构造器上: 如果组件只有一个有参构造器,这个有参构造器@Autowried可以省略,对应的组件还是能从容器中获取
-
放在参数位置
@Component public class Boss { private Car car; public Car getCar() { return car; } public void setCar(@Autowired Car car) { this.car = car; } @Override public String toString() { return "Boss{" + "car=" + car + '}'; } }
-
放在类属性上
Spring容器底层的一些组件在自定义组件中的的装配
自定义组件想要使用Spring容器底层的一些组件(ApplicationContext, BeanFactory),只需要让自定义组件实现xxxAware接口即可:在创建对象的时候,会调用接口规定的方法注入相关组件! 参照Aware接口设计。
原理:
通过xxxProcessor来实现的,例如:
ApplicationContextAware 对应一个 ApplicationContextAwareProcessor
@Component
public class Dog implements ApplicationContextAware {
private ApplicationContext applicationContext;
public Dog() {
System.out.println("dog constructor。。。。");
}
// 在bean创建完成并初始化之后
@PostConstruct
public void init() {
System.out.println("dog postConstruct。。。。");
}
// 在容器销毁bean之前
@PreDestroy
public void destory() {
System.out.println("dog PreDestroy。。。。");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫