从头认识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;}@Overridepublic 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 {@Autowiredprivate ApplicationContext applicationContext;@Testpublic 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
我的github:https://github.com/raylee2015/my_new_spring
转载于:https://www.cnblogs.com/gccbuaa/p/7249788.html
从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?...相关推荐
- Spring学习笔记:2(IOC装配Bean之xml方式)
xml配置方式装配Bean 本文借鉴于:https://www.cnblogs.com/qdhxhz/p/6511887.html Spring框架Bean实例化的方式提供了三种方式实例化Bean 构 ...
- Spring攻略学习笔记(13)------继承Bean配置
一:知识点 在Spring IoC容器中配置Bean时,可能拥有一个以上的共享某些公用配置的Bean,比如属性和<bean>元素中的属性.你常常需要为多个Bean重复这些配置. Sprin ...
- Spring注入Bean的七种方式
点击关注公众号,利用碎片时间学习 通过注解注入Bean 背景 我们谈到Spring的时候一定会提到IOC容器.DI依赖注入,Spring通过将一个个类标注为Bean的方法注入到IOC容器中,达到了控制 ...
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的Java ...
- Spring 注入 Bean 的七种方式
来源:juejin.cn/post/6844903813753602056 通过注解注入Bean 背景 我们谈到Spring的时候一定会提到IOC容器.DI依赖注入,Spring通过将一个个类标注为B ...
- Spring IOC 容器源码分析 - 填充属性到 bean 原始对象
1. 简介 本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的.我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反 ...
- Spring的Bean实例化、属性注入、对象注入、复杂注入(基于xml配置方式)
一.Bean实例化的三种方式: (1)使用类的无参构造创建 (2)使用静态工厂创建 (3)使用实例工厂创建 代码实例: (1)项目结构: (2)在pom.xml中导入spring的核心jar包依赖: ...
- springMVC通过spring.xml对属性注入bean值(工厂模式)
springMVC通过spring.xml对属性注入bean值,该bean是一个map容器: <bean id="configXMLCreatorFactory" class ...
- Spring 注入集合的成员变量属性
Spring支持list,set,map和prop四种集合类型的注入. 看一个例子:JavaCollection这个类包含了上述介绍的4种集合类型的成员变量: import java.util.*; ...
最新文章
- 学Python怎么样 发展前景如何
- sonar 中质量指标(度量)
- 如何从OnDocumentComplete事件中获得当前frame的html代码
- apipost提示error:invalid protocol的解决方案
- Hdu - 1210 - Eddy's 洗牌问题
- Git时出现“error: 源引用表达式 main 没有匹配 error: 推送一些引用到 ‘https://github.com/***.git‘ 失败”的错误提示
- 自定义按键连发工具_微软 PowerToys 增强工具,提升Win10 效率利器
- 1月10日云栖精选夜读:专访金榕:四年蜕变,阿里iDST是如何登上浪潮之巅的?...
- Java实现文件复制功能
- 如何安装固态硬盘和重装系统???
- [图像处理][Matlab] fspecial函数详解
- 产品经理之如何做好演讲
- python默认数据转换_Python之数据转换
- [联合集训6-25] 蓝雨 线段树+主席树+hash
- 三十多岁,如何打理尴尬年龄里的凌乱人生
- php 如何实现心跳包,Socket心跳机制-JS+PHP实现
- 信用评分卡Credit Scorecards (1-7)
- GUI线程安全详解(三)
- 基于ESP8266-12f 最小系统接线说明
- Hystrix-断路器核心原理
热门文章
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite...
- mongoose --- 建立一个集合规则,并导出.
- c++ --- 字符串中的标点符号
- JavaScriptBreak 语句 continue 语句
- 分布式锁与实现(一)——基于Redis实现
- 内存管理1retain和release
- Docker学习(三):镜像
- 你研究过单例么?这样写单例效率最高.
- DOM Element
- console程序也有版本和图标