这一章节我们来具体讨论一下@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)的更多相关文章

  1. 从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入

    这一章节我们来讨论一下注解装配的@autowired是如何通过set方法或者其它方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_ ...

  2. 如何在 spring 中启动注解装配?

    默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...

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

    这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...

  4. 从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)

    这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_n ...

  5. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

  6. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  7. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

  8. Spring入门(6)-使用注解装配

    Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...

  9. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

随机推荐

  1. Spring-statemachine Action不能并发执行的问题

    Spring-statemachine版本:当前最新的1.2.3.RELEASE版本 这几天一直被Action是串行执行搞得很郁闷,写了一个demo专门用来测试: public static void ...

  2. Use Uncertainty As a Driver

     Use Uncertainty As a Driver Kevlin Henney ConFRonTEd WiTH TWo opTionS, most people think that the ...

  3. 利用photoshop的动作功能实现图片批处理操作

    首先,通过"窗体菜单"点击"动作".启动动作栏. 然后.通过动作栏的菜单,新建动作. 接着,完毕一个标准操作,包含打开图像,图像处理或者格式转换,另存为,等. ...

  4. spring-cloud-netflix集成的服务

    1.Ribbon Ribbon is a client side IPC library that is battle-tested in cloud. It provides the followi ...

  5. 【Codeforces Round #459 (Div. 2) A】Eleven

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 这个数列增长很快的. 直接暴力模拟看看是不是它的一项就好了 [代码] #include <bits/stdc++.h> ...

  6. CodeForcesGym 100502D Dice Game

    Dice Game Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Or ...

  7. CentOS下部署巡风步骤详解

    本博客已经迁移到新的网站,www.je2se.com,请大家移步关注,互相交流,共同成长 巡风Centos 6.5部署指南 基础环境要求: Python2.7+ 安装Centos相关依赖 # Cent ...

  8. androidclient和站点数据交互的实现(基于Http协议获取数据方法)

    androidclient一般不直接訪问站点数据库,而是像浏览器一样发送get或者post请求.然后站点返回client能理解的数据格式,client解析这些数据.显示在界面上.经常使用的数据格式是x ...

  9. 作为刚開始学习的人应该怎样来学习FPGA

    FPGA作为一种高新的技术.已经逐渐普及到了各行各业.不管是消费类.通信类.电子行业都无处不在它的身影,从1985年第一颗FPGA诞生至 今,FPGA已经历了将近20多个年头,从当初的几百个门电路到如 ...

  10. hdu5024

    思路要开阔些,或者说要转化一下思路,别太死 把每一个点当拐点,爆一边就能够.用记忆化搜索也行.都不会超时 #include<bits/stdc++.h> using namespace s ...