首先在搭建好开发Spring项目的环境后(其实具体就是添加几个jar包,网上搜吧,多的很)
对依赖注入或者IOC有什么疑问,可以参考我之前的文章
https://blog.csdn.net/qq_38261445/article/details/90142455
注入多种类型实例
首先创建我们的类,里面包含多种类型
AllCollectionType.java
package com.sty;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class AllCollectionType {
private List<String> list;
private String[] array;
private Set<String> set;
private Map<String,String> map;
private Properties props;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
@Override
public String toString() {
return "AllCollectionType [list=" + list + ", array=" + Arrays.toString(array) + ", set=" + set + ", map=" + map
+ ", props=" + props + "]";
}
}
新建一个Spring bean.xml文件(applicationContext.xml)(右键->other->输入bean(在确保你安装了spring-tools-suite)->选择Spring下的Spring bean Configuration File)
<?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.xsd">
<!-- 该文件中的所有对象都在spring容器中 -->
<!-- id唯一标识符 class指定类型 -->
<bean id="collectionDemo" class="com.sty.AllCollectionType">
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
</list>
</property>
<property name="array">
<array>
<value>array1</value>
<value>array2</value>
<value>array3</value>
</array>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="map">
<map>
<entry>
<!-- key -->
<key><value>mapkey</value></key>
<value>mapval</value>
</entry>
<entry>
<!-- key -->
<key><value>mapkey1</value></key>
<value>mapval1</value>
</entry>
</map>
</property>
<property name="props">
<props>
<prop key="pk1">pv1</prop>
<prop key="pk2">pv2</prop>
<prop key="pk3">pv3</prop>
</props>
</property>
</bean>
</beans>
最后在Test里面验证
package com.sty.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sty.AllCollectionType;
import com.sty.Student;
public class Test {
//给各种类型注入
public static void collectionDemo() {
//不需要new对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AllCollectionType allCollectionType = (AllCollectionType)context.getBean("collectionDemo");
System.out.println(allCollectionType.toString());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/
//多种类型注入
collectionDemo();
}
}
附上实例结果
以上是本人通过实际课程案例,得出的一些心得,如果由错误或者不够完善的地方,还希望各位大神多多指教,谢谢!!