spring-注入对象list
生活随笔
收集整理的這篇文章主要介紹了
spring-注入对象list
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.創建項目
?? ?項目名稱:spring092901
二.添加jar包
?? ?commons-logging.jar
?? ?junit-4.4.jar
?? ?log4j.jar
?? ?spring-beans-3.2.0.RELEASE.jar
?? ?spring-context-3.2.0.RELEASE.jar
?? ?spring-core-3.2.0.RELEASE.jar
?? ?spring-expression-3.2.0.RELEASE.jar
三.添加配置文件
?? ?1.在項目中創建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄下添加配置文件
?? ??? ?配置文件名稱:applicationContext.xml
?? ??? ?配置文件內容:
?? ??? ?<?xml version="1.0" encoding="UTF-8"?>
?? ??? ?<beans xmlns="http://www.springframework.org/schema/beans"
?? ??? ??????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?? ??? ??????? xmlns:p="http://www.springframework.org/schema/p"
?? ??? ??????? xmlns:util="http://www.springframework.org/schema/util"
?? ??? ??????? xsi:schemaLocation="
?? ??? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
?? ??? ?http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
?? ??? ??? ?
?? ??? ?</beans>
四.創建實體bean
?? ?1.在src下創建包
?? ??? ?包名:cn.jbit.spring092901.domain
?? ?2.在包下創建bean
?? ??? ?bean名稱:CountryGeneralSituation.java
?? ??? ?bean內容:?? ?
?? ??? ?public class CountryGeneralSituation {
?? ??? ??? ?private String countryName;//國家名稱
?? ??? ??? ?private String womb;//發源地
?? ??? ??? ?private String relic;//遺址
?? ??? ??? ?//省略get and set ?? ?
?? ??? ?}
五.創建業務bean
?? ?1.在src下創建包
?? ??? ?包名:cn.jbit.spring092901.collection
?? ?2.在包下創建bean
?? ??? ?bean名稱:ListFromRef.java
?? ??? ?bean內容:
?? ??? ?public class ListFromRef {
?? ??? ??? ?private List<CountryGeneralSituation> list;
?? ??? ?
?? ??? ??? ?public List<CountryGeneralSituation> getList() {
?? ??? ??? ??? ?return list;
?? ??? ??? ?}
?? ??? ?
?? ??? ??? ?public void setList(List<CountryGeneralSituation> list) {
?? ??? ??? ??? ?this.list = list;
?? ??? ??? ?}
?? ??? ?}
?? ?3.在核心配置文件中配置bean
?? ??? ?<bean id="listFromRefBean" class="cn.jbit.spring092901.collection.ListFromRef">
?? ??? ??? ?<property name="list">
?? ??? ??? ??? ?<list>
?? ??? ??? ??? ??? ?<ref bean="babylonBean"/>
?? ??? ??? ??? ?</list>
?? ??? ??? ?</property>
?? ??? ?</bean>
?? ??? ?
?? ??? ?<bean id="babylonBean" class="cn.jbit.spring092901.domain.CountryGeneralSituation">
?? ??? ??? ?<property name="countryName" value="古巴比倫"></property>
?? ??? ??? ?<property name="womb" value="幼發拉底河流域,底格里斯河流域"></property>
?? ??? ??? ?<property name="relic" value="無"></property>
?? ??? ?</bean>
六.測試
?? ?1.在項目中創建test目錄
?? ??? ?/test
?? ?2.在test目錄下創建包
?? ??? ?cn.jbit.spring092901.collection
?? ?3.在包下 創建測試類
?? ??? ?類名:ListFromRefTest.java
?? ??? ?類內容:
?? ??? ?public class ListFromRefTest {
?? ??? ??? ?@Test
?? ??? ??? ?public void testCGS(){
?? ??? ??? ??? ?ClassPathXmlApplicationContext cxac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
?? ??? ??? ??? ?ListFromRef lfr = (ListFromRef) cxac.getBean("listFromRefBean");
?? ??? ??? ??? ?List list =lfr.getList();
?? ??? ??? ??? ?for (Object object : list) {
?? ??? ??? ??? ??? ?CountryGeneralSituation cgs = (CountryGeneralSituation) object;
?? ??? ??? ??? ??? ?System.out.println(cgs.getCountryName());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ?項目名稱:spring092901
二.添加jar包
?? ?commons-logging.jar
?? ?junit-4.4.jar
?? ?log4j.jar
?? ?spring-beans-3.2.0.RELEASE.jar
?? ?spring-context-3.2.0.RELEASE.jar
?? ?spring-core-3.2.0.RELEASE.jar
?? ?spring-expression-3.2.0.RELEASE.jar
三.添加配置文件
?? ?1.在項目中創建conf目錄
?? ??? ?/conf
?? ?2.在conf目錄下添加配置文件
?? ??? ?配置文件名稱:applicationContext.xml
?? ??? ?配置文件內容:
?? ??? ?<?xml version="1.0" encoding="UTF-8"?>
?? ??? ?<beans xmlns="http://www.springframework.org/schema/beans"
?? ??? ??????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?? ??? ??????? xmlns:p="http://www.springframework.org/schema/p"
?? ??? ??????? xmlns:util="http://www.springframework.org/schema/util"
?? ??? ??????? xsi:schemaLocation="
?? ??? ?http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
?? ??? ?http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
?? ??? ??? ?
?? ??? ?</beans>
四.創建實體bean
?? ?1.在src下創建包
?? ??? ?包名:cn.jbit.spring092901.domain
?? ?2.在包下創建bean
?? ??? ?bean名稱:CountryGeneralSituation.java
?? ??? ?bean內容:?? ?
?? ??? ?public class CountryGeneralSituation {
?? ??? ??? ?private String countryName;//國家名稱
?? ??? ??? ?private String womb;//發源地
?? ??? ??? ?private String relic;//遺址
?? ??? ??? ?//省略get and set ?? ?
?? ??? ?}
五.創建業務bean
?? ?1.在src下創建包
?? ??? ?包名:cn.jbit.spring092901.collection
?? ?2.在包下創建bean
?? ??? ?bean名稱:ListFromRef.java
?? ??? ?bean內容:
?? ??? ?public class ListFromRef {
?? ??? ??? ?private List<CountryGeneralSituation> list;
?? ??? ?
?? ??? ??? ?public List<CountryGeneralSituation> getList() {
?? ??? ??? ??? ?return list;
?? ??? ??? ?}
?? ??? ?
?? ??? ??? ?public void setList(List<CountryGeneralSituation> list) {
?? ??? ??? ??? ?this.list = list;
?? ??? ??? ?}
?? ??? ?}
?? ?3.在核心配置文件中配置bean
?? ??? ?<bean id="listFromRefBean" class="cn.jbit.spring092901.collection.ListFromRef">
?? ??? ??? ?<property name="list">
?? ??? ??? ??? ?<list>
?? ??? ??? ??? ??? ?<ref bean="babylonBean"/>
?? ??? ??? ??? ?</list>
?? ??? ??? ?</property>
?? ??? ?</bean>
?? ??? ?
?? ??? ?<bean id="babylonBean" class="cn.jbit.spring092901.domain.CountryGeneralSituation">
?? ??? ??? ?<property name="countryName" value="古巴比倫"></property>
?? ??? ??? ?<property name="womb" value="幼發拉底河流域,底格里斯河流域"></property>
?? ??? ??? ?<property name="relic" value="無"></property>
?? ??? ?</bean>
六.測試
?? ?1.在項目中創建test目錄
?? ??? ?/test
?? ?2.在test目錄下創建包
?? ??? ?cn.jbit.spring092901.collection
?? ?3.在包下 創建測試類
?? ??? ?類名:ListFromRefTest.java
?? ??? ?類內容:
?? ??? ?public class ListFromRefTest {
?? ??? ??? ?@Test
?? ??? ??? ?public void testCGS(){
?? ??? ??? ??? ?ClassPathXmlApplicationContext cxac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
?? ??? ??? ??? ?ListFromRef lfr = (ListFromRef) cxac.getBean("listFromRefBean");
?? ??? ??? ??? ?List list =lfr.getList();
?? ??? ??? ??? ?for (Object object : list) {
?? ??? ??? ??? ??? ?CountryGeneralSituation cgs = (CountryGeneralSituation) object;
?? ??? ??? ??? ??? ?System.out.println(cgs.getCountryName());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
本文轉自 ?素顏豬 ?51CTO博客,原文鏈接:http://blog.51cto.com/suyanzhu/1559514
總結
以上是生活随笔為你收集整理的spring-注入对象list的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL多实例安装配置指南
- 下一篇: pfSense2.32端口转发设置