spring的bean自动装配主要是通过@Autowired注解实现的
(一) bean源代码解析
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.ParaMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Autowired {
//为true时,没有找到符合要求的bean,spring会抛出异常 boolean required() default true; }
从代码里可以看到该注解可以使用在构造函数,普通方法,参数,属性以及注解上,不能使用在类上,使用范围非常广。
(二)在类属性上使用@Autowired
1. 示例bean
package demo.ioc.annotation.autowired; import org.springframework.stereotype.Component; @Component public class Person { private String uuid = "123"; public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } @Override public String toString() { return "Person{" + "uuid='" + uuid + '\'' + '}'; } }
package demo.ioc.annotation.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Company { @Autowired private Person person; @Override public String toString() { return "Company{" + "person=" + person + '}'; } }
2. 测试
package demo.ioc.annotation.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; public class TestMain { public static void main(String[] args) { // ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml"); ApplicationContext context = new AnnotationConfigApplicationContext(Company.class, Person.class); Company company = context.getBean("company", Company.class); System.out.println(company); } }
输出:Company{person=Person{uuid='123'}}
(三) 在构造函数上使用@Autowired
1.示例bean
package demo.ioc.annotation.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Company { private Person person; @Autowired public Company(Person person) { this.person = person; } @Override public String toString() { return "Company{" + "person=" + person + '}'; } }
2. 测试
package demo.ioc.annotation.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; public class TestMain { public static void main(String[] args) { // ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml"); ApplicationContext context = new AnnotationConfigApplicationContext(Company.class, Person.class); Company company = context.getBean("company", Company.class); System.out.println(company); } }
输出: Company{person=Person{uuid='123'}}
放置在构造函数上时,spring自动为构造函数的入参注入参数。
(四) 在普通方法上使用@Autowired
1. 示例bean
package demo.ioc.annotation.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Company { private Person person; @Autowired public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Company{" + "person=" + person + '}'; } }
2. 测试
package demo.ioc.annotation.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; public class TestMain { public static void main(String[] args) { // ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml"); ApplicationContext context = new AnnotationConfigApplicationContext(Company.class, Person.class); Company company = context.getBean("company", Company.class); System.out.println(company); } }
输出: Company{person=Person{uuid='123'}}
(五) 在入参上使用@Autowired
1. 示例bean
package demo.ioc.annotation.autowired; import org.springframework.beans.factory.annotation.Autowired; public class Company { private Person person; public Company(@Autowired Person person) { this.person = person; } @Override public String toString() { return "Company{" + "person=" + person + '}'; } }
2. 测试
package demo.ioc.annotation.autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; public class TestMain { public static void main(String[] args) { // ApplicationContext context = new ClasspathXmlApplicationContext("applicationContext.xml"); ApplicationContext context = new AnnotationConfigApplicationContext(Company.class, Person.class); Company company = context.getBean("company", Company.class); System.out.println(company); } }
输出:Company{person=Person{uuid='123'}}
(六) @Autowired的装配规则
2.当同类型的bean有多个时,会按照bean的名称进行自动装配
3.当没有找到合适的bean时,如果required=true,则抛出异常。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。