spring中如何向一个单例bean中注入非单例bean

spring中如何向一个单例bean中注入非单例bean

错误实例

这里有一个原型(生命周期为prototype)的类

package com.example.myDemo.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope(value = "prototype")
public class Man  {

    public void eat() {
        System.out.println("I like beef");
    }
}

有一个单例(生命周期为singleton)的类

package com.example.myDemo.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.stereotype.Component;

@Component
public class Woman {
    //使用依赖注入的方式,注入原型的Man
    @Autowired
    private Man man;

    public void eat() {
        System.out.println("man:"+man);
        System.out.println("I like fruits");
    }
}

测试方法

package com.example.myDemo;

import com.example.myDemo.component.MyFactoryBean;
import com.example.myDemo.component.Woman;
import com.example.myDemo.po.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.ApplicationContext;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MyDemoApplication {
    public static void main(String[] args) {
        ApplicationContext ac=SpringApplication.run(MyDemoApplication.class, args);
        Woman woman=(Woman)ac.getBean("woman");
        for(int i=0;i<5;i++){
            woman.eat();
        }
    }
}

测试结果
在这里插入图片描述
上面的结果显示Woman中的man是单例的,因为5次循环打印打出的结果是同一个对象。

Woman是单例的,Man是原型的,使用常规的@Autowired注解注入的却是同一个实例,因为Woman是单例的,意味着在整个spring容器中只有一个实例,在属性注入的时候肯定也只会注入一次,所以其中Man属性也只能是一个实例,出现上图的结果也就不稀奇了。

现在有这样一个需求要向单例bean中注入原型bean,要怎么实现这样的需求。

实现ApplicationContextAware接口

ApplicationContextAware接口是spring提供的一个扩展点,实现该接口的类可以获得ApplicationContext
修改Woamn类

package com.example.myDemo.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class Woman implements ApplicationContextAware {
    private Man man;
    private ApplicationContext ac;
    
    public void eat() {
        this.man = (Man) ac.getBean("man");
        System.out.println("man:" + man);
        System.out.println("I like fruits");
    }
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.ac = applicationContext;
    }
}

Woman实现了ApplicationContextAware接口,注入了ApplicaitonContext对象,然后再eat()方法中通过AppicationContext获得Man的实例,测试结果
在这里插入图片描述
可以看到man属性是多例的也就是符合原型模式的定义。

在eat()方法中使用ApplicationContext的getBean方法获取Man,eat()方法每执行一次均会调用一次getBean方法,getbean方法在执行的时候的时候会判断Man的生命周期,如果是原型(prototype)的,那么每调用一次就会重新实例化一个Man,所以会出现上述的结果。

该方法有一个很大的缺点那就是和spring耦合度太高,不符合降低系统的耦合度的要求。

lookup method

spring也考虑了向一个单例bean中注入原型bean的情况,提供了@Lookup注解,在XML配置方式下是标签,这里仅使用注解的方式演示

package com.example.myDemo.component;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class Woman  {
    private Man man;
    public void eat() {
        this.man = createMan();
        System.out.println("man:" + man);
        System.out.println("I like fruits");
    }
    @Lookup
    public Man createMan(){
        return null;
    }
}

测试结果
在这里插入图片描述
上图显示man是一个多例的,也就是向单例bean中注入了原型bean,其作用的是@Lookup注解。

被@Lookup注解或配置的方法有如下要求:

public|protected [abstract] return-type methodName(no-argments)

  • 方法可以是public也可以是protected;
  • 方法可以是抽象的也可以是非抽象的;
  • 方法的返回值是要注入的类型,这里是prototype类型的类;
  • 方法没有入参;
  • 方法体可以是空的。具体返回值可以是null或任何类型,对结果没有影响;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值