其他分享
首页 > 其他分享> > (二)spring的bean自动装配

(二)spring的bean自动装配

作者:互联网

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的装配规则

1.默认按照类型进行自动装配

2.当同类型的bean有多个时,会按照bean的名称进行自动装配

3.当没有找到合适的bean时,如果required=true,则抛出异常。

标签:装配,spring,Company,springframework,public,person,bean,context,annotation
来源: https://www.cnblogs.com/sfy-blog/p/16495537.html