`
yinghuayu1324117
  • 浏览: 70505 次
  • 性别: Icon_minigender_2
  • 来自: 保定
文章分类
社区版块
存档分类

Spring中的集合

阅读更多

List

 

 

@Te
 public void testList(){

       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:list.xml");

       EmpServiceImpl empServiceImpl = (EmpServiceImpl) ac.getBean("empServiceImpl");

       List<AddressServiceImpl> list = empServiceImpl.getList1();

    }

 

 

package cn.csdn.service;

import java.util.List;

public class EmpServiceImpl {

    private AddressServiceImpl addressServiceImpl;

    private List<AddressServiceImpl> list1;
   
 public void setAddressServiceImpl(AddressServiceImpl addressServiceImpl) {

       this.addressServiceImpl = addressServiceImpl;

    }

    public AddressServiceImpl getAddressServiceImpl() {

       return addressServiceImpl;

    }

    public List<AddressServiceImpl> getList1() {

       return list1;

    }

    public void setList1(List<AddressServiceImpl> list1) {

       this.list1 = list1;

    }
}

 

 

 

 

@Test
    public void testList(){
       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:list.xml");
       EmpServiceImpl empServiceImpl = (EmpServiceImpl) ac.getBean("empServiceImpl");
       List<AddressServiceImpl> list = empServiceImpl.getList1();
    }

 

 

<?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">

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton">

    <property name="list">

       <list>

       <value>1</value>

       <value>2</value>

       <value>3</value>

       <value>4</value>

       </list>

    </property>

</bean>

</beans>

 

Map

package cn.csdn.service;

import java.util.Map;

public class EmpServiceImpl {

    private AddressServiceImpl addressServiceImpl;

    private Map<String,Integer> map;

    public void setAddressServiceImpl(AddressServiceImpl addressServiceImpl) {

       this.addressServiceImpl = addressServiceImpl;

    }

    public Map<String, Integer> getMap() {

       return map;

    }

    public void setMap(Map<String, Integer> map) {

       this.map = map;

    }

    public AddressServiceImpl getAddressServiceImpl() {

       return addressServiceImpl;

    }

}

 

 

@Test

    public void testMap(){

        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:map.xml");

         EmpServiceImpl empServiceImpl = (EmpServiceImpl) ac.getBean("empServiceImpl");   

        Map<String,Integer> map = empServiceImpl.getMap();

    }

 

 

   

Set

package cn.csdn.service;

import java.util.Set;

public class EmpServiceImpl {

    private AddressServiceImpl addressServiceImpl;

    private Set<AddressServiceImpl> set; 

     public void setAddressServiceImpl(AddressServiceImpl addressServiceImpl) {

       this.addressServiceImpl = addressServiceImpl;

    }

    public Set<AddressServiceImpl> getSet() {

       return set;

    }

    public void setSet(Set<AddressServiceImpl> set) {

       this.set = set;

    }

    public AddressServiceImpl getAddressServiceImpl() {

       return addressServiceImpl;

    }

}

 @Test

    public void testSet(){

       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:set.xml");

       EmpServiceImpl empServiceImpl = (EmpServiceImpl) ac.getBean("empServiceImpl");

       Set<AddressServiceImpl> set = empServiceImpl.getSet();

       System.out.println(set.size());
    }

<?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">

  <bean id="addressServiceImpl" class="cn.csdn.service.AddressServiceImpl" scope="singleton">

    <property name="name">

      <value>北京</value>

    </property>

  </bean>

  <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton">

   <property name="set">

       <set>

          <ref bean="addressServiceImpl"/>

       </set>

   </property>

  </bean>

</beans>

  

 

 Props

package cn.csdn.service;

import java.util.Properties;

public class EmpServiceImpl {

    private AddressServiceImpl addressServiceImpl; 

    private Properties prop;

    public void setAddressServiceImpl(AddressServiceImpl addressServiceImpl) {

       this.addressServiceImpl = addressServiceImpl;

    }

    public AddressServiceImpl getAddressServiceImpl() {

       return addressServiceImpl;

    }

    public Properties getProp() {

       return prop;

    }

    public void setProp(Properties prop) {

       this.prop = prop;

    }

}

 @Test

    public void testProps(){

       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:props.xml");

       EmpServiceImpl empServiceImpl = (EmpServiceImpl) ac.getBean("empServiceImpl");

       Properties props = empServiceImpl.getProp();

    }

<?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">

<bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton">

    <property name="prop">

       <props>

           <prop key="1">baolin</prop>

           <prop key="2">xiaolin</prop>

           <prop key="3">xinlin</prop>

       </props>

    </property>

</bean>

</beans>

  

 

Null

package cn.csdn.service;

public class EmpServiceImpl {

    private AddressServiceImpl addressServiceImpl;

    public void setAddressServiceImpl(AddressServiceImpl addressServiceImpl) {

       this.addressServiceImpl = addressServiceImpl;

    }

    public AddressServiceImpl getAddressServiceImpl() {

       return addressServiceImpl;

    }

}

@Test

    public void testnull(){

       ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:null.xml");

       EmpServiceImpl empServiceImpl = (EmpServiceImpl) ac.getBean("empServiceImpl");

       AddressServiceImpl addr = empServiceImpl.getAddressServiceImpl();

    }

  

 

<?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">

  <bean id="empServiceImpl" class="cn.csdn.service.EmpServiceImpl" scope="singleton">

  <!--在2.5或3.0版本它都支持下面这种写法--> 

   <property name="addressServiceImpl">

      <null/>

   </property>

   <!—在2.5或3.0版本它都不支持下面这种写法

<property name="addressServiceImpl">

     <value>null</value>

   </propert

  -->
</bean>
</beans>

  

 

 

 

分享到:
评论

相关推荐

    Spring中集合类型的装配

    在Spring框架中,集合类型的装配是一项重要的功能,它允许我们把多个同类型的bean注入到一个集合对象中,如List、Set、Map等。这在处理依赖关系时非常有用,特别是当我们需要管理一组相似对象或者需要根据配置动态...

    spring资料集合1

    spring有关的资料 包括pro spring \ pro spring2.5\spring 2.0 reference \精通spring 共三部分

    Spring配置文件集合

    Spring框架是Java开发中广泛应用的轻量级框架,它的核心在于IoC(Inversion of Control,控制反转)和AOP(Aspect Oriented Programming,面向切面编程)。在本压缩包中,我们找到了一系列与Spring相关的配置文件,...

    spring集合属性

    综上所述,Spring集合属性是Spring框架中一个强大的特性,它允许我们灵活地配置和管理bean的集合类型属性,极大地提高了应用的可配置性和可扩展性。通过深入理解并熟练运用这个特性,我们可以更高效地构建和管理...

    spring开发文档集合

    Spring开发文档集合是一个宝贵的资源,包含了关于Spring框架的多种学习材料。这个压缩包中的文档旨在帮助开发者深入理解和熟练使用Spring框架,它是一个广泛应用于Java企业级应用的开源框架。Spring以其依赖注入...

    spring代码3

    在Spring框架中,集合装配是将一组对象,如List、Set、Map等,与Spring容器中的其他bean一起管理的关键概念。这些集合通常包含了相同类型或相关类型的对象,它们可以通过XML配置或者注解方式进行装配。本篇文章我们...

    Spring学习资料集合

    在Spring的学习资料中,可能还会涉及Spring Data,这是一个项目集合,旨在简化数据访问层的开发。通过Spring Data,你可以轻松地实现对各种数据存储(如JPA、MongoDB、Cassandra等)的操作。 Spring Security是安全...

    spring-framework-4.3.0 jar包集合

    Spring Framework 4.3.0 是一个里程碑式的版本,在Java企业级应用开发中扮演着核心角色。这个版本的发布标志着Spring框架在功能、性能和稳定性上又迈出了坚实的一步。以下将详细介绍Spring Framework 4.3.0的核心...

    Spring如何装配各种集合类型的属性

    在Spring框架中,集合类型的属性装配是常见的需求,如List、Set、Map等。这些集合在配置文件或注解中进行装配,可以帮助我们管理复杂的对象依赖关系。本篇将详细介绍Spring如何装配各种集合类型的属性。 1. **XML...

    Spring 学习文档集合

    这个压缩包文件集合提供了丰富的学习资源,帮助初学者深入了解和掌握Spring框架。让我们逐一解析这些资源: 首先,"spring2.0-reference_final_zh_cn.chm" 是Spring 2.0的中文参考手册。这个手册详细阐述了Spring ...

    jdk7-Spring MVC核心包集合.rar

    jdk7SpringMVC 13个核心jar包大集合 包括 1.spring-aop-3.2.0.RELEASE.jar 2.spring-aspects-3.2.0.RELEASE.jar 3.spring-beans-3.2.0.RELEASE.jar 4.spring-context-3.2.0.RELEASE.jar 5.spring-context-support-...

    spring boot + spring + mybatis 集合xml

    spring boot + spring + mybatis 集合xmlspring boot + spring + mybatis 集合xmlspring boot + spring + mybatis 集合xml

    Spring对集合的装配(各种集合类型的属性的注入方式)

    或者使用`@Resource`注解配合`@Qualifier`来指定特定的bean注入到集合中。 3. **使用FactoryBean**: Spring的`FactoryBean`接口可以用来创建复杂的集合,例如: ```java public class MyListFactoryBean ...

    spring3源码集合

    spring-aop-3.1.3.RELEASE-sources.jar spring-asm-3.1.3.RELEASE-sources.jar spring-aspects-3.1.3.RELEASE-sources.jar spring-beans-3.2.0.RELEASE-sources.jar spring-context-3.1.3.RELEASE-sources.jar ...

    SPRING JAR文件集合

    这个"SPRING JAR文件集合"包含了Spring框架所需的多个核心JAR文件,旨在为开发者提供一个便捷的一站式解决方案,以便在项目中快速集成Spring功能。 1. **Spring Core**: Spring的核心模块,提供了依赖注入(DI)和...

    Spring装配集合属性

    在Spring中可以装配4种集合类型属性:List、set、Map和Properties。与这四种集合对应的标签是、、、。CollectionBean是一个包含上述4种集合类型的JavaBean,代码如下:

    Spring练习集合可以运行.zip

    这个名为"Spring练习集合可以运行.zip"的压缩包,提供了丰富的实例,帮助初学者在实际操作中学习Spring的核心功能。 1. **Spring框架介绍**: Spring是一个开源的Java企业级应用框架,它通过依赖注入(Dependency ...

    spring boot demo 集合

    在这个集合中,我们可以期待看到以下一些关键知识点: 1. **自动配置**:Spring Boot的自动配置功能允许开发者只需添加依赖,就可以自动配置相关的Bean。例如,添加了JPA依赖,Spring Boot会自动配置数据源、事务...

    spring官方资料集合

    《Spring官方资料集合》是一个全面涵盖Spring框架及其常用组件集成的资源包,包含了Spring的多个重要方面的详细文档和实战教程。以下将针对这些文件逐一解析,深入探讨Spring框架的核心概念和扩展应用。 1. **...

    spring3.1.4 jar集合

    至于压缩包中的"dist"文件,这通常指的是分布文件或部署文件夹,可能包含了所有需要运行Spring 3.1.4应用程序的jar包和其他相关资源。开发者需要根据自己的项目需求,选择合适的模块和版本进行引用。 总的来说,...

Global site tag (gtag.js) - Google Analytics