解决SpringBoot中自己new出来的对象不能自动注入对象和属性的问题

本文介绍如何在Spring框架中使用自定义工具类GetBeanUtil进行Bean的管理和注入,以及如何通过GetPropertiesUtil工具类解决自定义类中属性的注入问题。详细展示了如何在未被Spring管理的类中获取Spring管理的Bean实例,以及如何在这些类中正确注入配置文件中的属性。

鉴于大家留言中指出的所有错误问题,现在我把公司里的源代码贴出来供大家参考:

 懒得看解释的,直接用这个类即可:      

@Component
public class GetBeanUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (GetBeanUtil.applicationContext == null) {
            GetBeanUtil.applicationContext = applicationContext;
        }
    }

    /**
     * 返回ApplicationContext
     *
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 返回bean
     *
     * @param beanName beanName
     * @return bean
     */
    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    /**
     * 获取bean
     *
     * @param c c
     * @param <T> 泛型
     * @return bean
     */
    public static <T> T getBean(Class<T> c) {
        return applicationContext.getBean(c);
    }

    /**
     * 获取bean
     * @param c c
     * @param  name 名称
     * @param <T> 泛型
     * @return T 泛型
     */
    public static <T> T getBean(String name, Class<T> c) {
        return getApplicationContext().getBean(name, c);
    }
}

使用方法:

public class AreaInvadeAnalyzer extends Analyzer {

    private AreaInvadeMapper areaInvadeMapper =GetBeanUtil.getBean(AreaInvadeMapper.class);

    ....其他代码省略

}

-----------------------------------------------------------------------------------------------------------------------下面旧文可以不看------------------------------------------------------------------------------------------------------------------------------------------------

在类上加@Component或者其他注解时,该类会交给spring来管理,自动创建对象,但是如果是自己new出来的对象或者自己类没加注解,但是内部又需要注入其他对象以及需要注入配置文件中的属性时,有以下办法解决:

1、解决注入对象问题:

新建一个工具类,比如GetBeanUtil

public class GetBeanUtil implements ApplicationContextAware {
    protected static ApplicationContext context;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);//name表示其他要注入的注解name名
    }

    public static <T> T getBean(T c){

        return applicationContext.getBean(c);//T表示类
    }
}

举例使用上述工具类:

public MyClass
{
    @AutoWired
    private OtherClass otherClass;
}

上面类在new MyClass时,OtherClass类注入会失败,此时改进一下情况即可:(注意,OtherClass必须是spring注解过的)

public MyClass
{
    private OtherClass otherClass = GetBeanUtil.getBean(OtherClass.class);
}

简单解释一下工具类为什么能够找到类,是因为在启动spring后,spring会将所有注解过的类加载容器内,并返回 给ApplicationContextAware,在其他注解后的类需要对象时,容器会自动注入,但是自己new的类缺不会被容器注入,此时,自己实现ApplicationContextAware,自己根据类的类型手动获得加载后的bean即可。

2、解决注入属性问题

创建一个工具类:

@Component//注意此时要加注解
public class GetPropertiesUtil
{
    
    private static String properties;
    
    public static Stirng getProperties()
    {    
        return properties;
    }
    @Value("${properties}")
    public void setProperties(String properties)
    {
        this.properties = proterties;
    }
}

注意两点:1、该工具类要加上注解,否则会像第一种情况;

                  2、内部属性加@Value的地方是set方法上,而且set方法不是statis;

 

使用举例:

public MyClass
{
    @Value("${properties}")
    private String properties;
}

上面类在new MyClass时,xxx属性注入会失败,此时改进一下情况即可:

public MyClass
{
    private String properties = GetPropertiesUtil.getProperties();
}

 

评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值