[Spring学习笔记]@Autowired

1 篇文章 0 订阅

Spring中@Autowired的用法

首先确定一下需求

一共有三个对象:Car、Person、Company
Car有name和code两个属性;Person有name和age两个属性;Company有Car和Person两个属性,作为测试输出类。
需要实现的输出为:

Car [name=Car_A, code=10]
Person [name=Person_A, age=20]

Car和Person两个类的代码如下:
Car.class

package com.Spring;
public class Car {
    private String name;
    private int code;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String toString() {
        return "Car [name=" + name + ", code=" + code + "]";
    }

}

Person.class

package com.Spring;

public class Person {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }

}

不使用Spring框架的情况

在不使用Spring的时候,在不更改Car和Person两个类的情况下,我们需要这样写Company的代码:

package com.Spring;

public class Company {

    private Car car;
    private Person person;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public static void main(String[] args) {
        Company company = new Company();
        Car car = new Car();
        car.setCode(10);
        car.setName("Car_A");
        company.setCar(car);
        Person person = new Person();
        person.setAge(20);
        person.setName("Person_A");
        company.setPerson(person);
        System.out.println(company.car.toString());
        System.out.println(company.person.toString());
    }

}

这样既可完成上面说到的输出。

使用 Spring但不使用@Autowired

首先我们需要先写明需要注册的类,创建Beans.xml 并完成如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="CarBean" class="com.Spring.Car">
        <property name="name" value="Car_A" />
        <property name="code" value="10" />
    </bean>
    <bean id="PersonBean" class="com.Spring.Person">
        <property name="name" value="Person_A" />
        <property name="age" value="20" />
    </bean>
    <bean id="CompanyBean" class="com.Spring.Company">
        <property name="Car" ref="CarBean" />
        <property name="Person" ref="PersonBean" />
    </bean>
</beans>

更改Company类如下:

package com.Spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Company {

    private Car car;
    private Person person;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        Company company = (Company) context.getBean("CompanyBean");
        System.out.println(company.car.toString());
        System.out.println(company.person.toString());
    }

}

即可完成需求。、

使用@Autowired的情况

Spring 2.5引入了@Autowired注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。通过@Autowired的使用来消除set,get方法。

(就是为了少写配置文件的代码)

修改Beans.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <bean id="CarBean" class="com.Spring.Car">
        <property name="name" value="Car_A" />
        <property name="code" value="10" />
    </bean>
    <bean id="PersonBean" class="com.Spring.Person">
        <property name="name" value="Person_A" />
        <property name="age" value="20" />
    </bean>
    <bean id="CompanyBean" class="com.Spring.Company">
    </bean>
</beans>

修改Company.java如下:

package com.Spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Company {
    @Autowired
    private Car car;
    @Autowired
    private Person person;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        Company company = (Company) context.getBean("CompanyBean");
        System.out.println(company.car.toString());
        System.out.println(company.person.toString());
    }

}

即可完成规定输出。这里需要注意@Autowired有两种查bean的方式:
首先bytype即按类型查找有没有符合的Bean,如果有且唯一即返回。
如果有相同type的则byname即查找符合的name,如果有则返回。
当然也可以使用@Qualifier进行指定byName方式注入

总结

@Autowired可以减少配置文件的代码,而且可以拥有更低的耦合度。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: @Autowire注解是Spring Boot框架中的一个注解,它用于自动装配Bean。当我们在一个类中使用@Autowired注解时,Spring Boot会自动扫描当前类所在的包及其子包中的所有Bean,并将其注入到当前类中。这样,我们就可以方便地使用其他类中的Bean,而不需要手动创建它们的实例。使用@Autowired注解可以大大简化代码,提高开发效率。 ### 回答2: 在Spring Boot框架中,@Autowired注解是一种自动装配的方式,它会自动将需要的Bean注入到类中。通过使用该注解,我们无需进行手动实例化或是进行setter方法赋值的操作,Spring会自动帮我们处理相关的依赖注入操作。 @Autowire注解可以用在类的字段、构造函数、Setter方法上。当存在多个相同类型的Bean时,可以使用@Qualifier注解指定具体的Bean名称。@Autowired注解在Spring Boot中与依赖注入紧密相连,使用它可以有效地在代码中减少冗余,提升代码的重用性和可读性。 对于使用@Autowired注解注入的bean,Spring Boot会自动处理如下几种情况: 1. 如果发现只有一个Bean与注解的类型匹配,那么将自动注入该Bean。 2. 如果存在多个Bean与注解的类型匹配,但是只有一个Bean具有@Primary注解标识,那么会自动注入@Primary标注的Bean。 3. 如果存在多个Bean与注解的类型匹配,同时没有Bean具有@Primary标识,那么需要结合@Qualifier注解指定具体要使用的Bean。 总的来说,@Autowired注解为我们提供了便捷的依赖注入方式,极大地提升了Spring Boot的开发效率和便捷性。在使用该注解时,需要注意尽可能精细地指定Bean的名称,以保证程序的正确性和可维护性。 ### 回答3: Spring Boot是一个快速开发、便捷部署的框架,它采用了自动配置的理念,从而简化了用户对于Spring的配置。而@Autowired则是Spring中常用的依赖注入注解之一。 首先,需要了解什么是依赖注入。简单来说,依赖注入就是将对象所需要的依赖(包括其它对象、数据或资源等)通过注入的方式实现。而@Autowired注解就是实现依赖注入的一种方式,它能够自动实现对于指定类型的依赖对象的注入,而不需要用户手动实现。 在Spring Boot中,@Autowired注解通常用于将一个类中所需要的其他依赖对象自动注入到该类中,从而方便我们进行开发。采用@Autowired注解时,Spring会扫描整个应用程序上下文,找到与@Autowired注解所标注的对象类型相符并且可用的对象,自动注入到类中。 需要注意的是,当使用@Autowired注解时,要确保被注入的对象已经被创建,并且其Bean对象也已经被创建和初始化。此外,如果出现多个对象同类型的情况,可以使用@Qualifier注解指定需要注入的对象,也可以使用@Primary注解来指定默认实现对象。 总之,@Autowired注解可以让开发者在不写一大堆繁琐代码的情况下,轻松实现依赖注入,提高了代码的可维护性和可读性,让开发更加高效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值