这一章节我们来讨论一下内部类注入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. http链接中请求进行编码,Http请求

    如果参数中含有特殊字符&,则强制URL编码<br> http协议中参数的传输是"key=value"这种简直对形式的,如果要传多个参数就需要用“&”符号 ...

  2. jboss之启动加载过程详解

    今天看了看jboss的boot.log和server.log日志,结合自己的理解和其他的资料,现对jboss的启动和加载过程做出如下总结: boot.xml是服务器的启动过程的日志,不涉及后续的操作过 ...

  3. Controller传值到前端页面的几种方式

    一丶追加字符串传值 #region 02-追加字符串传值 /// <summary> /// 02-追加字符串传值 /// </summary> /// <returns ...

  4. 官方安装docker-ce步骤

    这里是Centos7安装方式 安装依赖包 $ sudo yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 添加Docker ...

  5. vuex与redux,我们都一样

    vuex与redux的主要区别: redux:生成的全局数据流是通过每个组件的props逐层传递到各个子组件的,通过@connect装饰器绑定在this.props上面. vuex :生成的全局数据则 ...

  6. A5. JVM 如何判断GC对象

    [概述] 在堆里面存放着 Java 世界中几乎所有的对象实例,垃圾收集器在对堆进行回收前,第一件事情就是要确定这些对象之中哪些还 “存活” 着,哪些已经 “死去”(即不可能再被任何途径使用的对象). ...

  7. jquery 实现点评标签 类似淘宝大众点评的 快速准时 货品完好等

    111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...

  8. 关闭的语句: next、关闭的 Resultset: next、关闭的连接: next问题

    如果在rs.next()之前关闭了Statement或PreparedStatement,会导致下面的异常: java.sql.SQLException: 关闭的语句: next 如果在rs.next ...

  9. Docker从入门到实践

    一般说来 SPA 的项目我们只要启一个静态文件 Server 就可以了,但是针对传统项目就不一样了,一个项目会依赖很多服务端程序.之前我们的开发模式是在一台开发机上部署开发环境,所有人都在这台开发机上 ...

  10. LINUX-光盘

    cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force 清空一个可复写的光盘内容 mkisofs /dev/cdrom > ...