从头认识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 ...
随机推荐
- 看得懂的区块链,看不清的ICO人心【转】
比特币又开始下跌了,是狂欢尽头还是又一波调整,无从得知,背后的乱象会让监管者继续心烦,而这乱象对我来说,有时候会有些心寒. 你说我怎么可能想到,我一个写程序的人,突然有一天会发现,朋友圈里有一些搞技术 ...
- iOS-android-windowsphone等移动终端平台开发流程图
到了公司后,半个月时间就是在熟悉下面这张图里的流程, 项目流程图: 下面是我对这张图的一些理解:
- median(NOIP模拟赛Round 3)
也是神奇的题目.. 原题传送门 首先看到这道题目,很明显我们需要线性算法 1.前缀和+统计 2.DP+统计 对于第一种算法,我们可以对于任何一个a[i]对b进行比较,如果大于b标上1,等于b标上0,小 ...
- VLC for iOS 2.3.0
http://www.cocoachina.com/bbs/read.php?tid=231898 VLC for iOS 2.3.0 本帖属于CocoaChina会员发表,转帖请写明来源 ...
- FolderSize磁盘占用详情工具
FolderSize
- mysql故障(找不mysql命令)
[root@slave support-files]# mysql -uroot -p123-bash: mysql: command not found #我的mysql编译安装指定的路径是--ba ...
- 【原创】SSAS-引用维度与多数据源、多数据源视图引发分区错误
背景: 最近有个项目,有32家分公司,集团总部需要取这个32家分公司数据做分析,由于每个分公司的数据都比较庞大,所以最终方案是每个分公司一个DW,在cube搭建过程中将每个公司数据作为一个分区数据的来 ...
- 一篇好文档,请Thymeleaf Layout Dialect
Thymeleaf Layout Dialect https://ultraq.github.io/thymeleaf-layout-dialect/ This will introduce the ...
- Codeforces Round #446 (Div. 2) C. Pride【】
C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 「kuangbin带你飞」专题十九 矩阵
layout: post title: 「kuangbin带你飞」专题十九 矩阵 author: "luowentaoaa" catalog: true tags: mathjax ...