如何在通用类中获取注入的Bean的值

huangapple go评论48阅读模式
英文:

How to get values of injected beans in a common class

问题

我正在使用Springboot开发一个Web服务器,需要为其添加一些自定义配置。因此,我将配置放入resources/application.properties中,如下所示:

apple.price=1

在某些函数中,我需要获取这个配置。

class MyObj {
    @Value
    private int applePrice;
}

这不起作用,因为MyObj不受Springboot管理。

但是,我不能为MyObj类添加像@Component这样的注释,因为MyObj的对象是手动初始化的:MyObj obj = new MyObj();

所以看来我需要在一个非受管理的对象中获取注入的Bean。

我是一个Springboot新手,请在这个问题上帮助我。

英文:

I'm using Springboot to develop a web server and I need to add some custom configurations for it. So I put the config into resources/application.properties as below:

apple.price=1

In some function I need to get the config.

class MyObj {
    @Value
    private int applePrice;
}

This is not working because MyObj is not managed by Springboot.

But I couldn't add any annotation like @Component for the class MyObj because the objects of MyObj is initialized manually: MyObj obj = new MyObj();

So it seems that I need to get the injected bean in an unmanaged object.

I'm a Springboot newbie, please help me on this issue.

答案1

得分: 2

你需要创建一个由Spring管理的bean,并将属性注入到该bean中。然后,你可以将它作为构造函数参数传递给你的MyObj

这是一个示例:

@Component // 将由Spring实例化为单例bean
public class MyObjFactory {

    @Value("${apple.price}")
    private int applePrice;

    public MyObj newMyObject() {
        return new MyObj(applePrice);
    }
}

请记住,你不应该使用new操作符手动创建MyObjFactory。Spring在上下文创建期间实例化标记为@Component的类。因此,你需要在想要使用它的地方注入该工厂。你可以在这里阅读更多信息:https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html

英文:

You need to create a spring managed bean and inject property into that bean. Then you can pass it as a constructor argument to your MyObj.

Here is the example:

@Component // will be instantiated by Spring as a singleton bean
public class MyObjFactory {

    @Value("${apple.price}")
    private int applePrice;

    public MyObj newMyObject() {
        return new MyObj(applePrice);
    }
}

Remember that you should not manually create MyObjFactory using the new operator. Spring instantiates classes marked as @Component during context creation. So you need to inject that factory where you want to use it. You can read more about it here: https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html

答案2

得分: 0

如果您没有访问应用程序上下文或其他Bean的权限,可以保留对AutowireCapableSpringBeanFactory的静态引用,以手动自动装配手动创建的对象。您可以像这个答案中那样操作。

然后像这样自动装配实例:

MyObj obj = new MyObj();
ApplicationContextHolder.autowireBean(obj);
英文:

If you don't have access to the application context or other beans, you can keep static reference to AutowireCapableSpringBeanFactory to manually autowire manually created objects. You could do it as in this answer, for example.

Then autowire the instances like this:

MyObj obj = new MyObj();
ApplicationContextHolder.autowireBean(obj);

huangapple
  • 本文由 发表于 2023年5月29日 21:53:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定