第二讲 使用Spring IoC创建对象的3种方式

来源:互联网 发布:postgresql vs mysql 编辑:程序博客网 时间:2024/05/22 23:05
一、通过无参的构造方法来创建对象

  1. 编写实体类

publicclass User {
     
     publicUser() {
           System.out.println("user的无参构造方法");
     }
     privateString name;
     
     publicvoid setName(String name) {
           this.name= name;
     }
     
     publicvoid show() {
           System.out.println("name="+ name);
     }
     
}

  1. 编写beans.xml配置文件

<beansxmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd">
     
     <!-- scope属性默认是单例的可以不用写 -->
     <beanid="user"class="com.liujie.model.User"scope="singleton">
           <propertyname="name"value="张三"></property>
     </bean>
</beans>

   spring中bean的作用域(scope属性),有如下5种类型:

  • singleton:单例模式,在整个Spring IoC容器中,使用singleton定义的Bean将只有一个实例;
  • prototype:原型模式,每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例;
  • request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不同的Bean实例。只有在    Web应用中使用Spring时,该作用域才有效;
  • session:对于每次HTTP Session,使用session定义的Bean都将产生一个新实例。同样只有在Web应用中使用Spring时,该作用域才有效;
  • global session:每个全局的HTTP Session,使用session定义的Bean都将产生一个新实例。典型情况下,仅在使用portlet context的时候有效。同样只有在Web应用中使用Spring时,该作用域才有效。

   在多数情况,我们只会使用singletonprototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton

  1. 测试

publicclass Test {
     
     publicstatic void main(String[]args) {
           // 解析beans.xml文件,生成管理相应的bean对象
           ApplicationContextcontext = newClassPathXmlApplicationContext("beans.xml");
           // BeanFactory context = new ClassPathXmlApplicationContext("beans.xml");
           Useruser = (User) context.getBean("user");
           user.show();
           //用来检测spring创建user对象时是否使用的是单例模式(通过构造方法是否被调用多次来检测的)
           context.getBean("user");
           context.getBean("user");
     }
     
}

二、通过有参构造方法来创建对象

  1. 编写实体类

publicclass User {
     
     privateString name;
     
     publicUser(String name) {
           super();
           this.name= name;
     }
     
     publicvoid show() {
           System.out.println("name="+ name);
     }
     
}

  1. 编写beans.xml配置文件

          第一种:根据参数的下标来设置

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd">
     
     <beanid="user"class="com.liujie.model.User">
           <!-- index指构造方法参数,下标从0开始 -->
           <constructor-argindex="0"value="李四"></constructor-arg>
     </bean>
</beans>

          第二种:根据参数名称来设置

     <beanid="user"class="com.liujie.model.User">
           <!-- name指参数名 -->
           <constructor-argname="name"value="李四"></constructor-arg>
     </bean>

          第三种:根据参数类型来设置

     <beanid="user"class="com.liujie.model.User">
           <constructor-argtype="java.lang.String"value="李四"></constructor-arg>
     </bean>

  1. 测试

publicclass Test {
     
     publicstatic void main(String[]args) {
           ApplicationContextcontext = newClassPathXmlApplicationContext("beans.xml");
           Useruser = (User) context.getBean("user");
           user.show();
     }
     
}

三、通过工厂方法来创建对象

     静态工厂:

  1. 编写实体类

publicclass User {
     
     privateString name;
     
     publicUser(String name) {
           super();
           this.name= name;
           System.out.println("user有参构造方法");
     }
     
     publicvoid show() {
           System.out.println("name="+ name);
     }
     
}

  1. 编写工厂类

publicclass UserFactory {
     
     publicstatic User newInstance(Stringname) {
           returnnew User(name);
     }
     
}

  1. 编写beans.xml配置文件

     <beanid="user"class="com.liujie.factory.UserFactory"factory-method="newInstance">
           <constructor-argindex="0"value="王五"></constructor-arg>
     </bean>

  1. 测试

publicclass Test {
     publicstatic void main(String[]args) {
           ApplicationContextcontext = newClassPathXmlApplicationContext("beans.xml");
           Useruser = (User) context.getBean("user");
           user.show();
           
           context.getBean("user");
           context.getBean("user");
     }
}

     动态工厂:

  1. 编写工厂类

publicclass UserDynamicFactory {
     
     publicUser newInstance(Stringname) {
           returnnew User(name);
     }
     
}

  1. 编写beans.xml配置文件

     <beanid="userFactory"class="com.liujie.factory.UserDynamicFactory"></bean>
     <beanid="user"factory-bean="userFactory"factory-method="newInstance">
           <constructor-argindex="0"value="王五"></constructor-arg>
     </bean>

   不管是动态工厂还是静态工厂,user对象还是受
   <beanid="user"factory-bean="userFactory"factory-method="newInstance"scope="prototype">
   中的scope参数的影响。



   bean创建对象的方式
   如果bean对象的scope="singleton"(默认),bean对象是直接创建的,通过applicationContext.getBean获取对象时,永远都
   不会创建新的对象
   如果bean对象的scope="prototype",bean对象不会直接创建,编写bean以后一定要通过applicationContext.getBean获取对象
   时,才会是创建对象(如果有被引用的bean且scope="prototype",这个时候也会被创建),并且获取一次创建一次