这一章节我们来讨论一下如何通过属性注入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

我的github:https://github.com/raylee2015/my_new_spring

从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?的更多相关文章

  1. 【Spring源码解读】bean标签中的属性(一)你可能还不够了解的 scope 属性

    scope 属性说明 在spring中,在xml中定义bean时,scope属性是用来声明bean的作用域的.对于这个属性,你也许已经很熟悉了,singleton和prototype信手捏来,甚至还能 ...

  2. spring Ioc容器之使用XML配置Bean

    1.项目截图 2.创建xml文件 3.打印机接口 package com.example.demo.computerTest; public interface Printer { void init ...

  3. spring boot 动态注入bean

    方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...

  4. spring 注解注入bean

    通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...

  5. spring实战一:装配bean之注入Bean属性

    内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...

  6. Spring入门(5)-自动装配Bean属性

    Spring入门(5)-自动装配Bean属性 本文介绍如何装配Bean属性. 0. 目录 ByName ByType constructor 默认自动装配 混合使用自动装配和显示装配 1. ByNam ...

  7. Spring入门(4)-注入Bean属性

    Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...

  8. 【Spring源码解读】bean标签中的属性

    说明 今天在阅读Spring源码的时候,发现在加载xml中的bean时,解析了很多标签,其中有常用的如:scope.autowire.lazy-init.init-method.destroy-met ...

  9. Spring IOC 容器源码分析 - 填充属性到 bean 原始对象

    1. 简介 本篇文章,我们来一起了解一下 Spring 是如何将配置文件中的属性值填充到 bean 对象中的.我在前面几篇文章中介绍过 Spring 创建 bean 的流程,即 Spring 先通过反 ...

  10. 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值

    7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...

随机推荐

  1. 理想中的SQL语句条件拼接方式 (二)

    问题以及想要的效果,不重复叙述,如果需要的请先看 理想中的SQL语句条件拼接方式 . 效果 现在有2个类映射数据库的2张表,结构如下: public class User { public int U ...

  2. C++嵌套类及对外围类成员变量的访问

    C++嵌套类及对外围类成员变量的访问 在一个类中定义的类称为嵌套类,定义嵌套类的类称为外围类. 定义嵌套类的目的在于隐藏类名,减少全局的标识符,从而限制用户能否使用该类建立对象.这样可以提高类的抽象能 ...

  3. ffmpeg muxer 参数简要说明

    参数 值 说明 movflags MP4 Muxer 标记 rtphint 增加RTP的hint track empty_moov 初始化空的moov box frag_keyframe 在视频关键帧 ...

  4. redis介绍与配置

    redis 是什么 我们都知道 mysql.oracle 是关系型数据库,它们的特点就是像一个 Excel 表格一样,而且都是用 SQL 语句.但是你可能看到一种东西叫NoSQL.NoSQL 泛指非关 ...

  5. 使用Vagrant打造Linux开发环境

    1.安装Oracle VM VirtualBox.vagrant,安装完成需重启电脑 Oracle VM VirtualBox安装包下载:https://www.virtualbox.org/wiki ...

  6. TP5使用技巧

    1.fetchSql用于直接返回SQL而不是执行查询,适用于任何的CURD操作方法. 例如:$result = Db::table('think_user')->fetchSql(true)-& ...

  7. J.U.C并发框架源码阅读(十五)CopyOnWriteArrayList

    基于版本jdk1.7.0_80 java.util.concurrent.CopyOnWriteArrayList 代码如下 /* * Copyright (c) 2003, 2011, Oracle ...

  8. 使用iSCSI Target创建集中式安全存储(一)

    iSCSI 是一种块级别的协议,用于通过TCP/IP网络共享原始存储设备,可以用已经存在的IP和以太网如网卡.交换机.路由器等通过iSCSI协议共享和访问存储.iSCSI target是一种由远程iS ...

  9. 计蒜客 28317.Growling Gears-一元二次方程的顶点公式 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 G)

    G. Growling Gears 传送门 此题为签到题,直接中学的数学知识点,一元二次方程的顶点公式(-b/2*a,(4*a*c-b*b)/4*a):直接就可以得到结果. 代码: #include& ...

  10. ZOJ 3937 More Health Points (2016 浙江省赛 B题,可持久维护凸壳)

    题目链接  2016 ZJCPC Problem B 题意  CF 660F的树上版本. 其他做的方法都差不多,关键是把凸壳放到树上. 每次确定扔掉几个元素的时候直接$O(1)$修改(先不清楚这个位置 ...