Spring学习笔记-注入实战篇
?
spring對(duì)各種數(shù)據(jù)類(lèi)型都提供了注入支持,像java基本類(lèi)型,對(duì)象,集合等,這篇文章以代碼實(shí)踐為主,代碼注釋中會(huì)解釋注入的細(xì)節(jié)
測(cè)試類(lèi)中包含了我們編程中最常見(jiàn)的數(shù)據(jù)結(jié)構(gòu)
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(); } } ?
看下在配置文件中如何對(duì)各種類(lèi)型注入
<?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"> <!-- 整形普通屬性直接寫(xiě)入值即可 --> <property name="age" value="20"></property> <!-- 也可以搞個(gè)value標(biāo)簽 --> <property name="name"> <value>張三</value> </property> <!-- 布爾型 --> <property name="registed" value="true"></property> <!-- double類(lèi)型 --> <property name="weight" value="142.5"></property> <!-- list類(lèi)型注入 --> <property name="scores"> <list><!-- 這個(gè)地方是個(gè)list --> <value>90</value> <value>98</value> <value>87</value> </list> </property> <!-- 注入map --> <property name="attributes"> <map><!--這個(gè)地方是map,注意內(nèi)嵌的標(biāo)簽是entry,是一個(gè)鍵值對(duì) --> <entry key="a1"> <value>12345</value> </entry> <entry key="a2" value="hello-world"></entry> </map> </property> <!-- Properties注入 --> <property name="properties"> <props><!-- 雷同與map,但是注意prop標(biāo)簽只有key屬性,因?yàn)関alue只能是String類(lèi)型,prop對(duì)應(yīng)的值直接寫(xiě)在標(biāo)簽內(nèi)部 --> <prop key="p1">789</prop> <prop key="p2">567</prop> </props> </property> <!-- 集合注入 --> <property name="sets"> <set> <value>你好</value> <value>123</value> <null></null><!-- 這個(gè)地方我們注入一個(gè)null進(jìn)去 --> </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類(lèi)
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容器已經(jīng)啟動(dòng) return beanFactory.getBean(beanName); } private void init() { if (beanFactory == null) { beanFactory = new ClassPathXmlApplicationContext(configFiles());//這個(gè)地方 } } protected String[] configFiles() { //這里作了個(gè)約定,默認(rèn)的spring文件都放在與當(dāng)前類(lèi)平行的目錄下,名稱為applicationContex.xml //如要測(cè)試的類(lèi)為com.crazycoder2010.spring.injection.StudentTest //則該測(cè)試的spring配置文件路徑為com/crazycoder2010/spring/injection/applicationContex.xml //子類(lèi)可以復(fù)寫(xiě)這個(gè)方法類(lèi)實(shí)現(xiàn)定制化需求 String folder = getClass().getPackage().getName() .replaceAll("//.", "/") + "/"; String file = "applicationContext.xml"; return new String[] { folder + file }; } } ?
?
轉(zhuǎn)載于:https://www.cnblogs.com/javaexam2/archive/2011/03/03/2632567.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Spring学习笔记-注入实战篇的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 怎么把linix系统装进u盘 制作Lin
- 下一篇: JS获取按键码