首页 > 代码库 > 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)

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

这一章节我们来具体讨论一下@autowired里面的參数required。

1.domain(重点)

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;

public class Cake {

	private String name = "";

	public String getName() {
		return name;
	}

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

}

厨师类:

(1)以下是会报错的厨师类

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;

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

public class Chief {
	@Autowired
	private Cake cake = null;

	private String name = "";

	public String getName() {
		return name;
	}

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

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

}

报错信息:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9.Cake] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:920)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:789)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:703)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:474)
... 40 more


(2)以下是同意注入是空对象

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;

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

public class Chief {
	@Autowired(required = false)
	private Cake cake = null;

	private String name = "";

	public String getName() {
		return name;
	}

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

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

}

上面须要注意的是,@Autowired标签是默认强制注入非空对象,可是我们能够通过required=false属性,注入null对象


2.測试类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9;

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_9/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="jack"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_9.Chief"
		p:name="jack" />
</beans>

为了測试上面的样例,我们有益删除了cake的bean


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


文件夹:http://blog.csdn.net/raylee2007/article/details/50611627 

 

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



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