从头认识Spring-2.3 注解装配[email protected](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

时间: 2024-11-16 00:24:33

从头认识Spring-2.3 注解装配[email protected](4)-required(1)的相关文章

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected](2)-通过set方法或者其它方法注入

这一章节我们来讨论一下基于java的标准注解装配标签@Inject是如何通过通过set方法或者其它方法注入? 在使用@Inject标签之前.我们须要在pom文件中面增加以下的代码: <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency>

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected](3)-通过构造器方法注入

这一章节我们来讨论一下基于java的标准注解装配标签@Inject是怎样通过通过构造器方法注入? 在使用@Inject标签之前,我们需要在pom文件里面加入下面的代码: <dependency> <groupId>javax.inject</groupId> <artifactId>javax.inject</artifactId> <version>1</version> </dependency> 上面是j

从头认识Spring-2.3 注解装配[email&#160;protected](3)-通过构造器方法注入

这一章节我们来讨论一下注解装配的@autowired是怎样通过set方法或者其他方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_8; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name

从头认识Spring-2.4 基于java的标准注解装配[email&#160;protected]限定器@Named

这一章节我们来讨论一下基于java的标准注解装配标签@Inject的限定器@Named. 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_16; import javax.inject.Named; @Named("myCake") public class Cake { private String name = ""; public String getName(

从头认识Spring-2.3 注解装配[email&#160;protected](5)-限定器@Qualifier(1)

这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11; public class Cake { private String name = ""; public String getName() { return name; } public void setName(String name

21、自动装配[email&#160;protected]&amp;@Inject

21.自动装配[email protected]&@Inject Spring 还支持使用@Resource(JSR250)和@Inject(JSR330)[Java规范的注解] AutowiredAnnotationBeanPostProcessor 完成解析自动装配功能 21.1 @Resource 可以和@Autowired一样实现自动注入功能,默认是按照组件名称进行装配的. 没有能支持@Primary功能,没有支持@Autowired(required = false) 21.2 @In

24、自动装配[email&#160;protected]环境搭建

24.自动装配[email protected]环境搭建 Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能. 开发环境.测试环境.正式环境 数据源切换 24.1 添加 数据源和jdbc驱动 pom 依赖 <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --> <dependency> <groupId>com.mchange</groupId> <artifac

25、自动装配[email&#160;protected]根据环境注册bean

25.自动装配[email protected]根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的激活的时候才能生效 没有标注环境标识的bean,在任何环境下都是加载的 package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.a

Java注解用法[email&#160;protected]

Java注解用法[email protected] 参考地址:https://www.cnblogs.com/perfei456/p/8962167.html 1.注解目标 通过 @SuppressWarnings的源码可知,其注解目标为类.字段.函数.函数入参.构造函数和函数的局部变量.建议注解应声明在最接近警告发生的位置 2.抑止警告的关键字 3.代码示例 示例1--抑制单类型的警告: 1 @SuppressWarnings("unchecked") 2 public void a