Set方式注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器注入
复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> game;
private Properties info;
private String wife;
}
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.zhao.pojo.Address">
<property name="address" value="武汉"></property>
</bean>
<bean id="student" class="com.zhao.pojo.Student">
<!--第一种:普通值注入-->
<property name="name" value="赵宇航"></property>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"></property>
<!--数组注入-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
</array>
</property>
<!--List-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>敲代码</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="身份证" value="123456"></entry>
<entry key="银行卡" value="1234567"></entry>
</map>
</property>
<!--Set-->
<property name="game">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<property name="wife">
<null></null>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">1810300305</prop>
<prop key="性别">男性</prop>
</props>
</property>
</bean>
</beans>
Test
import com.zhao.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");
System.out.println(student.toString());
}
}
扩展方式注入
我们可以使用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.zhao.pojo.User" p:name="zhoayuhang" p:age="20"></bean>
<!--c命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.zhao.pojo.User" c:age="18" c:name="zhaoyuhang"></bean>
</beans>
注意点:p(set注入)命名和c(构造器注入)命名空间不能直接使用,需要导入xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
Bean的作用域
1.单例模式(Spring)默认机制,每次从容器中get,都是同一个对象
<bean id="user" class="com.zhao.pojo.User" p:name="zhoayuhang" p:age="20" scope="singleton"></bean>
2.原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="user" class="com.zhao.pojo.User" p:name="zhoayuhang" p:age="20" scope="prototype"></bean>
3.其余的request,session,application,这些只能在web开发中使用到。