博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?...
阅读量:4308 次
发布时间:2019-06-06

本文共 3217 字,大约阅读时间需要 10 分钟。

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

注意:这里首字母能够不区分大写和小写

也就是

是一样的

 可是像以下的全然的大写,就会抛异常

 

 

測试输出:

0

Blueberry Cheesecake
7.0

 

总结:这一章节主要介绍了如何通过属性向对象注入值,还有中间须要注意的大写和小写的问题

 

文件夹: 

 

我的github:

 

转载于:https://www.cnblogs.com/gccbuaa/p/7249788.html

你可能感兴趣的文章
期货市场技术分析05_交易量和持仓兴趣
查看>>
TB交易开拓者入门教程
查看>>
TB创建公式应用dll失败 请检查用户权限,终极解决方案
查看>>
python绘制k线图(蜡烛图)报错 No module named 'matplotlib.finance
查看>>
talib均线大全
查看>>
期货市场技术分析06_长期图表和商品指数
查看>>
期货市场技术分析07_摆动指数和相反意见理论
查看>>
满屏的指标?删了吧,手把手教你裸 K 交易!
查看>>
不吹不黑 | 聊聊为什么要用99%精度的数据回测
查看>>
对于模拟交易所引发的思考
查看>>
高频交易的几种策略
查看>>
量化策略回测TRIXKDJ
查看>>
量化策略回测唐安奇通道
查看>>
CTA策略如何过滤部分震荡行情?
查看>>
量化策略回测DualThrust
查看>>
量化策略回测BoolC
查看>>
量化策略回测DCCV2
查看>>
mongodb查询优化
查看>>
五步git操作搞定Github中fork的项目与原作者同步
查看>>
git 删除远程分支
查看>>