首页 > 其他分享 >使用Spring方法注入协调不同步的bean

使用Spring方法注入协调不同步的bean

时间:2023-06-09 11:03:32浏览次数:33  
标签:同步 Spring System bean axe println lookupbean public

使用Spring方法注入协调不同步的bean

<? xml version="1.0" encoding="UTF-8" ?>
 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

 < beans >

   < bean  id ="chinese"  class ="Bean.lookupbean.Chinese" >
     < property  name ="axe" >
       < ref  local ="axe" />
     </ property >
     <!--  指定lookup注入方法,该方法用来保证每次产生的non-singleton bean都是新的实例,使用bean属性确定lookup方法的返回值  -->
     < lookup-method  name ="createAxe"  bean ="axe" />
   </ bean >
   < bean  id ="axe"  class ="Bean.lookupbean.SteelAxe"  singleton ="false" ></ bean >
 </ beans >

     spring bean有两种状态,singleton和non-singleton,区别是singletom在整个生命周期中只实例化一次,由spring进行管 理,而non-singleton可以实例化多次,spring不负责进行管理,当有依赖关系存在是,会有这样一个问题,当singleton bean依赖non-singleton时,由于singleton bean只初始化一次,而non-singleton可以初始化多次,这样就会造成不同步的想象,解决办法是使用spring提供的lookup方法注 入


定义接口:



package  Bean.lookupbean;

 public   interface  Person  ... {
    public Axe getAxe();
    public void useAxe();
    public abstract Axe createAxe();
}  

    


 package  Bean.lookupbean;

 public   interface  Axe  ... {
   public String chop();
}

实现类:


注意,方法注入所需要的方法,必须是abstract的


package  Bean.lookupbean;

 import  java.util.ArrayList;
 import  java.util.HashMap;
 import  java.util.HashSet;
 import  java.util.List;
 import  java.util.Map;
 import  java.util.Properties;
 import  java.util.Set;

 import  Bean.lookupbean.Person;

 public   abstract   class  Chinese  implements  Person  ... {
    private Axe axe;
    public void useAxe() ...{
        axe.chop();
        
    }
    public Axe getAxe() ...{
        return axe;
    }
    public void setAxe(Axe axe) ...{
        this.axe = axe;
    }
    //方法注入所需要的方法,由spring提供实现
    public abstract Axe createAxe();

  
}


 package  Bean.lookupbean;

 public   class  SteelAxe  implements  Axe  ... {
    private int count=0;
    public String chop() ...{
        
        return "这是一把铁斧子"+ ++count;

    }

}


配置文件;


这里要把axe bean设置成singleton=false否则,由于容器内只有一个目标bean实例,即使采用lookup注入,每次依然返回同一个实例,lookup方法不仅适用于设值注入,也适用于构造注入

<? xml version="1.0" encoding="UTF-8" ?>
 <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >

 < beans >

   < bean  id ="chinese"  class ="Bean.lookupbean.Chinese" >
     < property  name ="axe" >
       < ref  local ="axe" />
     </ property >
     <!--  指定lookup注入方法,该方法用来保证每次产生的non-singleton bean都是新的实例,使用bean属性确定lookup方法的返回值  -->
     < lookup-method  name ="createAxe"  bean ="axe" />
   </ bean >
   < bean  id ="axe"  class ="Bean.lookupbean.SteelAxe"  singleton ="false" ></ bean >
 </ beans >


测试代码:


public   static   void  main(String[] args)  throws  Exception  ... {
        
        String path=new Test().getClass().getResource("/").getPath();
        String realpath=path.substring(1, path.length());
        ApplicationContext context=new FileSystemXmlApplicationContext(realpath+"/lookupbean.xml");
        System.out.println("不使用lookup");
        Person p=(Person)context.getBean("chinese");
        System.out.println(p.getAxe());
        System.out.println(p.getAxe());
        System.out.println(p.getAxe().chop());
        System.out.println(p.getAxe().chop());
        System.out.println("使用lookup");
        System.out.println(p.createAxe());
        System.out.println(p.createAxe());
        System.out.println(p.createAxe().chop());
        System.out.println(p.createAxe().chop());
        
       
       
    }


运行结构:


不使用lookup

Bean.lookupbean.SteelAxe@1e87719

