依赖注入
1.构造器注入:同上
2.set()注入,依赖注入的本质
1.依赖:bean对象的创建依赖于容器
2.注入:bean对象中的所有属性,由容器来注入!
各种类型的注入
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;
@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 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;
}
}
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 + '\'' +
'}';
}
}
配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.liye.pojo.Address">
<property name="address" value="立业"/>
</bean>
<bean id="student" class="com.liye.pojo.Student">
<property name="name" value="请将"/>
<property name="address" ref="address"/>
<!--数组注入-->
<property name="book">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>唱歌</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="学生卡" value="123r3"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>上看到结局</value>
</set>
</property>
<!--null-->
<property name="wife">
<null></null>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">1544165156</prop>
<prop key="userName">root</prop>
<prop key="password">null</prop>
</props>
</property>
</bean>
</beans>
测试
public class MyText {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans4.xml");
Student student=context.getBean("student",Student.class);
System.out.println(student.toString());
}
}
结果:
Student{name=‘请将’, address=Address{address=‘立业’}, book=[红楼梦, 西游记, 三国演义, 水浒传], hobbys=[唱歌], card={学生卡=123r3}, games=[上看到结局], info={学号=1544165156, password=null, userName=root}, wife=‘null’}
P注入和C注入
重点:使用P和C要导入约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
public class User {
String name;
int age;
User() {
}
User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p:对应set注入-->
<bean id="user" class="com.liye.pojo.User" p:name="asd" p:age="12"/>
<!--c:对应构造器注入,类中必须要有有参构造器-->
<bean id="user2" class="com.liye.pojo.User" c:_0="12" c:_1="12"/>
</beans>