从头认识Spring-1.9 内部类注入Bean
这一章节我们来讨论一下内部类注入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
从头认识Spring-1.9 内部类注入Bean的更多相关文章
- Spring在Thread中注入Bean无效的解决方式
在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...
- Spring IOC容器中注入bean
一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...
- Spring的DI(Ioc) - 注入bean 和 基本数据类型
注入bean有两种方式: 注入其他bean: 方式一 <bean id="orderDao" class="cn.itcast.service.OrderDaoBe ...
- Spring的几种注入bean的方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的se ...
- Spring中如何动态注入Bean实例教程
前言 在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式,容易出现未被正确注入成功的情况. 本文将介绍一种在实际项目中基于动态的方式来提取Spring管理的Be ...
- 解决Spring+Quartz无法自动注入bean问题
问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...
- Spring框架知识总结-注入Bean的各类异常
近日整合sping和hibernate框架时遇到了一系列的异常,本次主要说明一下spring框架可能出现的异常及解决方案. 我们借助sping强大的bean容器管理机制,通过BeanFactory轻松 ...
- Spring 注解Autowired自动注入bean异常解决
错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...
- Spring为IOC容器注入Bean的五种方式
一 @Import导入组件,id默认是组件的全类名 //类中组件统一设置.满足当前条件,这个类中配置的所有bean注册才能生效: @Conditional({WindowsCondition.clas ...
随机推荐
- Exception Information
https://developer.apple.com/library/content/technotes/tn2004/tn2123.html Exception Information The t ...
- mybatis 使用参数
Parameter 1. 传入简单类型 JAVA代码: public User get(Long id) { return (User) getSqlSession().selectOne(&q ...
- 当ECharts碰到TWaver
百度公司的ECharts发展迅速,已经成为HTML5 Chart的佼佼者,这让大家骄傲:中国人终于也有世界级的开源通用UI产品了.正如其网站所说,它是百度的,是中国的,也是世界的.想想那些年,我们追逐 ...
- 我的MYSQL学习心得链接
http://www.cnblogs.com/lyhabc/p/3793524.html
- C++ Error C2662 cannot convert 'this' pointer from 'const *'
---恢复内容开始--- 这个错误在于一点:常量对象只能调用常量成员(函数\变量),不能调用非常量成员.另一方面,非常量对象,既可以调用常量成员,又可以调用非常量成员. class A { publi ...
- 利用CMD 創建新文件的機種方法
用 CMD 創建新文件 説明一下: 是在Windows的 CMD命令行模式下,或者在PowerShell命令行模式下創建新文件的機種方法. 創建空文件 cd.>a.txt cd.表示改变当前目录 ...
- UVA - 1623 Enter The Dragon(贪心)
题目: 思路: 读完题之后有了以下想法: 当遇到下雨的天,就找这个湖泊上一次下雨满了之后又一次不下雨的日期.有就在这个日期下记录被神龙喝干的湖的编号,没有就是不符合题意. 这个想法是对的,但是却被代码 ...
- Python学习-字符串函数操作2
字符串函数操作 find( sub, start=None, end=None):从左到右开始查找目标子序列,找到了结束查找返回下标值,没找到返回 -1 sub:需要查找的字符串 start=None ...
- BZOJ 1012 洛谷1198 最大数 maxnumber
用线段数维护即可 #include<cstdio> #include<algorithm> #define ls (cur<<1) #define rs (cur& ...
- FJoi2017 1月21日模拟赛 comparison(平衡树+thita重构)
题目大意: 经黄学长指出,此题原题出自2014湖北省队互测 没有人的算术 规定集合由二元组(A,B)构成,A.B同时也是两个这样的集合,即A.B本身也是二元组 规定二元组S为严格最小集合,S=(S,S ...