注解@Autowired是Spring对组件自动装配的一种方式。常用于在一个组件中引入其他组件。
//引入Person组件
@Autowired
Person person;
自动装配:sprng通过依赖注入(DI),完成IOC容器中各个组件依赖的关系赋值
annotationConfigApplicationContext.getBean(Person.class);
annotationConfigApplicationContext.getBean("person");
@Primary
@Bean
public Person person(){
return new Person("张三");
}
@Qualifier("person")
@Autowired
Person person;
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.dashu.bean.Person' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
@Autowired(required = false)
Person person;
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Autowired {
boolean required() default true;
}
public class Person {
private String name;
public Person() {}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
@Bean
public Person person(){
return new Person("张三");
}
}
@Service
public class TestService {
@Autowired
Person person;
public void test(){
System.out.println(person);
}
}
import com.dashu.config.BeanConfig;
import com.dashu.service.TestService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
//加载配置类获取容器
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class);
//容器中获取TestService
TestService testService = annotationConfigApplicationContext.getBean(TestService.class);
//调用测试方法
testService.test();
}
}
如果有多个相同类型的组件,再将属性的名称作为组件的id去容器中查找
annotationConfigApplicationContext.getBean("person");
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
@Bean
public Person person1(){
return new Person("张三");
}
@Bean
public Person person2(){
return new Person("李四");
}
}
import com.dashu.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
Person person2;
public void test(){
System.out.println(person2);
}
}
@Autowired配合@Primary使用,当使用@Autowired自动装配时,默认优先选择被注解@Primary标注的组件
@Primary
@Bean
public Person person(){
return new Person("张三");
}
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
@Bean
public Person person1(){
return new Person("张三");
}
@Primary
@Bean
public Person person2(){
return new Person("李四");
}
}
import com.dashu.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
Person person;
public void test(){
System.out.println(person);
}
}
@Autowired配合@Qualifier使用,使用注解@Qualifier可以指定需要装配组件的id
@Qualifier("person")
@Autowired
Person person;
import com.dashu.bean.Person;
import org.springframework.context.annotation.*;
@ComponentScan({"com.dashu"})
@Configuration
public class BeanConfig {
@Bean("person1")
public Person person1(){
return new Person("张三");
}
@Bean("person2")
public Person person2(){
return new Person("李四");
}
}
import com.dashu.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Qualifier("person2")
@Autowired
Person person;
public void test(){
System.out.println(person);
}
}