从头认识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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/268798.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

chrome java虚拟机_JATT:谷歌的Java虚拟机自动调整工具

JATT是google的Java虚拟机自动调整工具&#xff0c;它是一个开源软件工具&#xff0c;用于优化Java虚拟机(JVM)。JATT是基于OpenTuner开发的&#xff0c;OpenTuner是另一个开源软件框架&#xff0c;用于构建域特定的自动微调器。 JATT专门用于调整HotSpot JVM&#xff0c;这是最…

初识北京

话说话说就来了北京&#xff0c;长期在南方生活惯了&#xff0c;来这里还真有点不习惯。。。 1.、北京不算整洁的城市&#xff0c;作为一个有历史的城市&#xff0c;电线乱牵&#xff0c;路面邋遢&#xff0c;当然&#xff0c;对于一贯低要求的中国人来说问题也不是很严重&…

jsb调用java_在JS代码中使用反射调用java代码注意事项(附webview使用方法)(转)...

本文是推荐使用过jsb.reflection的开发者进行阅读。关于jsb.reflection的说明请参照&#xff1a;我们在代码编写过程中&#xff0c;通常会需要在js脚本中调用到java代码或者Objective-C的代码。例如&#xff1a;接入sdk&#xff0c;显示webview&#xff0c;使用原生代码&#x…

Excel表格内容导出到页面

引入org.apache.poi.ss.usermodelpublic void addExcelBooks() throws Exception { HttpServletRequest request ServletActionContext.getRequest(); String filepath request.getParameter("filepath"); String fileType filepath.substring(filepath.lastIndex…

综述ASP.NET下的AJAX模式

本文内容: 一、导言 二、XMLHttpWebForm模式 三、XMLHttpHttpHandler模式 四、ASP.NET 2.0/3.5回调模式 五、AJAX框架模式 -------------------------------------------------------------------------------------------------- 一、导言 在这篇文章中&#xff0c;将介绍…

java 获取类加载器_java-如何从类加载器获取类路径?

更新&#xff1a;我下面的原始答案很不充分&#xff0c;因为我花了三年的时间开发FastClasspathScanner&#xff0c;并提交了大量关于某些类路径环境无法使用该库的错误报告。 FastClasspathScanner现在可以处理许多复杂的类路径规范机制。 在一般情况下(即使扫描它)&#xff0…

JIRA-6.3.6安装与破解

首先下载JIRA-6.3.6的安装包&#xff1a; wget http://www.atlassian.com/software/jira/downloads/binary/atlassian-jira-6.3.6.tar.gz 在这里笔者新建JIRA用户来专门运行JIRA useradd jira passwd jira 把包解压到jira的宿主目录(/home/jira)下&#xff1a; tar -zxvf atla…

网店健康成长之路

对网店管理新规&#xff0c;前天中国社科院信息化研究中心举办中国网络零售产业环境学术研讨会&#xff0c;各界专家共同建议&#xff1a;国家尽快出台“电子商务促进法”&#xff0c;以规范各地政府、不同部门的监管行为。何时管&#xff1a;监管时机未到“网络零售产业规模占…

[development][profile][dpdk] KK程序性能调优

KK程序&#xff1a; 1. 两个线程&#xff0c;第一个从DPDK收包&#xff0c;通过一个ring数据传递给第二个线程。第二个线程将数据写入共享内存。 2. 第二个内存在发现共享内存已满时&#xff0c;会直接丢弃数据。 3. 线程二有个选项debug&#xff0c;用于每一次ring_dequeue之后…

求二进制数中1的个数

要求&#xff1a;对于一个字节&#xff08;8bit&#xff09;的变量&#xff0c;求其二进制表示中“1”的个数&#xff0c;要求算法的执行效率尽可能地高。 大多数的读者都会有这样的反应&#xff1a;这个题目也太简单了吧&#xff0c;解法似乎也相当地单一&#xff0c;不会有太…

CentOS安装glibc-2.14

到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.xz tar glibc-2.14.tar.gz cd glibc-2.14 mkdir build cd build ../configure --prefix/usr/local/glibc-2.14 make -j4 su xxxx make install 看看现在libc.so.6在哪个位置&#xff0c;然后修改软链接 ln -s /usr/local/gl…

cesium米转换经纬度_cesium 常见坐标系及坐标转换(工具篇)

/** Description: 坐标转换工具&#xff0c;下面的坐标转换默认都是基于WGS84 椭球体*/class Degrees {constructor(longitude, latitude, height 0) {this.longitude longitude;this.latitude latitude;this.height height;}}Cesium.Degrees Degrees;//不建议这样直接修改…

sql 2005 try catch

--删除卡的记录 delete from ObjTransportCard where CardIdCardId --删除交易的记录 delete from ObjTransaction where CardIdCardId 在Sql 2000中,为了成功执行这个存储过程,我需要在每一条语句后面去判断ERROR,如果有错,则执行回滚.上例只是我举的一个简单的例子,如果在一个…

selenium拖动元素java_【自动化测试】Java+Selenium操作页面元素(合集)

本文基于Java语言&#xff0c;依托于Eclipse工具&#xff0c;使用Selenium框架&#xff0c;主要介绍在Selenium中&#xff0c;如何操作Web页面中的各种元素。Eclipse 搭建1.1、Eclipse 配置1.2、引入依赖包修改pom.xml文件在dependencys节点下&#xff0c;添加如下内容&#xf…

面试题 锁消除是什么

锁消除是在编译器级别的事情。 在即时编译器时&#xff0c;如果发现不可能被共享的对象&#xff0c;则可以消除这些对象的锁操作。 也许你会觉得奇怪&#xff0c;既然有些对象不可能被多线程访问&#xff0c;那为什么要加锁呢&#xff1f;写代码时直接不加锁不就好了。 但是…

通过日志恢复SQL Server的历史数据

园子里前段时间发过一篇通过日志恢复MSSQL数据例子 &#xff0c;我总结一下 通过日志还原&#xff0c;最重要的是&#xff1a; 1.必须有一个完整的备份&#xff0c;且这个备份必须是在修改、删除数据之前做的。 2.在更新、删除数据之后&#xff0c;做日志备份&#xff0c;该log…