Spring中注入List,Set,Map,Properties的xml文件配置方法
2023-09-11 14:17:56 时间
下面的例子展示了如何注入
- List – <list/>
- Set – <set/>
- Map – <map/>
- Properties – <props/>
Spring beans
import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Customer { private List<Object> lists; private Set<Object> sets; private Map<Object, Object> maps; private Properties pros; //... }
配置文件:
<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="CustomerBean" class="com.mkyong.common.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongList" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongSet" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </set> </property> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.mkyong.common.Person"> <property name="name" value="mkyongMap" /> <property name="address" value="address" /> <property name="age" value="28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@nospam.com</prop> <prop key="support">support@nospam.com</prop> </props> </property> </bean> <bean id="PersonBean" class="com.mkyong.common.Person"> <property name="name" value="mkyong1" /> <property name="address" value="address 1" /> <property name="age" value="28" /> </bean> </beans>
运行:
public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
相关文章
- Spring MVC程序中得到静态资源文件css,js,图片
- Spring MVC 使用拦截器 HiddenHttpMethodFilter配置Rest风格的URL
- spring mvc:常用标签库(文本框,密码框,文本域,复选框,单选按钮,下拉框隐藏于,上传文件等)
- Spring Data操作Redis时,发现key值出现 xacxedx00x05tx00tb
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- spring boot:用itextpdf处理pdf表格文件(spring boot 2.3.2)
- spring 学习2-Spring Configuration in detail
- spring boot注解 --@spring-boot-devtools 自动加载修改的文件和类
- Spring中配置和读取多个Properties文件--转
- 深入理解Spring Redis的使用 (八)、Spring Redis实现 注解 自动缓存
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- [Spring学习笔记 3 ] spring 注解详解,完全注解,常用注解
- [Docker] MySQL with spring docket image
- spring boot:接收数组参数及多文件混合json参数(spring boot 2.3.4)
- Spring MVC 中的异常处理 (handling exceptions)
- Atitit spring单元测试 注解 获取服务名 Spring文件单独放在一个文件夹,去掉dubbo配置,方便测试 里面包含的mybatis 找不到,只好设置成相对于class绝对路径可以了
- 解决 Spring HttpMessageConverter 解析多余属性出错问题:DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
- spring代码异常捕获到logback logging.config=logback-spring.xml文件中不能输出异常e.printStackTrace
- Spring到底应该学哪些内容?
- 002-Spring Boot将WAR文件部署到Tomcat
- 007-Spring Boot-@Enable*注解的工作原理-EnableConfigurationProperties、ImportSelector、ImportBeanDefinitionRegistrar
- Spring官方推荐的@Transactional还能导致生产事故?
- Spring+SpringMVC+Mybatis(开发必备技能)03、图片上传
- 【Spring源码学习】spring IOC容器管理
- 【java】Spring Boot --spring boot项目整合xxl-job