Spring基础,DI(重点:依赖注入) 构造方式注入,set方式注入,c命名和p命名空间注入,作用域(scope)
1,构造器注入
<bean id="user" class="com.king.pojo.User"> <!--无参构造创建对象--> <property name="name" value="king"/> <!--有参构造创建对象,index是下标0为第一个位置,value是赋的值--> <constructor-arg index="0" value="龙神"/> </bean>
2,Set方式注入
依赖注入:本质,set注入
依赖:bean对象创建依赖的容器
注入:bean对象中的所有属性,由容器来注入
【环境搭建】
复杂类型
package com.king.pojo; 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 + '\'' + '}'; } }
真实测试对象
public class Student { private String name; private Address address; private String[] book; private List<String> hobby; private Map<String,String> card; private Set<String> games; private String wife; private Properties info;
bean注入信息
<?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中注册一个类,相当于创建一个对象--> <bean id="address" class="com.king.pojo.Address"> <property name="address" value="哈尔滨"/> </bean> <bean id="student" class="com.king.pojo.Student"> <!--第一种普通值注入--> <property name="name" value="king"/> <!--第二种,bean注入,ref:注册(创建)之后就可以引用了--> <property name="address" ref="address"/> <!--数组注入,ref--> <property name="book"> <array> <value>红楼梦</value> <value>西游记</value> <value>水浒传</value> <value>三国演义</value> </array> </property> <!--list--> <property name="hobby"> <list> <value>跑步</value> <value>打篮球</value> <value>看电影</value> </list> </property> <!--map--> <property name="card"> <map> <entry key="身份证" value="111111222222223333"/> <entry key="银行卡" value="1111112222222233334444"/> </map> </property> <!--set--> <property name="games"> <set> <value>lol</value> <value>bob</value> <value>coc</value> </set> </property> <!--null--> <property name="wife"> <null/> </property> <!--Properites--> <property name="info"> <props> <prop key="driver">11111</prop> <prop key="url">0000</prop> <prop key="username">root</prop> <prop key="pwd">123456</prop> </props> </property> </bean> </beans>
单元测试
import com.king.pojo.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.toString()); } }
效果
3,拓展方式注入
p 命名 空间注入(本质,传的是property(传属性),set方式注入)
c 命名 空间注入(本质,构造器注入,construct-args)
注意:p,c命名空间不能直接使用,需要加入相应的xml约束
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xml
<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.king.pojo.User" p:name="king" p:age="18"/> <!--c命名空间注入,本质,构造器器注入construct-args--> <bean id="user2" class="com.king.pojo.User" c:age="18" c:name="kuang"/> </beans>
单元测试
@Test public void test2(){ ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml"); //体现反射机制,通过对象名,获得这个类的属性和对象 User user = context.getBean("user",User.class); System.out.println(user.toString()); }
4.作用域
4.1单例模式(spring默认):singleton
无论若何去调用xml创建的对象,调用的都是同一个对象
4.2原型模式(prototype)
每次调用的都是新的对象
4.3其余的request,session,application这些只能在web开发中使用到