工作中,领导认为properties文件太恶心(其实就不想让用),还好properties文件就是一个字符串的问题,下面我将Spring怎么实例化String、File、Map、List对象的常见方法列下面。供大家参考。废话不多说,还是上代码吧。
<?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.0.xsd"> <!-- SOLR_HOME:直接文件访问 SolrResourceLoader.locateSolrHome();--> <bean id="uesec.core.SOLR_HOME.txt" class="java.lang.String" singleton="true"> <constructor-arg> <value>.\\puese_home\\</value> </constructor-arg> </bean> <bean id="uesec.core.SOLR_XML.txt" class="java.io.File" singleton="true"> <constructor-arg index="0"> <ref bean="uesec.core.SOLR_HOME.txt" /> </constructor-arg> <constructor-arg index="1" type="java.lang.String"> <value>solr.xml</value> </constructor-arg> </bean> <!-- solr服务:内核名称 --> <bean id="uesec.core.SOLR_CORE.txt" class="java.lang.String" singleton="true"> <constructor-arg type="java.lang.String"> <value>TXT</value> </constructor-arg> </bean> <bean id="uesec.core.SOLR_CoreContainer.txt" class="org.apache.solr.core.CoreContainer" singleton="true"> <constructor-arg index="0" type="java.lang.String"> <ref bean="uesec.core.SOLR_HOME.txt" /> </constructor-arg> <constructor-arg index="1" type="java.io.File"> <ref bean="uesec.core.SOLR_XML.txt" /> </constructor-arg> </bean> <!-- Construct List --> <bean id="nameList" class="java.util.ArrayList"> <constructor-arg> <list> <value>liuyu</value> </list> </constructor-arg> </bean> <!-- Construct Map --> <bean id="nameMap" class="java.util.HashMap"> <constructor-arg> <map> <entry key="liuyu" value-ref="nameList" /> </map> </constructor-arg> </bean> <!-- Inject List and Map --> <bean id="myDemo" class="spring.inject.demo.MyDemo"> <property name="nameList"> <ref local="nameList" /> </property> <property name="nameMap"> <ref local="nameMap" /> </property> </bean> </beans>
public class MyDemo { private List<String> nameList = null; private Map<String, List<String>> nameMap = null; public List<String> getNameList() { return nameList; } public void setNameList(List<String> nameList) { this.nameList = nameList; } public Map<String, List<String>> getNameMap() { return nameMap; } public void setNameMap(Map<String, List<String>> nameMap) { this.nameMap = nameMap; } }
时间: 2024-10-07 19:56:39