从头认识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 ...
随机推荐
- Gradle打包jar可执行程序
1. 使用springboot插件 apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'spring-boot' buildscri ...
- Windows提高_1.3文件操作
文件操作 不带句柄的文件操作 // 1. 拷贝文件,第三个参数为 FALSE 表示会覆盖 // CopyFile(L"D:\\1.txt", L"E:\\2.txt&qu ...
- CAD实现文档坐标到视区坐标的转换(com接口Delphi语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- Python学会之后,一般能拿到多少工资?
Python在约40年前出现以来,已经有数以千计基于这项技术的网站和软件项目,Python因其独有的特点从众多开发语言中脱颖而出,深受世界各地的开发者喜爱. 随着Python的技术的流行,Python ...
- 如何做到在webpack打包vue项目后,在外部动态修改配置文件
在我们做完vue项目后,只需要执行 npm run dist 就可以轻松进行打包转测试,可是如果我们临时需要修改一些配置文件比如域名,这时候我们就有点懵逼了,那就修改了再重新打一次包? NO NO N ...
- 登录deepin 15.9后不显示任务栏,无法操作
一直觉得在Linux下编程很酷,所以决定装个Deepin试试,安装很顺利,然后搭建了开发环境,写了一个简单程序,觉得挺不错的. 哪知第二天一开机,登录后找不到任务栏了,做不了啥操作,走接傻眼了,直觉以 ...
- linux ltrace-跟踪进程调用库函数的情况
当前位置:硬件 | 监测 | 内核 | Shell / 性能监测与优化 /ltrace ltrace命令是用来跟踪进程调用库函数的情况. 语法 ltrace [option ...] [command ...
- 关于js中的事件委托小案例
需求:页面上有一个按钮,和一个空的ul,要求点击按钮,会给ul中动态添加li元素,然后,点击动态添加的元素,在控制台上输出,这是第几个元素 <ul> </ul> <but ...
- JavaScript 面向对象的编程(二) 类的封装
类的定义 方式一 var Book = function(id, name, price){ //私有属性,外部不能直接访问 var num = 1; //私有方法, function checkId ...
- SGU 485 Arrays
485. Arrays Time limit per test: 1.75 second(s)Memory limit: 262144 kilobytes input: standardoutput: ...