从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?
这一章节我们来讨论一下如何通过属性注入Bean?
这一章节分为两部分,第一部分我们通过属性向对象注入值,第二部分我们通过属性向对象注入还有一个对象的引用。
1.如何通过属性向对象注入值?
(1)domain
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7;
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;
}
}
这一个领域类我们仅仅须要一个Cake就够了。可是我们在里面会加上名称(name)和大小(size)这两个属性,然后我们通过Spring来帮我们赋值。
(2)測试类:
package com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7; 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_7/ApplicationContext-test.xml" })
public class CakeTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
Cake cake = applicationContext.getBean(Cake.class);
System.out.println(cake.getId());
System.out.println(cake.getName());
System.out.println(cake.getSize());
}
}
没什么特别。仅仅须要get那个Bean出来,然后打印一下几个属性就可以。
(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: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="cake"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake">
<property name="name" value="Blueberry Cheesecake" />
<property name="size" value="7" />
</bean> </beans>
配置文件比較重要,我们在Bean里面须要插入property这个标签,然后name这个属性须要跟我们的domain类的属性名字一样。
注意:这里首字母能够不区分大写和小写
也就是
<bean id="cake"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake">
<property name="Name" value="Blueberry Cheesecake" />
<property name="Size" value="7" />
</bean>
和
<bean id="cake"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake">
<property name="name" value="Blueberry Cheesecake" />
<property name="size" value="7" />
</bean>
是一样的
可是像以下的全然的大写,就会抛异常
<bean id="cake"
class="com.raylee.my_new_spring.my_new_spring.ch01.topic_1_7.Cake">
<property name="NAME" value="Blueberry Cheesecake" />
<property name="SIZE" value="7" />
</bean>
測试输出:
0
Blueberry Cheesecake
7.0
总结:这一章节主要介绍了如何通过属性向对象注入值,还有中间须要注意的大写和小写的问题
文件夹:http://blog.csdn.net/raylee2007/article/details/50611627
从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?的更多相关文章
- 【Spring源码解读】bean标签中的属性(一)你可能还不够了解的 scope 属性
scope 属性说明 在spring中,在xml中定义bean时,scope属性是用来声明bean的作用域的.对于这个属性,你也许已经很熟悉了,singleton和prototype信手捏来,甚至还能 ...
- spring Ioc容器之使用XML配置Bean
1.项目截图 2.创建xml文件 3.打印机接口 package com.example.demo.computerTest; public interface Printer { void init ...
- spring boot 动态注入bean
方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...
- spring 注解注入bean
通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...
- spring实战一:装配bean之注入Bean属性
内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...
- Spring入门(5)-自动装配Bean属性
Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...
- Spring入门(4)-注入Bean属性
Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...
- 【Spring源码解读】bean标签中的属性
说明 今天在阅读Spring源码的时候,发现在加载xml中的bean时,解析了很多标签,其中有常用的如:scope.autowire.lazy-init.init-method.destroy-met ...
- Spring IOC 容器源码分析 - 填充属性到 bean 原始对象
1. 简介 本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的.我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反 ...
- 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值
7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...
随机推荐
- poj 3311 floyd+dfs或状态压缩dp 两种方法
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6436 Accepted: 3470 ...
- 华中农业大学第四届程序设计大赛网络同步赛 J
Problem J: Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1766 Solved: 299[Subm ...
- 知问前端——对话框UI(二)
dialog()方法的事件 除了属性设置外,dialog()方法也提供了大量的事件,这些事件可以给各种不同状态时的对话框提供回调函数,这些回调函数中的this值等于对话框内容的div对象,不是整个对话 ...
- Flask学习总结笔记推荐
https://blog.csdn.net/kikaylee/article/category/6551426
- git的使用03
之前我们写的都是将代码存在本地,我们还可以将代码github官网上,放在github的服务器上去托管
- WPF+MVVM数据绑定问题集锦
1. 数据绑定的问题 在使用数据绑定时,一般使用 ObservableCollection<T> 类,不使用list列表集合,因为list数据发生变化时,UI界面不更新,而Observa ...
- uva12265 贩卖土地 单调栈
输入一个n*m的矩阵,每个格子可能是空地,也可能是沼泽.对于每个空地格子,求出以它为右下角的空矩形的最大周长,然后统计每个周长出现了多少次. 输入包含多组测试数据,第一行输入一个正整数N,表示输入样例 ...
- BZOJ1588 营业额统计 (Splay)
营业额统计 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额. ...
- InputSplit—>RecordReder—>map(key,value,context)的过程解析
上图首先描述了在TaskTracker端Task(MapTask.ReduceTask)的执行过程,MapTask(org.apache.hadoop.mapred)首先被TaskRunner调用,然 ...
- Javascript模块化编程(二):AMD规范(转)
这个系列的第一部分介绍了Javascript模块的基本写法,今天介绍如何规范地使用模块. (接上文) 七.模块的规范 先想一想,为什么模块很重要? 因为有了模块,我们就可以更方便地使用别人的代码,想要 ...