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

歡迎訪問 生活随笔!

生活随笔

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

javascript

Spring学习笔记-注入实战篇

發布時間:2025/4/5 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring学习笔记-注入实战篇 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

spring對各種數據類型都提供了注入支持,像java基本類型,對象,集合等,這篇文章以代碼實踐為主,代碼注釋中會解釋注入的細節

測試類中包含了我們編程中最常見的數據結構

package com.crazycoder2010.spring.injection; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private int age; private double weight; private String name; private boolean registed; private Properties properties; private Map<?, ?> attributes; private List<?> scores; private Set<?> sets; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isRegisted() { return registed; } public void setRegisted(boolean registed) { this.registed = registed; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Map<?, ?> getAttributes() { return attributes; } public void setAttributes(Map<?, ?> attributes) { this.attributes = attributes; } public List<?> getScores() { return scores; } public void setScores(List<?> scores) { this.scores = scores; } public Set<?> getSets() { return sets; } public void setSets(Set<?> sets) { this.sets = sets; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("age=").append(this.age).append("/n").append("weight=") .append(this.weight).append("/n").append("name=") .append(this.name).append("/n").append("registed=") .append(this.registed).append("/n").append("properties={"); StringBuilder tmp = new StringBuilder(); for (Object key : this.properties.keySet()) { tmp.append(key).append("=").append(properties.get(key)).append(","); } builder.append(tmp).append("}/n"); builder.append("attributes={"); StringBuilder map = new StringBuilder(); for (Object key : attributes.keySet()) { map.append(key).append("=").append(attributes.get(key)).append(","); } builder.append(map).append("}/n"); builder.append("scores={"); for (Object obj : scores) { builder.append(obj).append(","); } builder.append("}/n"); builder.append("sets={"); for (Object obj : sets) { builder.append(obj).append(","); } builder.append("}/n"); return builder.toString(); } } ?

看下在配置文件中如何對各種類型注入

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="student" class="com.crazycoder2010.spring.injection.Student"> <!-- 整形普通屬性直接寫入值即可 --> <property name="age" value="20"></property> <!-- 也可以搞個value標簽 --> <property name="name"> <value>張三</value> </property> <!-- 布爾型 --> <property name="registed" value="true"></property> <!-- double類型 --> <property name="weight" value="142.5"></property> <!-- list類型注入 --> <property name="scores"> <list><!-- 這個地方是個list --> <value>90</value> <value>98</value> <value>87</value> </list> </property> <!-- 注入map --> <property name="attributes"> <map><!--這個地方是map,注意內嵌的標簽是entry,是一個鍵值對 --> <entry key="a1"> <value>12345</value> </entry> <entry key="a2" value="hello-world"></entry> </map> </property> <!-- Properties注入 --> <property name="properties"> <props><!-- 雷同與map,但是注意prop標簽只有key屬性,因為value只能是String類型,prop對應的值直接寫在標簽內部 --> <prop key="p1">789</prop> <prop key="p2">567</prop> </props> </property> <!-- 集合注入 --> <property name="sets"> <set> <value>你好</value> <value>123</value> <null></null><!-- 這個地方我們注入一個null進去 --> </set> </property> </bean> </beans> ?

打印輸出

?

age=20

weight=142.5

name=張三

registed=true

properties={p2=567,p1=789,}

attributes={a1=12345,a2=hello-world,}

scores={90,98,87,}

sets={你好,123,null,}

?

附BaseSpring類

package com.crazycoder2010.spring; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BaseSpring { BeanFactory beanFactory = null; public Object getBean(String beanName) { init();//確保spring容器已經啟動 return beanFactory.getBean(beanName); } private void init() { if (beanFactory == null) { beanFactory = new ClassPathXmlApplicationContext(configFiles());//這個地方 } } protected String[] configFiles() { //這里作了個約定,默認的spring文件都放在與當前類平行的目錄下,名稱為applicationContex.xml //如要測試的類為com.crazycoder2010.spring.injection.StudentTest //則該測試的spring配置文件路徑為com/crazycoder2010/spring/injection/applicationContex.xml //子類可以復寫這個方法類實現定制化需求 String folder = getClass().getPackage().getName() .replaceAll("//.", "/") + "/"; String file = "applicationContext.xml"; return new String[] { folder + file }; } } ?

?

轉載于:https://www.cnblogs.com/javaexam2/archive/2011/03/03/2632567.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Spring学习笔记-注入实战篇的全部內容,希望文章能夠幫你解決所遇到的問題。

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