SSM——Spring 学习总结(二) Spring IOC容器

<

目录

  • Spring 学习总结(二)Spring IOC容器
    • 1、通过配置文件向 Map、List、Set、Properties、String数组中注入数据

Spring 学习总结(二)Spring IOC容器

1、通过配置文件向 Map、List、Set、Properties、String数组中注入数据

  • 创建Map、List、Set、Properties、String数组元素,并创建set,get,toString方法;
ackage qiao.student;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
	private Map<String,String> map;
	private Set<String> set;
	private String[] str;
	private List<String> list;
	private Properties props;
	public Map<String, String> getMap() {
		return map;
	}
	public void setMap(Map<String, String> map) {
		this.map = map;
	}
	public Set<String> getSet() {
		return set;
	}
	public void setSet(Set<String> set) {
		this.set = set;
	}
	public String[] getStr() {
		return str;
	}
	public void setStr(String[] str) {
		this.str = str;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Properties getProps() {
		return props;
	}
	public void setProps(Properties props) {
		this.props = props;
	}
	public String toString() {
		String s = "";
		for(String sc:str) {
			s += sc+",";
		}
		return "list:"+this.list+"\nMap:"+this.map+"\nSet:"+this.set+"\nProperties"+this.props+"\nString:"+s;
	}
}
  • 编写配置文件

<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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
	
	<property name="list">    
	    <list>
	        <value>aaavalue>
	        <value>bbbvalue>
	        <value>cccvalue>
	   list>
	property>
	
	<property name="set">          
	    <set>
	        <value>dddvalue>
	        <value>eeevalue>
	        <value>fffvalue>
	   set>
	property>                
	
	        
	    <map>
	        <entry>
	            <key>                    
	                <value>gggvalue>
	            key>
	            <value>hhhvalue>
	       entry>
	        <entry>
	            <key>
	                <value>111value>
	            key>
	            <value>222value>
	       entry>
	   map>
	property>
	
	<property name="str">            
	<array>
		<value>iiivalue>
	    <value>jjjvalue>
	    <value>kkkvalue>
	array>
	property>
	
	<property name="props">              
	     <props>
	        <prop key="ccc">cccprop>
	        <prop key="ddd">dddprop>
	        <prop key="eee">eeeprop>
	     props>
	property>
	bean>
	
	beans>
  • 编写测试类
package qiao.text;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import qiao.student.Student;
public class Test {
    public static void main(String[] args) {
	@SuppressWarnings("resource") 
	ApplicationContext context= new	ClassPathXmlApplicationContext("applicationContext.xml"); 
	Student student = (Student)context.getBean("student1"); 
	System.out.println(student);	
    }
}
  • 运行结果
    SSM——Spring 学习总结(二) Spring IOC容器_第1张图片
    如有问题还请指出,谢谢。江湖最高道义 “抱拳了”

你可能感兴趣的:(JAVAWeb初学者,Java初学者,SSM,Spring,IOC,容器,学习总结)