从头认识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
从头认识Spring-2.3 注解装配-@autowired(4)-required(1)的更多相关文章
- 从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入
这一章节我们来讨论一下注解装配的@autowired是如何通过set方法或者其它方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_ ...
- 如何在 spring 中启动注解装配?
默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)
这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...
- 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)
这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...
- 模拟实现Spring中的注解装配
本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring自动装配----注解装配----Spring自带的@Autowired注解
Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...
- Spring入门(6)-使用注解装配
Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
随机推荐
- 紫书 例题 10-21 UVa 11971(连续概率)
感觉这道题的转换真的是神来之笔 把木条转换成圆,只是切得次数变多一次 然后只要有一根木条长度为直径就租不成 其他点的概率为1/2^k 当前这个点的有k+1种可能 所以答案为1 - (k+1)/2^k ...
- 我的CSDN原创高质量免积分下载资源列表(持续更新)
最近几个月,我在CSDN平台,发表了大量原创高质量的项目,并给出了相应的源码.文档等相关资源. 为了方便CSDN用户或潜在需求者,下载到自己想要的资源,特分类整理出来,欢迎大家下载. 我的原则:原创高 ...
- unity 天空盒有缝隙的解决方案
修改天空盒图片的属性:advanced->wrap mode->clamp
- 三 Client 如何找到正确的 Region Server
客户端在进行put.delete.get等操作的时候,它都需要数据到底存在哪个Region Server上面,这个定位的操作是通过 Connection.locateRegion方法来完成的. loc ...
- Qt编译mysql以及创建表后进行导入操作
鉴于很多同学对Qt编译myql总是不能成功.出现各种问题,今天特此写出本教程,希望可以帮到须要的同学. 首先,须要明确编译的目的和原理. 目的:Qt 5.2版本号曾经都是不带mysql驱 ...
- FSM的几种策略
FSM是什么?FSM就是Finite(有限) State(状态) 机(Machine)的缩写.(之所以中英文混写,是为了强调学懂FSM的原理是根本,刻意去采用“几段式”的写法并不重要) riple F ...
- style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)"
style="background-image: url(__HOMEPAGE__/views/IMJ2V2/images/banner2.jpg)" 一.问题 backgroun ...
- 18.angularJS服务
转自:https://www.cnblogs.com/best/tag/Angular/ 服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可 ...
- BZOJ 1082 暴搜
思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...
- Linux系统中命令提示符解释-Linux中命令格式、分类-bash中的快捷键
1.命令提示符 [root@mysqlserver01 opt]# [root@mysqlserver01 ~]# root 当前用户名 @ 固定的(格式) mysqlserver01 主机名 opt ...