第1章:spring注入/1.6 一个Bean依赖另外一个Bean/1.6.2 @Service方式/1.6.2.2 单例模式

  • 单例模式
  1. 需求:一个类的属性中要用到另外一个类的Bean,而且要求属性Bean要先初始化
  2. 配置文件,它集成了很多配置项,比如:

    authcode:

       check:

          num: 5

          time: 120

  3. 被依赖的类(比如配置类),很多类都会用到这个配置项,比如:
    package com.yxbj.config;
    
    import org.springframework.beans.factory.annotation.Value;
    
    public class ConfigParam {
    
    	@Value("${authcode.check.num}")
    	int checkNum;
    
    	@Value("${authcode.check.time}")
    	int checkTime;
    
    	public int getCheckNum() {
    		return checkNum;
    	}
    
    	public void setCheckNum(int checkNum) {
    		this.checkNum = checkNum;
    	}
    
    	public int getCheckTime() {
    		return checkTime;
    	}
    
    	public void setCheckTime(int checkTime) {
    		this.checkTime = checkTime;
    	}
    
    	@Override
    	public String toString() {
    		return "ConfigParam [checkNum=" + checkNum + ", checkTime=" + checkTime + "]";
    	}
    
    	public ConfigParam() {
    		System.out.println("ConfigParam初始化");
    	}
    
    }
    
  4. 依赖类,它有一个属性依赖配置类,比如:
    package com.yxbj.authcode;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.yxbj.config.ConfigParam;
    
    @Service
    public class ServiceNoSingleton {
    
    	@Autowired
    	ConfigParam configParam;
    
    	public ServiceNoSingleton(ConfigParam configParam) {
    		System.out.println("构造器传入参数" + configParam);
    	}
    
    	public boolean isTimeout(int time) {
    		System.out.println(configParam);
    		if (time > configParam.getCheckTime()) {
    			return true;
    		}
    		return false;
    	}
    }
    
    1. 类上加注解

      @Service

      public class ServiceSingleton {

    2. 定义要依赖的属性

      @Autowired

      ConfigParam configParam;

    3. 有参数构造器:

      /**

            * 修改成私有变成单例模式

            *

      扫描二维码关注公众号,回复: 6500257 查看本文章

            * @param configParam

            */

           private ServiceSingleton(ConfigParam configParam) {

                 System.out.println("构造器传入参数" + configParam);

           }

  5. 创建一个配置类,比如:

    @Configuration

    public class BeanConfig {

         @Bean(name = "configParam")

         public ConfigParam configParam() {

               return new ConfigParam();

         }

    //这里的Bean用@Service代替

    //   @Bean

    //   @DependsOn(value = { "configParam" })

    //   public DependOnSingleton dependOnNoSingleton() {

    //         System.out.println("获取单例构造器:AuthCodeManage");

    //         DependOnSingleton dependOnSingleton = DependOnSingleton.getSignleton();

    //         return dependOnSingleton;

    //   }

    }

  6. 使用依赖类,比如在Control中使用:
    package com.yxbj.control;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.yxbj.authcode.ServiceNoSingleton;
    
    @RestController
    public class MyUserControl {
    
    	@Autowired
    	ServiceNoSingleton serviceNoSingleton;
    
    	@GetMapping("user")
    	public String getUser() {
    		boolean b = serviceNoSingleton.isTimeout(150);
    		if (b) {
    			return "已经超时";
    		} else {
    			return "未超时";
    		}
    	}
    }
    
  7. 启动过程日志:

    ConfigParam初始化

    构造器传入参数ConfigParam [checkNum=5, checkTime=120]

  8. 调用:http://localhost:8080/user/
  9. 完整源代码:

猜你喜欢

转载自blog.csdn.net/u011830122/article/details/92089238