依赖注入(DI)
settet注入 (重点)
拓展注入实现
Bean的作用域
Singleton
Prototype
Request
Session
-
依赖注入(DI)
- 依赖注入(Dependency Injection,DI)。
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
- 主要通过类的set方法来实现
- 也可以使用构造器来注入,如使用<constructor-arg/>
2.settet注入 (重点)
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is .
测试pojo类 :
Address.java
public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Student.java
1 package com.kuang.pojo; 2 import java.util.List; 3 import java.util.Map; 4 import java.util.Properties; 5 import java.util.Set; 6 7 public class Student { 8 9 private String name; 10 private Address address; 11 private String[] books; 12 private List<String> hobbys; 13 private Map<String,String> card; 14 private Set<String> games; 15 private String wife; 16 private Properties info; 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public void setAddress(Address address) { 23 this.address = address; 24 } 25 26 public void setBooks(String[] books) { 27 this.books = books; 28 } 29 30 public void setHobbys(List<String> hobbys) { 31 this.hobbys = hobbys; 32 } 33 34 public void setCard(Map<String, String> card) { 35 this.card = card; 36 } 37 38 public void setGames(Set<String> games) { 39 this.games = games; 40 } 41 42 public void setWife(String wife) { 43 this.wife = wife; 44 } 45 46 public void setInfo(Properties info) { 47 this.info = info; 48 } 49 50 public void show(){ 51 System.out.println("name="+ name 52 + ",address="+ address.getAddress() 53 + ",books=" 54 ); 55 for (String book:books){ 56 System.out.print("<<"+book+">>\t"); 57 } 58 System.out.println("\n爱好:"+hobbys); 59 60 System.out.println("card:"+card); 61 62 System.out.println("games:"+games); 63 64 System.out.println("wife:"+wife); 65 66 System.out.println("info:"+info); 67 68 } 69 }
常量注入
通过beans.xml来实现注入
<bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="小明"/> </bean>
测试:
@Test public void test01(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) context.getBean("student"); System.out.println(student.getName()); }
3、Bean注入
注意:这里的值是一个引用,ref
<bean id="addr" class="com.kuang.pojo.Address"> <property name="address" value="重庆"/> </bean> <bean id="student" class="com.kuang.pojo.Student"> <property name="name" value="小明"/> <property name="address" ref="addr"/> </bean>
其他注入方式:
1 <bean id="student" class="com.ybzn.pojo.Student"> 2 <!-- 第一种,普通注入,通过value注入 --> 3 <property name="name" value="李"/> 4 5 <!-- 第二种,Bean注入,ref --> 6 <property name="address" ref="address"/> 7 8 <!-- 第三种,数组注入,array --> 9 <property name="books"> 10 <array value-type="java.lang.String"> 11 <value>红楼梦</value> 12 <value>西游记</value> 13 <value>三国演义</value> 14 </array> 15 </property> 16 17 <!-- 第四种,List注入,list --> 18 <property name="hobbys"> 19 <list> 20 <value>听个歌</value> 21 <value>敲代码</value> 22 <value>看电影</value> 23 </list> 24 </property> 25 26 <!-- 第五种,Map注入,map --> 27 <property name="card"> 28 <map> 29 <entry key="李" value="李"></entry> 30 <entry key="傅" value="归"></entry> 31 <entry key="鸭" value="龟"></entry> 32 </map> 33 </property> 34 35 <!-- 第六种,set注入,set --> 36 <property name="games"> 37 <set> 38 <value>LoL</value> 39 <value>COC</value> 40 <value>BoB</value> 41 </set> 42 </property> 43 44 <!-- 第七种,null空值注入,null --> 45 <property name="wife"> 46 <null/> 47 </property> 48 49 <!-- 第八种,property注入,props --> 50 <property name="info"> 51 <props> 52 <prop key="学号">20170154</prop> 53 <prop key="宿舍">50栋22</prop> 54 </props> 55 </property> 56 </bean>
4.拓展注入实现
User.java : 【注意:这里没有有参构造器!】
public class User { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
1、P命名空间注入** : 需要在头文件中假如约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p" 不导入约束无法使用 <!--P(属性: properties)命名空间 , 属性依然要设置set方法--> <bean id="user" class="com.kuang.pojo.User" p:name="狂li" p:age="18"/>
2、c 命名空间注入 : 需要在头文件中假如约束文件
导入约束 : xmlns:c="http://www.springframework.org/schema/c" <!--C(构造: Constructor)命名空间 , 属性依然要设置set方法--> <bean id="user" class="com.kuang.pojo.User" c:name="狂神" c:age="18"/>
发现问题:爆红了,刚才我们没有写有参构造!
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!
测试代码,和之前一样通过getBean(id);
@Test public void test02(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) context.getBean("user"); System.out.println(user); }
Bean的作用域
在Spring中,那些组成应用程序的主体及由Spring IoC容器所管理的对象,被称之为bean。简单地讲,bean就是由IoC容器初始化、装配及管理的对象 .
几种作用域中,request、session作用域仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架),只能用在基于web的Spring ApplicationContext环境。
Singleton
当一个bean的作用域为Singleton,那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。Singleton是单例类型,就是在创建起容器时就同时自动创建了一个bean的对象,不管你是否使用,他都存在了,每次获取到的对象都是同一个对象。注意,Singleton作用域是Spring中的缺省作用域。要在XML中将bean定义成singleton,可以这样配置:
<bean id="ServiceImpl" class="cn.csdn.service.ServiceImpl" scope="singleton">
测试:
@Test public void test03(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); User user = (User) context.getBean("user"); User user2 = (User) context.getBean("user"); System.out.println(user==user2); }
Prototype作用域
当一个bean的作用域为Prototype,表示一个bean定义对应多个对象实例。Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()方法)时都会创建一个新的bean实例。Prototype是原型类型,它在我们创建容器的时候并没有实例化,而是当我们获取bean的时候才会去创建一个对象,而且我们每次获取到的对象都不是同一个对象。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。在XML中将bean定义成prototype,可以这样配置:
<bean id="account" class="com.foo.DefaultAccount" scope="prototype"/> 或者 <bean id="account" class="com.foo.DefaultAccount" singleton="false"/>
Request作用域
当一个bean的作用域为Request,表示在一次HTTP请求中,一个bean定义对应一个实例;即每个HTTP请求都会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id="loginAction" class=cn.csdn.LoginAction" scope="request"/>
针对每次HTTP请求,Spring容器会根据loginAction bean的定义创建一个全新的LoginAction bean实例,且该loginAction bean实例仅在当前HTTP request内有效,因此可以根据需要放心的更改所建实例的内部状态,而其他请求中根据loginAction bean定义创建的实例,将不会看到这些特定于某个请求的状态变化。当处理请求结束,request作用域的bean实例将被销毁。
Session作用域
当一个bean的作用域为Session,表示在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。考虑下面bean定义:
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
针对某个HTTP Session,Spring容器会根据userPreferences bean定义创建一个全新的userPreferences bean实例,且该userPreferences bean仅在当前HTTP Session内有效。与request作用域一样,可以根据需要放心的更改所创建实例的内部状态,而别的HTTP Session中根据userPreferences创建的实例,将不会看到这些特定于某个HTTP Session的状态变化。当HTTP Session最终被废弃的时候,在该HTTP Session作用域内的bean也会被废弃掉。