SpringFramework|@Autowired

@Autowried的使用

前述

Java: 1.8

Maven: 3

SpringFramework版本以及各组件成员: 5.1.1.RELEASE

  • spring-context
  • spring-core
  • spring-beans

@Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。

  • 在属性上 - 省略setter
  • 在setter方法上 - 实现byName装配
  • 在构造函数上 - 应用于构造注入

以及@Autowired(required=false), 这表明依赖是非必要的. (默认必要)

在属性上的@Autowried(以省略setter)

你可以在属性中使用 @Autowired 注释来除去 setter 方法。当时使用 为自动连接属性传递的时候,Spring 会将这些传递过来的值或者引用自动分配给那些属性。

Bean.java (并不是严格意义上的JavaBean):

package noioo;

public class Bean {

    public void sayHelloWorld(){
        System.out.println("Hello World");
    }
}

BeanUser.java(Bean的使用者):

package noioo;

import org.springframework.beans.factory.annotation.Autowired;

public class BeanUser {

    @Autowired
    private Bean bean;


    public Bean getBean() {
        return bean;
    }

    public void useBean(){
        bean.sayHelloWorld();
    }
}

spring-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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="beanUser" class="noioo.BeanUser">
    </bean>

    <bean id="bean" class="noioo.Bean">
    </bean>
</beans>

Runner.java - 执行者

package noioo;

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

public class Runner {

    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
        BeanUser beanUser = (BeanUser)context.getBean("beanUser");
        beanUser.useBean();
    }
}

执行结果

Hello World

Process finished with exit code 0

在Setter方法上的@Autowired(实现byName)

你可以在 XML 文件中的 setter 方法中使用 @Autowired 注释来除去 元素。当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中视图执行 byType 自动连接。

❗不修改上面示例中的内容有:

  • Bean.java
  • spring-beans.xml
  • MainRunner.java

BeanUser.java修改如下:

package noioo;

import org.springframework.beans.factory.annotation.Autowired;

public class BeanUser {


    private Bean bean;

    @Autowired // [相当于byName]将在配置文件中查找名为bean的SpringBean来进行注入.
    public void setBean(Bean bean) {
        this.bean = bean;
    }

    public Bean getBean() {
        return bean;
    }

    public void useBean(){
        bean.sayHelloWorld();
    }
}

执行结果

Hello World

Process finished with exit code 0

构造函数上的@Autowired(构造注入)

你也可以在构造函数中使用 @Autowired。一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接。

❗不修改上面示例中的内容有:

  • Bean.java
  • spring-beans.xml
  • MainRunner.java

BeanUser.java修改为构造注入并添加@Autowired:

package noioo;

        import org.springframework.beans.factory.annotation.Autowired;

public class BeanUser {


    private Bean bean;

    @Autowired
    public BeanUser(Bean bean){
        this.bean = bean;
    }


    public Bean getBean() {
        return bean;
    }

    public void useBean(){
        bean.sayHelloWorld();
    }
}

执行结果

Hello World

Process finished with exit code 0

转载于:https://www.cnblogs.com/shwo/p/9863633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值