依赖注入:Set注入
依赖:bean对象的创建依赖于容器!
注入:bean对象中的所有属性,由容器来注入!
【环境搭建】
复杂类型
package com.linfeng.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
真实测试对象
package com.linfeng.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
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[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
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 String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbies=" + hobbies +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
beans.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="student" class="com.linfeng.pojo.Student">
<property name="name" value="林峰"/>
</bean>
</beans>
测试类
import com.linfeng.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
String name = student.getName();
System.out.println(name);
}
}
完善注入信息
<?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.linfeng.pojo.Address">
<property name="address" value="南京" />
</bean>
<bean id="student" class="com.linfeng.pojo.Student">
<property name="name" value="林峰"/>
<property name="address" ref="address"/>
<property name="books" >
<array>
<value>三国</value>
<value>水浒</value>
<value>西游记</value>
</array>
</property>
<property name="hobbies">
<list>
<value>代码</value>
<value>滑雪</value>
<value>游泳</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="12345324"/>
<entry key="银行卡" value="124325123"/>
</map>
</property>
<property name="games">
<set>
<value>LOL</value>
<value>CF</value>
</set>
</property>
<property name="wife" >
<null/>
</property>
<property name="info">
<props>
<prop key="driver">1241234</prop>
<prop key="url">1241324234</prop>
</props>
</property>
</bean>
</beans>
拓展方式注入
我们可以使用p命名空间和c命名空间进行注入
<?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命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.kuang.pojo.User" p:name="黑心白莲" p:age="20"/>
<!--c命名空间注入,通过构造器注入:constructor-args-->
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22"/>
</beans>
测试
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
User user2 = context.getBean("user2",User.class);
System.out.println(user2);
}
注意点:p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
或者 使用XML自动生成约束的可以在上边直接敲xmlns:p/c=就自动提示了
Bean的作用域(Scopes)
单例模式(Spring默认机制)
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="singleton"/>
原型模式:每次从容器中get的时候,都会产生一个新对象!
<bean id="user2" class="com.kuang.pojo.User" c:name="狂神" c:age="22" scope="prototype"/>
其余的request、session、application、这些只能在web开发中使用到!