前言:上节我带大家快速上手了Spring,这一节我们讲解Spring中的依赖注入(DI),废话不多说,直接上正文!
在上一节的IOC创建方式中进行了讲解:https://blog.csdn.net/qq_58233406/article/details/127244071
Address.java
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Student.java
package com.bby.pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setGames(Set<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
常量注入
<bean id="student" class="com.bby.pojo.Student">
<property name="name" value="啵啵鱼"/>
bean>
测试
public class StudentTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
<bean id="student" class="com.bby.pojo.Student">
<property name="name" value="啵啵鱼"/>
bean>
注意点:这里的值是一个引用,ref
<bean id="addr" class="com.bby.pojo.Address">
<property name="address" value="重庆"/>
bean>
<bean id="student" class="com.bby.pojo.Student">
<property name="name" value="小明"/>
<property name="address" ref="addr"/>
bean>
<bean id="student" class="com.bby.pojo.Student">
<property name="name" value="小明"/>
<property name="address" ref="addr"/>
<property name="books">
<array>
<value>西游记value>
<value>红楼梦value>
<value>水浒传value>
array>
property>
bean>
<property name="hobbys">
<list>
<value>听歌value>
<value>看电影value>
<value>爬山value>
list>
property>
<property name="card">
<map>
<entry key="中国邮政" value="456456456465456"/>
<entry key="建设" value="1456682255511"/>
map>
property>
<property name="games">
<set>
<value>LOLvalue>
<value>BOBvalue>
<value>COCvalue>
set>
property>
<property name="wife"><null/>property>
<property name="info">
<props>
<prop key="学号">20190604prop>
<prop key="性别">男prop>
<prop key="姓名">小明prop>
props>
property>
需要在头文件中加入约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<bean id="user" class="com.bby.pojo.User" p:name="bobooy" p:age="18"/>
需要在头文件中加入约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<bean id="user" class="com.bby.pojo.User" c:name="bobooy" c:age="18"/>
注意:一定要写上带参构造
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .
<bean id="user" class="com.bby.pojo.User" c:name="啵啵鱼" c:age="20" scope="singleton"/>
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的SpringApplicationContext环境。
尾言:创作不易,如果本文的内容对您有帮助,还望客官可以三连支持一下博主,👍(点赞)+✏️(评论)+⭐️(收藏)是我创作的巨大动力!