了解更多关于Spring的基础知识
实现步骤:
public class Dao {
private String name;
public void setName(String name) {
this.name = name;
}
public void print(){
System.out.println("my name is "+name);
}
}
public class Service {
//以Dao类型创建属性
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
public void printf(){
dao.print();
}
}
<bean id="service" class="Test_bean.Service">
<property name="dao" ref="dao">property>
bean>
<bean id="dao" class="Test_bean.Dao">
<property name="name" value="offer">property>
bean>
public class Springtest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Service service = context.getBean("service", Service.class);
service.printf();
}
}
my name is offer
实现步骤:
<bean id="service" class="Test_bean.Service">
<property name="dao">
<bean id="dao" class="Test_bean.Dao">
<property name="name" value="offer">property>
bean>
property>
bean>
public class Service {
//以Dao类型创建属性
private Dao dao;
public void setDao(Dao dao) {
this.dao = dao;
}
//与级联赋值配合使用,必须加上get方法
public Dao getDao() {
return dao;
}
public void printf(){
dao.print();
}
}
<bean id="service" class="Test_bean.Service">
<property name="dao" ref="dao">property>
<property name="dao.name" value="offer">property>
bean>
<bean id="dao" class="Test_bean.Dao">bean>
在Spring配置文件中通过XML文件来配置集合属性,如数组,List集合,set集合,Map集合
public class Service {
private String[] array;
private List<String> list;
private Set<String> set;
private Map<String,String> map;
public void setArray(String[] array) {
this.array = array;
}
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
@Override
public String toString() {
return "Service{" +
"array=" + Arrays.toString(array) +
", list=" + list +
", set=" + set +
", map=" + map +
'}';
}
}
<property name="array">
<array>
<value>AAAvalue>
<value>BBBvalue>
array>
property>
<property name="list">
<list>
<value>CCCvalue>
<value>DDDvalue>
list>
property>
<property name="set">
<set>
<value>EEEvalue>
<value>FFFvalue>
set>
property>
<property name="map">
<map>
<entry key="ab" value="GGG">entry>
<entry key="cd" value="HHH">entry>
map>
property>
public class Springtest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Service service = context.getBean("service", Service.class);
System.out.println(service);
}
}
Service{array=[AAA, BBB], list=[CCC, DDD], set=[EEE, FFF], map={ab=GGG, cd=HHH}}
目的:如果只能将集合对象配置在某个bean内部,则这个集合的配置将不能重用。我们需要将集合bean的配置拿到外面,供其他bean引用。
1、在Spring配置文件引入名称空间util
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
2、使用util标签完成list集合提取
<util:list id="list">
<value>AAAvalue>
<value>BBBvalue>
util:list>
<bean id="service" class="Test_bean.Service">
<property name="list" ref="list">property>
bean>
❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
若对Spring基础知识感兴趣的可以关注一下博主,我会持续更新Spring基础知识(一边学习一边记录),一起进步,有错误的地方也可以在评论区指出来喔,谢谢大家啦!!!