日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

从头认识Spring-1.7 如何通过属性注入Bean?(1)-如何通过属性向对象注入值?...

發布時間:2023/12/10 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 从头认识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)-如何通过属性向对象注入值?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。