(Spring框架)单例和多例以及依赖注入

本文介绍了Spring框架中bean的单例(singleton)和多例(prototype)的区别,并通过示例代码说明了如何通过配置文件设置bean的scope属性。在单例模式下,多次获取bean会得到相同实例,而在多例模式下,每次获取将创建新的实例。同时,文章还阐述了依赖注入的概念,展示了如何通过Spring配置文件实现对象间的依赖关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*****************************************************************************************************/ 
22:50 2017/10/18      (Spring框架)单例和多例以及依赖注入
/*****************************************************************************************************/ 
·bean属性中,常用的属性还有scope,其默认值是"singleton".
  scope属性:当值是singleton表示单例,当值是prototype表示多例。   
  这里单例和多例的区别:
            (1)、单例:多次用factory.getBean("user",user.class)获得实体类,获得是同一个类。
                 多例:多次用factory.getBean("user",user.class)获得实体类,获得是多个类。
            (2)、范例:
            ·单例(两次取到的对象是一样的):
                 |-:配置文件:  
                      <?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
                      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                      <!-- 相当于User user = new User() -->
                      <!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
                      <bean id = "user" class = "com.wk.model.user" scope = "singleton"></bean>
                    </beans>   
                 |-:测试类:
                   package com.wk.test;


                   import static org.junit.Assert.*;


                   import org.junit.Test;
                   import org.springframework.beans.factory.BeanFactory;
                   import org.springframework.context.support.ClassPathXmlApplicationContext;


                   import com.wk.model.user;


                   public class TestUser {
           @Test
           public void test01() {
                   //加载xml配置文件。
   BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
           //得到配置文件中的对象
                   user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
                   user u2 = factory.getBean("user",user.class);
                   System.out.println(u1 == u2);
                  }
               } 
             结果是:ture。
             ·多例(两次取到的对象不一样):
                 |-:配置文件:  
                      <?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
                      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                      <!-- 相当于User user = new User() -->
                      <!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
                      <bean id = "user" class = "com.wk.model.user" scope = "prototype"></bean>
                    </beans>   
                 |-:测试类:
                   package com.wk.test;


                   import static org.junit.Assert.*;


                   import org.junit.Test;
                   import org.springframework.beans.factory.BeanFactory;
                   import org.springframework.context.support.ClassPathXmlApplicationContext;


                   import com.wk.model.user;


                   public class TestUser {
           @Test
           public void test01() {
                   //加载xml配置文件。
   BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
           //得到配置文件中的对象
                   user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
                   user u2 = factory.getBean("user",user.class);
                   System.out.println(u1 == u2);
                  }
               } 
               结果是false。
不写scope的话默认值是单例。
·判断对象是单例还是多例:
      判断一个对象到底是单例还是多例,依据就是:在不同的状态下是否会发生改变。如果说在不同的状态
   下会发生改变,就是多例。如果说在不同的状态下不会发生改变,就是单例。
       1、若对象中有属性,但是属性不会发生变化,就是单例。
       2、一个对象里面没有属性,就是使用单例。
·在各个类中只是声明了一个变量,但没有创建对象,创建对象在配置文件中完成。      
·范例:
    包层:action、dao、dao(impl)、model、service、service(impl)、test
    action:完成与界面层的传值和跳转;负责与页面的传值和跳转。
    service: 处理业务逻辑
      * 以面向接口编程的形式,较为灵活。
      * 1、加密或解密。
      * 2、异常处理。
      * 3、验证码的问题。
    !!!配置文件:
    <?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
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
      <!-- 相当于User user = new User() -->
      <!-- scope属性:当值是singleton表示单例,当值是prototype表示多例 -->
      <bean id = "user" class = "com.wk.model.user" scope = "prototype"></bean>
      <bean id = "userDao" class = "com.wk.dao.UserDao" scope = "prototype"></bean>
      <bean id = "userService" class = "com.wk.service.UserService">
             <!-- 依赖注入:把用到的userDao对象注入到userService层中,
                                 这样就相当于在userService类之中创建了一个userDao对象  --> 
            <!-- 依赖注入:name属性的值与userService中的属性名称相对应,ref与上面的id属性相对应  -->                               
            <property name = "userDao" ref = "userDao"></property> <!-- ref与上面注入对象的id值对应 -->
      </bean>
      <bean id = "Action" class = "com.wk.action.Action" scope = "prototype">
            <property name="userService" ref = "userService"></property>
      </bean>
      
</beans>   
    !!!测试类:
package com.wk.test;


import static org.junit.Assert.*;


import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.wk.action.Action;
import com.wk.model.user;


public class TestUser {
@Test
public void test01() {
           //加载xml配置文件。
   BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
       //得到配置文件中的对象
//    user u = (user)factory.getBean("user");//向下转型易出差错
           user u1 = factory.getBean("user",user.class);//运行时类,这样就避免了强制转换
//    u1.show();
           /**
            * 判断一个对象到底是单例还是多例,依据就是:在不同的状态下是否会发生改变。如果说在不同的状态
            * 下会发生改变,就是多例。如果说在不同的状态下不会发生改变,就是单例。
            * 1、若对象中有属性,但是属性不会发生变化,就是单例。
            * 2、一个对象里面没有属性,就是使用单例。
            */ 
           user u2 = factory.getBean("user",user.class);
           System.out.println(u1 == u2);
}
public void test02() {
//加载xml配置文件
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
Action userAction = factory.getBean("Action", Action.class);
user user = new user(1,"张三","123");
userAction.setUser(user);
userAction.add();


Action userAction2 = factory.getBean("userAction", Action.class);
userAction2.add();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值