依赖注入
1、构造器注入
-
使用无参构造方法创建对象,默认!
-
假设我们要使用有参构造创建对象
-
下标赋值
<!--第一种通过下标赋值--> <bean id="user" class="com.vekzjj.pojo.User"> <constructor-arg index="0" value="vekzjj"/> </bean>
-
类型
<!--第二种通过类型创建--> <bean id="user" class="com.vekzjj.pojo.User"> <constructor-arg type="java.lang.String" value="vekzjj"/> </bean>
-
参数名
<!--第三种,直接通过参数名来赋值--> <bean id="user" class="com.vekzjj.pojo.User"> <constructor-arg name="name" value="vekzjj"/> </bean>
-
2、Set方式注入【重点】
- 依赖注入:本质是Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,又容器注入
【环境搭建】
-
复杂类型
package com.vekzjj.pojo; 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> games; private String wife; private Properties 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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.vekzjj.pojo.Address"> <property name="address" value="西安"/> </bean> <!--普通值注入,value--> <bean id="student" class="com.vekzjj.pojo.Student"> <property name="name" value="vekzjj"/> <!--bean注入,ref--> <property name="address" ref="address"/> <!--数组注入--> <property name="books"> <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> <value>三国演义</value> </array> </property> <!--List注入--> <property name="hobbys"> <list> <value>听歌</value> <value>打游戏</value> <value>看电影</value> </list> </property> <!--Map注入--> <property name="card"> <map> <entry key="QQ" value="2088374723"/> <entry key="电话" value="13999007878"/> </map> </property> <!--Set--> <property name="games"> <set> <value>LOL</value> <value>csgo</value> <value>永劫无间</value> </set> </property> <!--null--> <property name="wife"> <null/> </property> <!--Properties--> <property name="info"> <props> <prop key="学号">20202501033</prop> <prop key="班级">20-23</prop> <prop key="sex">男</prop> </props> </property> </bean> </beans>
-
测试类
public class DemoTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.toString()); } }
-
输出
Student{ name='vekzjj', address=Address{address='西安'}, books=[红楼梦, 西游记, 水浒传, 三国演义], hobbys=[听歌, 打游戏, 看电影], card={ QQ=2088374723, 电话=13999007878 }, games=[LOL, csgo, 永劫无间], wife='null', info={ 学号=20202501033, 班级=20-23, sex=男 } }
3、第三方式注入
1、 p-namespace
-
在bean的namespace中加入:
xmlns:p="http://www.springframework.org/schema/p"
-
<!--p命名空间注入可以直接注入属性的值--> <bean id="user" class="com.vekzjj.pojo.User" p:name="vekzjj" p:age="20"/>
2、c-namespace
-
在bean的namespace中加入:
xmlns:c="http://www.springframework.org/schema/c"
-
<!--c命名空间注入,通过构造器注入--> <bean id="user2" class="com.vekzjj.pojo.User" c:name="zjj" c:age="20"/>
注意点:p命名和c命名空间不能直接使用,需要导入xml约束
4、bean的作用域(scope)
Scope | Description |
---|---|
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
prototype | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext . |
session | Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext . |
application | Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext . |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket . Only valid in the context of a web-aware Spring ApplicationContext . |
-
单例模式(Spring默认机制)
<bean id="user2" class="com.vekzjj.pojo.User" c:name="zjj" c:age="20" scope="singleton"/>
-
原型模式:每次从容器中get的时候,都会产生一个新对象
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
-
其他的request、session、application(这些只能在web开发中使用到!)