2019-04-25_Spring单例学习

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配置

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">










    



    


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单例学习)