Bean.lookupbean.SteelAxe@1e87719

这是一把铁斧子1

这是一把铁斧子2

使用lookup

Bean.lookupbean.SteelAxe@e32802

Bean.lookupbean.SteelAxe@b23210

这是一把铁斧子1

这是一把铁斧子1


可以看到,不使用lookup,spring返回同一个实例(由于不同步的原因),使用lookup,每次返回一个新的实例


标签:同步,Spring,System,bean,axe,println,lookupbean,public
From: https://blog.51cto.com/u_16065168/6445890

相关文章

  • SpringAOP
    一、proxy增强1、基于JDKjava自带的代理功能,只能针对接口,目标类与代理类为平级关系publicclassJDKProxy{ interfaceFoo{ voidfoo(); } staticclassTargetimplementsFoo{ publicvoidfoo(){ System.out.println("targetfoo"); } } publicstaticvo......
  • 深入浅出Spring原理及实战「缓存Cache开发系列」
    1.  缓存Cache的概念和作用在现代软件开发中,缓存已经成为了一个非常重要的概念。缓存是指将数据存储在一个临时的存储器中,以便于快速访问和读取。缓存的作用是提高系统的性能和响应速度,减少网络流量和数据库的负载。以电影院购票为例,当用户选择一部电影时,系统需要查询电影的......
  • NetBeans快捷键与Eclipse的比较
    NetBeans快捷键与Eclipse的比较 首先声明:NetBeans只针对6.0正式版,其他版本没有进行过验证   因为自己最早是用Eclipse的,所以以前用NetBeans的时候,对于NetBeans的快捷键很不适应。严重影响了对于NetBeans的使用,估计有不少人都会有和我差不多的感觉。其实,快捷键的分布只是一个......
  • netbeans字体美化
    http://coffee13.blogspot.com/2008/06/netbeans-61.html2008年6月2日星期一装修netbeans6.1中文版一.准备工作:1.安装dejavu字体(ver2.25)。2.安装jdk1.6,这版本的字体支持反锯齿。3.安装netbeans6.1ml,最新中文版。二.美化netbeans编辑器:1.把“如何调整Netbeans......
  • 美团太细了:Springcloud 微服务优雅停机,如何实现?
    文章很长,且持续更新,建议收藏起来,慢慢读!疯狂创客圈总目录博客园版为您奉上珍贵的学习资源:免费赠送:《尼恩Java面试宝典》持续更新+史上最全+面试必备2000页+面试必备+大厂必备+涨薪必备免费赠送:《尼恩技术圣经+高并发系列PDF》,帮你实现技术自由,完成职业升级,薪......
  • spring tx:advice 和 aop:config 配置事务
    <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.or......
  • spring中获取属性的方法SpelExpressionParser()
    Aa=newA();Bb=newB();声明el表达式ExpressionParserpaeser=newSpelExpressionParser();设置你要通过el表达式取的字段Expressionaid=paeser.parseExpression("id");代表内置对象(b)中的id属性Expressionbid=paeser.parseExpression(b.id);如果b对象......
  • 用Spring Cloud Alibaba构建用户中心!只要5分钟
    你好,我是田哥。前几天,有几位朋友私信我,叫我写一个使用SpringCloudAlibaba搭建项目。今天,我给安排上,使用SpringCloudAlibaba搭建一个用户中心。下面,我就来给大家说一下,我饿还是如何搭建的,具体步骤如下:使用IDEA创建项目结构添加maven相关依赖安装Nacos业务代码整个技术栈:Spring......
  • spring mvc
    springmvc接收json格式字符串@PostMapping("jsonhello")publicStringjsonhello(@RequestBodyMap<String,Object>map){System.out.println(JSONObject.toJSONString(map));return"SUCCESS";}apipost请求wireshark......
  • SpringBoot+Vue+Spring Cloud Alibaba 实现大型电商系统【分布式微服务实现】
    前言这篇博客主要记录的是谷粒商城项目的完整开发过程大家想看的话也可以去这里看看:笔记地址传送门后续还会继续维护这个笔记一、项目架构二、技术总和三、开发准备1、开发环境搭建2、工程初始化四、分布式基础篇(全栈开发篇)五、分布式高级篇(微服务架构篇)......