Spring注解方式如何处理Bean之间的依赖注入

一、Spring XML 处理依赖注入的方式

在xml配置文件中,Spring处理依赖注入有两种方式:

1、通过setter方法

2、通过构造方法

// 声明接口
public interface UserDao {
    String getName();
}

// 处理接口实现
public class UserDaoImpl implements UserDao{
    private String name;

    public UserDaoImpl(String name) {
        this.name=name;
        System.out.println("调用 UserDaoImpl 构造器,name="+name);
    }

    public UserDaoImpl(){
        System.out.println("调用 UserDaoImpl 的无参构造器 ");
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("setName 方法被调用");
        this.name = name;
    }
}

// 主测试类
public class MainTest {
    public static void main(String[] args) {

        // 加载配置文件就会触发bean的加载
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
      
       /**
         * 执行结果:
         * 调用 UserDaoImpl 构造器,name=我走构造器
         * 调用 UserDaoImpl 的无参构造器 
         * setName 方法被调用
         */
    }
}

// 在 Spring主配置文件:applicationContext.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"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 通过有参构造器注入 -->
    <bean id="userDao" class="com.demo.practice5.dao.UserDaoImpl">
        <constructor-arg value="我走构造器" name="name"/>
    </bean>

    <!-- 通过无参构造器初始化+setter方法注入属性 -->
    <bean id="userDao2" class="com.demo.practice5.dao.UserDaoImpl">
        <property name="name" value="我走setter"/>
    </bean>

</beans>
二、Spring 注解处理依赖注入

注解方式主要是通过 set方法来完成注入的,也就是使用注解替代 <property>标签完成属性注入。

1、实现属性注入的主要注解:

(1)@Value,使用在字段或方法上,用于注入普通数据。

例如:常量(@Value("hello"))、spring容器中的常量(@Value("${jdbc.username}"))

(2)@Autowired,使用在字段或方法上,用于根据类型(byType)注入引用数据。

(3)@Qualifier,使用在字段或方法上,结合 @Autowired,根据名称(byName)注入

(4)@Resource,使用在字段或方法上,根据类型或者名称进行注入。

tips:@Resource位于 javax.annotation包,spring提供了对这个注解的解析;其他三个注解都位于 org.springframework.beans.factory.annotation包中

2、关于@Autowired注解

用于根据类型进行注入,当容器中同一个类型的Bean实例有多个时,会尝试自动根据名字进行匹配。这点跟xml方式的 <bean autowire="byType">  有点不一样,xml方式遇到容器中存在多个同类型的bean时,会直接报错,xml编译不通过。

注意:@Autowired注解的注入名称取值方式,例如:

@Autowired     // 被注入的beanName = userDao2,也就是属性名

private UserDao userDao2;

@Autowired  

public void setUserDao(UserDao userDao3){  // 被注入的beanName=userDao3,也就是set方法的参数名

}

也可以使用 @Qualifier来指明要注入的beanName

@Qualifier("userDao2") + @Autowired  = @Resource("userDao2")

因为@Autowired注解定义中只有一个 required 属性,无法直接指定beanName,所以需要搭配额外注解@Qualifier;而 @Resource注解可以通过 name属性来直接指定beanName。

3、关于@Resource注解

既可以根据类型注入,也可以根据名称注入;无参就是根据类型注入,有参数就是根据名称注入

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值