ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

(二)spring的bean自动装配

2022-07-19 20:00:31  阅读:139  来源: 互联网

标签:装配 spring Company springframework public person bean context annotation


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有