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());
                }
            }

        }

本文转自  素颜猪  51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1559514

相关文章
|
22天前
|
前端开发 Java Spring
Spring MVC 是如何对对象参数进行校验的
【6月更文挑战第4天】对象参数校验是使用 SpringMVC 时常用的功能,这篇文章尝试分析了,Spring 是如何实现这一功能的。
31 5
|
1月前
|
XML Java 程序员
Spring6框架中依赖注入的多种方式(推荐构造器注入)
依赖注入(DI)是一种过程,对象通过构造函数参数、工厂方法的参数或在对象实例构建后设置的属性来定义它们的依赖关系(即与其一起工作的其他对象)。
40 3
|
2天前
|
Java Spring 容器
spring如何进行依赖注入,通过set方法把Dao注入到serves
spring如何进行依赖注入,通过set方法把Dao注入到serves
|
12天前
|
存储 XML Java
在 Java 中,Spring 框架提供了一种更加简单的方式来读取和存储对象
【6月更文挑战第18天】Java Spring 框架利用注解简化对象管理:@Component(及衍生注解@Service等)标注Bean类,自动注册到容器;@Autowired用于字段或方法,实现依赖注入,提升灵活性,减少XML配置。
17 2
|
18天前
|
Java Spring
解决 Spring 中 Prototype Bean 注入后被固定的问题
【6月更文挑战第8天】学习 Spring 框架内不原理的意义就是,当遇到问题时,分析出原因,就可以从多个切入点,利用 Spring 的特性,来解决问题。
29 2
|
20天前
|
JSON 前端开发 Java
Spring MVC 级联对象参数校验
【6月更文挑战第6天】在 Spring MVC 的使用过程中,我们会发现很多非常符合直觉的功能特性,但往往我们会习惯这种「被照顾得很好」的开发方式,依靠直觉去判断很多功能特性的用法。
19 1
|
25天前
spring-boot报错循环注入报错:has been injected into other beans
spring-boot报错循环注入报错:has been injected into other beans
66 3
|
3天前
|
Java Linux 程序员
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
|
25天前
|
Java
Java list中的对象转为list,list中的对象转为map
Java list中的对象转为list,list中的对象转为map
15 1
|
1月前
|
存储 Java 对象存储
Spring 更简单的读取和存储对象
Spring 更简单的读取和存储对象