注入依赖:
注入:bean的所有属性都由容器来注入
依赖:bean的所有配置都离不开容器
附:学习spring可以直接看文档
基于setter配置
1,使用list将集合输出(需要的jar包上一章有),主要区分value和ref的使用区别
value:即字面量属性
ref:非字面量属性
接下来看代码,新建两个javabean,建在pojo包下
代码分别为
Student.java
package pojo;
public class Student {
private String name;
private Integer id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "name=" + name + "," + "id=" + id;
}
}
UserQaq
package pojo;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class UserQaq {
private String name;
private Integer age;
private List<Student> list1;
private List<Integer> list2;
private Set<Student> set1;
private Set<String> set2;
private Map<Integer,String> map1;
public Map<String, Student> getMap2() {
return map2;
}
public void setMap2(Map<String, Student> map2) {
this.map2 = map2;
}
private Map<String,Student> map2;
public Map<Integer, String> getMap1() {
return map1;
}
public void setMap1(Map<Integer, String> map1) {
this.map1 = map1;
}
public Set<Student> getSet1() {
return set1;
}
public void setSet1(Set<Student> set1) {
this.set1 = set1;
}
public Set<String> getSet2() {
return set2;
}
public void setSet2(Set<String> set2) {
this.set2 = set2;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<Student> getList1() {
return list1;
}
public void setList1(List<Student> list1) {
this.list1 = list1;
}
public List<Integer> getList2() {
return list2;
}
public void setList2(List<Integer> list2) {
this.list2 = list2;
}
@Override
public String toString() {
return "name = " + name + "\n" + "age=" + age + "\n" + "list1 = " + list1 + "\n" + "list2" + list2
+ "\n" + "set1=" + set1 + "\n" + "set2" + set2 + "\n" + "map1=" + map1 + "\n" + "map2" + map2;
}
}
在容器中配置xml文件,新建一个applicationContext.xml文件
<?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">
<bean id="userQaq" class="pojo.UserQaq">
<property name="name" value="蔡康程"></property>
<property name="age" value="21"></property>
<property name="list1">
<!--非字面量属性ref,如此属性,不能写成字符串的属性-->
<list>
<ref bean="student1"></ref>
<ref bean="student2"></ref>
<ref bean="student3"></ref>
<ref bean="student4"></ref>
<ref bean="student5"></ref>
</list>
</property>
<property name="list2">
<list>
<!--字面量属性使用value-->
<value>1234</value>
<value>2345</value>
<value>3456</value>
<value>4567</value>
</list>
</property>
<property name="set1">
<set>
<ref bean="student1"></ref>
<ref bean="student2"></ref>
<ref bean="student3"></ref>
<ref bean="student4"></ref>
<ref bean="student5"></ref>
</set>
</property>
<property name="set2">
<set>
<value>qaq</value>
<value>qqq</value>
<value>qwq</value>
<value>qeq</value>
<value>qrq</value>
</set>
</property>
<property name="map1">
<map>
<entry key="123" value="红楼梦"></entry>
<entry key="456" value="三国演义"></entry>
</map>
</property>
<property name="map2">
<map>
<entry key="123" value-ref="student1"></entry>
<entry key="456" value-ref="student2"></entry>
<entry key="123" value-ref="student3"></entry>
<entry key="456" value-ref="student4"></entry>
<entry key="123" value-ref="student5"></entry>
</map>
</property>
</bean>
<bean id="student1" class="pojo.Student">
<property name="id" value="123"></property>
<property name="name" value="李海乐"></property>
</bean>
<bean id="student2" class="pojo.Student">
<property name="id" value="133"></property>
<property name="name" value="钟华金"></property>
</bean>
<bean id="student3" class="pojo.Student">
<property name="id" value="143"></property>
<property name="name" value="易景亮"></property>
</bean>
<bean id="student4" class="pojo.Student">
<property name="id" value="153"></property>
<property name="name" value="渣男"></property>
</bean>
<bean id="student5" class="pojo.Student">
<property name="id" value="163"></property>
<property name="name" value="万翀"></property>
</bean>
</beans>
接下来写测试类
package tool;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pojo.UserQaq;
public class TestUserQaq {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserQaq userQaq = (UserQaq) context.getBean("userQaq");
System.out.println(userQaq);
}
}
最后可以发现,在idea中出现此标志
测试成功!