美文网首页
2019-04-25_Spring单例学习

2019-04-25_Spring单例学习

作者: kikop | 来源:发表于2019-04-25 07:42 被阅读0次

    Spring单例学习
    1.概述
    2.练习
    2.1.接口定义
    package com.tech.ability.springstudy.service;

    /**

    • Created by kikop on 2019/2/25.
      */
      public interface IHelloWorldService {

      public void sayHello();

    }
    2.2.接口实现
    package com.tech.ability.springstudy.service.impl;

    import com.tech.ability.springstudy.service.IHelloWorldService;

    /**

    • Created by kikop on 2019/2/25.
      */
      public class SpringHelloWorldImpl implements IHelloWorldService {

      @Override
      public void sayHello() {
      System.out.println("SpringHelloWorldImpl");
      }
      }

    2.3.后台属性注入
    package com.tech.ability.springstudy;

    import com.tech.ability.springstudy.service.IHelloWorldService;

    /**

    • Created by kikop on 2019/2/25.
      */
      public class HelloWorldServiceInject {
    //单例测试
    public int globalVal = 0;
    
    public int getGlobalVal() {
        return globalVal;
    }
    
    public void setGlobalVal(int globalVal) {
        this.globalVal = globalVal;
    }
    
    
    //属性注入
    private IHelloWorldService iHelloWorldService;
    
    public IHelloWorldService getiHelloWorldService() {
        return iHelloWorldService;
    }
    
    public void setiHelloWorldService(IHelloWorldService iHelloWorldService) {
        this.iHelloWorldService = iHelloWorldService;
    }
    
    public HelloWorldServiceInject() {
    }
    

    }

    2.4.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
    http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--这里定义Spring配置文件,并声明所有可用的bean-->
    
    <!--1.定义实现 iHelloWorldService接口的bean对象-->
    <bean id="springHelloWorldImplSingleTon" class="com.tech.ability.springstudy.service.impl.SpringHelloWorldImpl"></bean>
    
    <bean id="structHelloWorldImpl" class="com.tech.ability.springstudy.service.impl.StructHelloWorldImpl"></bean>
    
    <!--2.引用实现 iHelloWorldService接口的bean对象,默认单例Singleton-->
    <bean id="helloWorldServiceInjectSingleTon" class="com.tech.ability.springstudy.HelloWorldServiceInject"
          scope="singleton">
        <property name="iHelloWorldService" ref="springHelloWorldImplSingleTon"></property>
    </bean>
    
    <bean id="helloWorldServiceInjectNew" class="com.tech.ability.springstudy.HelloWorldServiceInject"
          scope="prototype">
        <property name="iHelloWorldService" ref="springHelloWorldImplSingleTon"></property>
    </bean>
    

    </beans>
    2.5.测试
    package com.tech.ability.springstudy;

    import com.tech.ability.mycommonutils.DateUtil;
    import com.tech.ability.springstudy.service.IHelloWorldService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**

    • Created by kikop on 2019/2/25.
      */
      public class MySpringTest {

      /**

      • spring属性注入测试
        */
        public static void ioctest() {

        //1.引用xml文件,xml等文件放在resources里面,独立于webapp下面的东西。
        ApplicationContext context =
        new ClassPathXmlApplicationContext("myspring/spring.xml");

        //2.获取指定服务
        HelloWorldServiceInject service =
        (HelloWorldServiceInject) context.getBean("helloWorldServiceInjectSingleTon");

        //2.获取服务下注入的Bean(SpringHelloWorldImpl、StructHelloWorldImpl)
        IHelloWorldService hw = service.getiHelloWorldService();

        //3.测试
        hw.sayHello();

      }

    public static void singletonTest() {
    
        //1.引用xml文件,xml等文件放在resources里面,独立于webapp下面的东西。
        ApplicationContext context =
                new ClassPathXmlApplicationContext("myspring/spring.xml");
    
        //2.获取指定服务
        HelloWorldServiceInject service =
                (HelloWorldServiceInject) context.getBean("helloWorldServiceInjectSingleTon");
    
        //2.1.测试
        service.setGlobalVal(100);
        System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service.getGlobalVal())));
    
        //3.再次获取指定服务
        HelloWorldServiceInject service2 =
                (HelloWorldServiceInject) context.getBean("helloWorldServiceInjectSingleTon");
        System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service2.getGlobalVal())));
    }
    
    public static void prototypeTest() {
        //1.引用xml文件,xml等文件放在resources里面,独立于webapp下面的东西。
        ApplicationContext context =
                new ClassPathXmlApplicationContext("myspring/spring.xml");
    
        //2.获取指定服务
        HelloWorldServiceInject service =
                (HelloWorldServiceInject) context.getBean("helloWorldServiceInjectNew");
        //2.1.测试
        service.setGlobalVal(100);
        System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service.getGlobalVal())));
    
    
        //3.再次获取指定服务
        HelloWorldServiceInject service2 =
                (HelloWorldServiceInject) context.getBean("helloWorldServiceInjectNew");
        System.out.println(DateUtil.getCurrentThreadInfo(String.valueOf(service2.getGlobalVal())));
    }
    
    public static void main(String[] args) {
    
        System.out.println("singletonTest");
        singletonTest();
        System.out.println("prototypeTest");
        prototypeTest();
    }
    

    }
    2.6.结果查看

    singletonTest
    2019-04-25 07:40:53.092 INFO [main][AbstractApplicationContext.java:589] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@21a947fe: startup date [Thu Apr 25 07:40:53 CST 2019]; root of context hierarchy
    2019-04-25 07:40:53.148 INFO [main][XmlBeanDefinitionReader.java:316] - Loading XML bean definitions from class path resource [myspring/spring.xml]
    [2019-04-25 07:40:53] main:100
    [2019-04-25 07:40:53] main:100
    prototypeTest
    2019-04-25 07:40:53.582 INFO [main][AbstractApplicationContext.java:589] - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e55dd0c: startup date [Thu Apr 25 07:40:53 CST 2019]; root of context hierarchy
    2019-04-25 07:40:53.583 INFO [main][XmlBeanDefinitionReader.java:316] - Loading XML bean definitions from class path resource [myspring/spring.xml]
    [2019-04-25 07:40:53] main:100
    [2019-04-25 07:40:53] main:0

    Process finished with exit code 0

    参考
    1.实战Java高并发程序设计

    相关文章

      网友评论

          本文标题:2019-04-25_Spring单例学习

          本文链接:https://www.haomeiwen.com/subject/rdwogqtx.html