给MessageBean注入参数值,为Bean注入集合参数值。

来源:互联网 发布:python url下载文件 编辑:程序博客网 时间:2024/06/01 08:30

一、类MessageBean,添加四个属性,并生成这几个属性对应的getter和setter方法;

最后在execute方法获取这几个属性的信息。

package org.daliu.bean;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;/** * 类MessageBean,添加四个属性,并生成这几个属性对应的getter和setter方法; * 最后在execute方法获取这几个属性的信息 *  * @author Administrator *  */public class MessageBean {//1.属性值,生成一下getter和setter方法private String moduleName;private int pageSize;private String username;private String password = "";//2. list集合,生成一下getter和setter方法private List<String> someList;private Set<String> someSet;private Map<String,Object> someMap;private Properties someProps;/* *  */public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getModuleName() {return moduleName;}public void setModuleName(String moduleName) {this.moduleName = moduleName;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}/** * 输出属性和的信息 * @return */public String execute() {System.out.println("属性的消息为:");System.out.println("moduleName=" + moduleName);System.out.println("pageSize=" + pageSize);System.out.println("username=" + username);System.out.println("password=" + password);System.out.println("---List信息如下---");for(String s : someList){System.out.println(s);}System.out.println("---Set信息如下---");for(String s : someSet){System.out.println(s);}System.out.println("---Map信息如下---");Set<String> keys = someMap.keySet();for(String key : keys){System.out.println(key+"="+someMap.get(key));}System.out.println("---Properties信息如下---");Set<Object> keys1 = someProps.keySet();for(Object key : keys1){System.out.println(key+"="+someProps.getProperty(key.toString()));}return "success";}/* * list集合 的 get 、set方法 */public List<String> getSomeList() {return someList;}public void setSomeList(List<String> someList) {this.someList = someList;}public Set<String> getSomeSet() {return someSet;}public void setSomeSet(Set<String> someSet) {this.someSet = someSet;}public Map<String, Object> getSomeMap() {return someMap;}public void setSomeMap(Map<String, Object> someMap) {this.someMap = someMap;}public Properties getSomeProps() {return someProps;}public void setSomeProps(Properties someProps) {this.someProps = someProps;}}

二、配置Spring的环境

<?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:context="http://www.springframework.org/schema/context"xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd"><bean id="messageBean" class="org.daliu.bean.MessageBean"><!-- 为MessageBean的四个属性注入基本参数值 private String moduleName; private int pageSize; private String username; private String password = ""; --><property name="moduleName" value="test"></property><property name="pageSize" value="5"></property><property name="username" value="testname"></property><property name="password" value="123456"></property><!-- 为MessageBean添加集合属性,用于注入集合测试 private List<String> someList; private Set<String> someSet; private Map<String,Object> someMap; private Properties someProps; --><property name="someList"><list><value>广州</value><value>深圳</value><value>梧州</value><value>南宁</value></list></property><property name="someSet"><set><value>jack</value><value>jack2</value><value>jack3</value></set></property><property name="someMap"><map><entry key="1001" value="java程序设计"></entry><entry key="1002" value="c程序设计"></entry><entry key="1003" value="c#程序设计"></entry></map></property><property name="someProps"><props><prop key="usename">root</prop><prop key="password">123456</prop></props></property></bean></beans>


三、测试案例


package org.daliu.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.tarena.bean.MessageBean;public class Test1 {@Test/** * 测试注入属性值和可以为Bean注入集合参数值。 * @throws Exception */public void  testAttribute() throws Exception{String conf = "applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(conf);System.out.println(ac);MessageBean bean = ac.getBean("messageBean",MessageBean.class);bean.execute();}}

</pre><p></p><p></p><pre>
四、测试结果。

属性的消息为:moduleName=testpageSize=5username=testnamepassword=123456---List信息如下---广州深圳梧州南宁---Set信息如下---jackjack2jack3---Map信息如下---1001=java程序设计1002=c程序设计1003=c#程序设计---Properties信息如下---usename=rootpassword=123456



0 0
原创粉丝点击