这一章节我们来讨论一下内部类注入Bean。

1.domain

蛋糕类:(跟前一章节的一样)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9;

public class Cake {

	private final int id = index++;

	private static int index = 0;

	private String name = "";

	private double size = 0;

	public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public double getSize() {
return size;
} public void setSize(double size) {
this.size = size;
} public int getId() {
return id;
} @Override
public String toString() {
return "create the cake,its id:" + id + ", size:" + size + " inch ,name:" + name;
}
}

厨师类:(基本没有变化)

package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9;

public class Chief {

	private Cake cake = null;

	public Cake getCake() {
return cake;
} private String name = ""; public Chief(String name) {
this.name = name;
} public Chief(String name, Cake cake) {
this.name = name;
this.cake = cake;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void setCake(Cake cake) {
this.cake = cake;
} private final int id = index++; public int getId() {
return id;
} private static int index = 0; public Cake makeOneCake() {
return cake;
} }

測试类:

package com.raylee.my_new_spring.my_new_spring.ch01.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/ch01/topic_1_9/ApplicationContext-test.xml" })
public class ChiefTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
Chief chief = applicationContext.getBean(Chief.class);
System.out.println("chief name:" + chief.getName());
System.out.println(chief.getName() + " make a cake ,cake's name :" + chief.makeOneCake());
}
}

2.配置文件

以下是通过属性注入内部类的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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="chief"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9.Chief">
<constructor-arg value="jack" />
<property name="cake">
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9.Cake">
<property name="name" value="jack's own cake" />
<property name="size" value="9" />
</bean>
</property>
</bean>
</beans>

注意:我们内部类注入,配置文件中面的cake是不带id的,并且。他不能为其它的Bean所公用。

以下是通过构造器注入内部类的配置文件:

<?

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.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="chief"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9.Chief">
<constructor-arg value="jack" />
<constructor-arg>
<bean class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_9.Cake">
<property name="name" value="jack's own cake" />
<property name="size" value="9" />
</bean>
</constructor-arg>
</bean>
</beans>

大家须要注意的是。我们上面的配置文件配置Bean的时候配置了两个属性,因此我们的构造器里面,必须存在这两个属性值,不然就会抛异常。

总结:这一章节主要介绍内部类注入Bean的形式和注意点。

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

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

从头认识Spring-1.9 内部类注入Bean的更多相关文章

  1. Spring在Thread中注入Bean无效的解决方式

    在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...

  2. Spring IOC容器中注入bean

    一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...

  3. Spring的DI(Ioc) - 注入bean 和 基本数据类型

    注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...

  4. Spring的几种注入bean的方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式).   使用属性的se ...

  5. Spring中如何动态注入Bean实例教程

    前言 在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式,容易出现未被正确注入成功的情况. 本文将介绍一种在实际项目中基于动态的方式来提取Spring管理的Be ...

  6. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

  7. Spring框架知识总结-注入Bean的各类异常

    近日整合sping和hibernate框架时遇到了一系列的异常,本次主要说明一下spring框架可能出现的异常及解决方案. 我们借助sping强大的bean容器管理机制,通过BeanFactory轻松 ...

  8. Spring 注解Autowired自动注入bean异常解决

      错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...

  9. Spring为IOC容器注入Bean的五种方式

    一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...

随机推荐

  1. Reuse a SSL socket

    It's possible to reuse a SSL socket after proper cleanup. See SSL Socket free and shutdown on stacko ...

  2. 2星|《约见投资人》:A股上市公司软文集

    约见资本人:58家上市公司创始人亲述创业之路 全书写了58个A股上市公司的故事,基本是宣传上市公司老总的软文.基本的套路是创始人历尽苦难创立了公司,取得了好业绩.最希望看的分析与数据几乎没有.看了一小 ...

  3. nw.js开发第一个程序(html开发桌面程序exe)

    一.环境配置 windows系统 cnpm install node 下载nw.js https://github.com/nwjs/nw.js 找到download下载合适的版本 二.开发 项目目录 ...

  4. C++ 模板template和template

    原文链接:https://blog.csdn.net/skyleung/article/details/42195509 template<class T>和template<typ ...

  5. POJ_2255_Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12342   Accepted: 7712 De ...

  6. GetArxPath

    extern HINSTANCE _hdllInstance;CString GetArxPath(){ CString strArxPath; GetModuleFileName(_hdllInst ...

  7. 无插件纯Web HTML5 3D机房 进阶篇(新增设备、线缆、巡查等功能)

    前情提要 前阵子写了一篇无插件纯Web 3D机房,介绍了如何用html5在网页上创建无插件的精美3d机房场景.这两个月以来,陆续收到很多朋友的鼓(膝)励(盖),受宠若惊之余,对索要源代码的朋友都已经尽 ...

  8. boostrap标签

    字体: <lead>:加强显示 <strong><b>:字体加粗 <i><em>:斜体字 .text-muted:提示,使用浅灰色(#999 ...

  9. 『 Luogu P3205 』 HNOI2010 合唱队

    解题思路 设置两个二维数组 $f$ 和 $g$,含义如下. $f[l][r]$ 表示在期望得到的队形中 $l\rightarrow r$ 这段区间初始队形排列的方案数,并且最后一个加入进去的是第 $l ...

  10. [Algorithm] 5. Kth Largest Element

    Description Find K-th largest element in an array. Example In array [9,3,2,4,8], the 3rd largest ele ...