从头认识Spring-2.3 注解装配-@autowired(4)-required(2)

本文深入探讨了Spring框架中@Autowired注解的required参数在多构造器注入场景的应用。通过具体示例,展示了如何正确设置required参数避免异常,并解释了当存在多个构造器时,Spring如何选择合适的构造器进行依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一章节我们来继续详细讨论一下@autowired里面的参数required,在多构造器注入的情况。

1.domain(重点)

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10;

public class Cake {

	private String name = "";

	public String getName() {
		return name;
	}

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

}

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10;

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

public class Chief {
	private Cake cake = null;

	private Oven oven = null;

	@Autowired(required = false)
	public Chief(Cake cake) {
		this.cake = cake;
	}

	@Autowired(required = false)
	public Chief(Cake cake, Oven oven) {
		this.cake = cake;
		this.oven = oven;
	}

	private String name = "";

	public String getName() {
		return name;
	}

	public Cake makeOneCake() {
		if (cake != null) {
			System.out.println(getName() + " use " + oven.getName() + " and make " + cake.getName());
		} else {
			System.out.println("null");
		}
		return cake;
	}

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

}

厨师类这里需要注意的是,当出现多构造器注入的情况,虽然我们都可以使用@autowired标签,但是需要把required都弄成是false,不然抛异常

还有的是,当出现多构造器注入时,注入成功的是注入对象最多的那个构造器,也就是上面有两个参数的那个


烤炉类:(重新引入)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10;

public class Oven {
	private String name = "";

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

	public String getName() {
		return name;
	}

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

}


2.测试类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_10/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief jack = applicationContext.getBean(Chief.class);
		jack.makeOneCake();
	}
}


3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean id="blueberryCheeseCake"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10.Cake"
		p:name="blueberryCheeseCake" scope="prototype" />
	<bean id="bigOven"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10.Oven"
		p:name="bigOven" />
	<bean id="jack"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_10.Chief"
		p:name="jack" />
</beans>


测试输出:

jack use bigOven and make blueberryCheeseCake


总结:这一章节我们主要讨论了@autowired里面的参数required。


目录:http://blog.youkuaiyun.com/raylee2007/article/details/50611627 

 

我的github:https://github.com/raylee2015/my_new_spring


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值