Spring——配置、依赖注入
一、Spring配置
1.别名
<!--别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="userNew"/>
2.Bean的配置
id : bean 的唯一标识符,也就是相当于我们学的对象名
class : bean 对象所对应的全限定名 : 包名 + 类型
<bean id="addr" class="com.cm.pojo.Address">
<property name="address" value="重庆"/>
</bean>
name :也是别名,而且name 可以同时取多个别名(用空格间隔)
<bean name="user user2 user3" class="com.cm.pojo.User">
<property name="name" value="张三"/>
</bean>
3.import
这个import,一般用于团队开发使用,它可以将多个配置文件导入合并为一个。
假设,现在项目中有多个人开发,这三个人使用不同的类开发,不同的类需要注册在不同的xml配置文件中,我们可以利用import将所有人的xml合并为一个总的!
applicationContext.xml
将几个xml全部合并在applicationContext.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">
<import resource="service_user2.xml"/>
<import resource="service_student.xml"/>
</beans>
使用的时候,直接使用总的配置就可以了。
二、依赖注入
1.构造器注入
前面已经说过了
2.Set方式注入 【重点】
- 依赖注入:Set注入!
- 依赖:bean对象的创建依赖于容器!
- 注入: bean对象中的所有属性,由容器来注入!
【环境搭建】
-
复杂类型
public class Address { private String address; public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
-
真实测试对象
public class Student { private String name; private Address address; private String[] books; private List<String> list; private Map<String,String> map; private Set<String> set; private String wife ;// null private Properties info; public void setName(String name) { this.name = name; System.out.println("name"); } public void setAddress(Address address) { this.address = address; } public void setBooks(String[] books) { this.books = books; } public void setList(List<String> list) { this.list = list; } public void setMap(Map<String, String> map) { this.map = map; } public void setSet(Set<String> set) { this.set = set; } 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) + ", list=" + list + ", map=" + map + ", set=" + set + ", wife='" + wife + '\'' + ", info=" + info + '}'; } }
-
applicationContext.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"> <import resource="service_student.xml"/> </beans>
-
完善注入信息
service_student.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">
<!-- services -->
<bean id="addr" class="com.cm.pojo.Address">
<property name="address" value="重庆"/>
</bean>
<bean id="student" class="com.cm.pojo.Student">
<property name="name" value="张三"/>
<property name="address" ref="addr"/>
<property name="books">
<array>
<value>红楼梦</value>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
</array>
</property>
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<value>list3</value>
<value>list4</value>
</list>
</property>
<property name="map">
<map>
<!--这里是键值对-->
<entry key="1" value="map1"/>
<entry key="2" value="map2"/>
<entry key="3" value="map3"/>
</map>
</property>
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<value>set3</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="id">9</prop>
<prop key="name">小红</prop>
<prop key="sex">女</prop>
</props>
</property>
</bean>
<!-- more bean definitions for services go here -->
</beans>
-
测试类
@Test public void testStudent() { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student); }
结果(可以看到使用了set注入)