1.构造器注入
如果只有一个有参数的构造方法并且参数类型与注入的bean的类型匹配,那就会注入到该构造方法中。要求类里要有有参构造。每个构造参数就是一个依赖项 详见下文中:使用有参数构造创建对象
Spring IOC创建对象的方式_张志明(努力奋斗版)的博客-CSDN博客1.默认使用无参构造器创建对象 tips:要求实体类中不显示写出构造函数 或者 写出缺省的构造函数.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:schemaLocati...https://blog.csdn.net/TIANJIAWEN/article/details/124794391?spm=1001.2014.3001.5501
2.set方式注入
在xml配置文件中写入,然后在set方法中注入,要求类里依赖项要有set方法 要求实体类中不显示写出构造函数 或者 写出缺省的构造函数
set方式依赖注入:
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有实行 由容器来注入
xml配置 常见的8种类型
<?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="address" class="com.kero.pojo.Address"/>
<bean id="student" class="com.kero.pojo.Student">
<!-- 第一种 普通注入 value-->
<property name="name" value="kk"/>
<!-- 第二种 Bean注入 ref-->
<property name="address" ref="address"/>
<!-- 第三种数组 -->
<property name="book" >
<array>
<!-- 上面两种也能分开写 只不过那样写比较简便-->
<value>哈利波特1</value>
<value>哈利波特2</value>
<value>哈利波特3</value>
<value>哈利波特4</value>
</array>
</property>
<!-- 第4种list-->
<property name="hobbys" >
<list>
<value>打篮球1</value>
<value>打篮球2</value>
<value>打篮球3</value>
<value>打篮球4</value>
</list>
</property>
<!-- 第5种map-->
<property name="card" >
<map>
<entry key="身份证" value="12332423534"/>
<entry key="银行卡" value="SDGFJ34"/>
</map>
</property>
<!-- 第6种set-->
<property name="games" >
<set>
<value>ow1</value>
<value>ow2</value>
</set>
</property>
<!-- 第7种null-->
<property name="wife" >
<null/>
</property>
<!-- 第8种properties-->
<property name="info" >
<props>
<prop key="学号">65684</prop>
<prop key="性别">男</prop>
<prop key="driver">65684</prop>
<prop key="url">45456456</prop>
<prop key="password">65634584</prop>
<prop key="username">东方红东方红</prop>
</props>
</property>
</bean>
</beans>
Student/Address.java
import java.util.*;
public class Student {
private String name;
private Address address;
private String []book;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private Properties info;
private String wife;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBook() {
return book;
}
public void setBook(String[] book) {
this.book = book;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", book=" + Arrays.toString(book) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
}
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
test.java
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("bean.xml");
Student student = (Student) classPathXmlApplicationContext.getBean("student");
System.out.println(student.toString());
}
}
输出结果:
Student{name='kk', address=Address{address='scnu'}, book=[哈利波特1, 哈利波特2, 哈利波特3, 哈利波特4], hobbys=[打篮球1, 打篮球2, 打篮球3, 打篮球4], card={身份证=12332423534, 银行卡=SDGFJ34}, games=[ow1, ow2], info={password=65634584, driver=65684, 学号=65684, url=45456456, username=东方红东方红, 性别=男}, wife='null'}
3.拓展方式注入
p命名空间注入 可以直接注入属性的值 需要隐式或者显示声明无参构造器
需要导入 xmlns:p="http://www.springframework.org/schema/p"
<bean id="user" class="com.kero.pojo.User" p:name="zhangke" p:age="21"/>
c命名空间注入 可以通过构造器注入属性的值 需要显示声明有参构造器
需要导入 xmlns:c="http://www.springframework.org/schema/c"
<bean id="user" class="com.kero.pojo.User" c:name="zzzz" c:age="2"/>