这一章节我们来讨论一下如何通过属性注入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. libcurl网络连接使用tcp/ip

    CURL *curl; CURLcode res; const char *request = "GETas.xxxxE测试发送"; curl_socket_t sockfd; / ...

  2. Linux下find命令参数及用法详解

    由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下.即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限.在运行一个非常 ...

  3. Struts2.0中ActionInvocation使用

    Interceptor的接口定义没有什么特别的地方,除了init和destory方法以外,intercept方法是实现整个拦截器机制的核心方法.而它所依赖的参数ActionInvocation则是我们 ...

  4. centos7配置环境LNMP

    #yum安装epel-release第三方软件包 yum install epel-release #要验证EPEL仓库是否建立成功 yum repolist xshell访问系统出现中文乱码的解决方 ...

  5. 翻煎饼_简单模拟_C++

    一.题目描述(懒人可直接跳过看题目概述) 题目来源: SWUST OJ  题目0254 http://acm.swust.edu.cn/problem/0254/ 二.问题概述 给出一列数,每次可将包 ...

  6. VLC-开源播放器编译

    http://blog.sina.com.cn/s/blog_7b9d64af0101jpvy.html 需要VLC 在iOS开发中,如果你的程序中播放视频,并且多媒体播放功能是你iOS程序的核心功能 ...

  7. gitlab 搭建与迁移

    #下载gitlabhttps://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-10.1.3-ce.0.el6.x86_64.rpm ...

  8. java1.7集合源码阅读:ArrayBlockingQueue

    ArrayBlockingQueue是一个先进先出线程安全的队列,队列头部是进入队列时间最长的元素,队尾是进入队列时间最短的元素,同时队列的最大容量是固定的. 先看类定义: public class ...

  9. django视图重定向

    # 原创,转载请留言联系 当请求访问到某个视图时,我们想让它重定向到其他页面,应该怎么做呢? 1.HttpResponseRedirect 需求:当我们访问127.0.0.1/my_redirect时 ...

  10. jmeter压测脚本编写与静态文件处理

    一.压测脚本编写 概述:工具为谷歌浏览器-->F12-->Network,访问被测站点,通过其中的请求的地方来构造压测脚本 二.静态文件处理 概述:静态文件包括css/js/图片等,它们有 ...