从头认识Spring-2.3 注解装配-@autowired(4)-required(1)_weixin_30881367的博客-程序员宅基地

这一章节我们来具体讨论一下@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



转载于:https://www.cnblogs.com/jzssuanfa/p/7043636.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_30881367/article/details/99827116

智能推荐

ROS显示路径之visualization/Marker_ros2 写发布一条路径然后显示_xtxu_16的博客-程序员宅基地

本教程将向您展示如何发送四个基本形状(盒子、球体、圆柱体和箭头)。我们将创建一个程序,每秒发送一个新的标记,用不同的形状替换最后一个。1.在工作空间创建一个usingmarker的功能包:catkin_create_pkg using_markers roscpp visualization_msgs2.在src/basic _ shapes. cpp中粘贴以下内容:#include <ros/ros.h>#include <visualization_msgs/Marker._ros2 写发布一条路径然后显示

集成电路技术类毕业论文文献有哪些?_集成电路硕士论文如何写_六维论文推荐的博客-程序员宅基地

本文是为大家整理的集成电路技术主题相关的10篇毕业论文文献,包括5篇期刊论文和5篇学位论文,为集成电路技术选题相关人员撰写毕业论文提供参考。_集成电路硕士论文如何写

SpringMVC上传文件后返回文件服务器地址路径_weixin_33935505的博客-程序员宅基地

先写一个表单:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/ht..._接口返回文件在服务器中的地址

EMC2-源码库_emc2 源码_linuxarmsummary的博客-程序员宅基地

Index of /hardy/dists Name Last modified Size Description Parent Directory - breezy/ 17-Aug-2012 21:03 - _emc2 源码

c++:stoi()和to_string()函数_stoi函数_一萧一剑走江湖的博客-程序员宅基地

包含在头文件#include<string>1.stoi函数 作用: 将 n 进制的字符串转化为十进制 用法 stoi(字符串,起始位置,n进制(默认10进制)),将 n 进制的字符串转化为十进制 举例: stoi(str, 0, 2); //将字符串 str 从 0 位置之后的数字的 2 进制数,转换为十进制 注意: stoi()函数如果传入的字符串s中含有不是数字的字符,则只会识别到从开头到第一个非法字符之 前,如果第一个字符就是非法._stoi函数

随便推点

tcl/tk参考——列表操作lreplace_dulixin的博客-程序员宅基地

..名称lreplace - 在一个列表中使用新的元素替代其它元素 语法lreplace list first last ?element element ...?描述lreplace返回一个新的列表,新的列表通过替代一个或者多个list列表中的元素。first和last指定了需要替代的范围的索引。first和last符合索引的指定方法,0指列表的第一个元素,end指列_lreplace

Hook插件(一)_gltools_hook插件-程序员宅基地

1. 新建工程时选择“No Activity”2. App目录下创建一个新的文件夹”lib”(这里一定要注意,不是使用libs文件夹而是创建一个lib文件夹,如果这里使用了libs文件夹的话,在安装成功模块之后重启会发现Hook是失败的,通过打印tag为xposed的日志信息会发现这样的错误:Java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation这个错误主要是因_gltools_hook插件

Logistic回归详解及案例_dps直线回归_Lucky_youth的博客-程序员宅基地

刚开始学习回归的时候一头雾水——线性回归linear regression,逻辑回归logistic regression和Softmax regression?线性回归是回归算法,而逻辑回归和softmax本质上是分类算法_dps直线回归

Unity镜子效果的实现(无需镜子Shader)_weixin_34190136的博客-程序员宅基地

Unity镜子效果制作教程本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar —— 心分享、心创新!助力快速实现一个简单的镜面反射效果为新手节省..._unity实现贴图mirror效果

关于imageview 点击事件_android imageview 不能获取点击事件_zanyeping4763的博客-程序员宅基地

刚学IOS,自己做了个APP,但是imageView点击判断这卡不知道改怎么写,求大神给瞅瞅……代码见楼下……_android imageview 不能获取点击事件

hpux下dbc_max_pct参数_db_pct_bingzhuan的博客-程序员宅基地

The dbc_min_pct 核心参考定义动态高速缓冲区所使用的最小内存百分比,下面是容许的dbc_min_pct 值: 最小: 2 最大: 90 缺省: 5在文件系统 I/O 操作过程中,数据存储在一个高速缓冲区中,它的大小可以是固定的,可以是动态分配的。当bufpages 和 nbuf 参数设为缺省值零时,高速缓冲区的大小根据对系统内存的竞争性请求,动态地增大或减_db_pct

推荐文章

热门文章

相关标签