首页 \ 问答 \ 如何使用@Bean批注配置子bean(How to configure child beans with @Bean annotation)

如何使用@Bean批注配置子bean(How to configure child beans with @Bean annotation)

我想在使用bean的@Bean类型定义时使用Spring的bean继承。 具体来说,让

public class Serwis {
    Integer a;
    Integer b;
    Map<Integer, Integer> m = new HashMap<>();
}

并假设基于xml的配置如下所示:

<bean id="absSerwis" class="service.Serwis"
      p:a="11">
    <property name="m">
        <map>
            <entry key="111" value="111"></entry>
        </map>
    </property>
</bean>

<bean id="defSerwis" parent="absSerwis"
      p:b="12"
  />

它确实创建了包含bean defSerwis的深层副本的bean absSerwis ; 特别是m的内容被复制。 现在,我想使用@Bean注释来定义像defSerwis这样的bean,比如

@Autowired
@Qualifier("absSerwis")
private Serwis absSerwis;

@Bean
public Serwis cccSerwis() {
    Serwis s = new Serwis();
    BeanUtils.copyProperties(absSerwis, s); //wrong; does shallow copy
    return s;
}

这样做的正确方法是什么?


I would like to use Spring's bean inheritance while employing the @Bean-type definition of beans. Specifically, let

public class Serwis {
    Integer a;
    Integer b;
    Map<Integer, Integer> m = new HashMap<>();
}

and suppose the xml-based configuration would look like:

<bean id="absSerwis" class="service.Serwis"
      p:a="11">
    <property name="m">
        <map>
            <entry key="111" value="111"></entry>
        </map>
    </property>
</bean>

<bean id="defSerwis" parent="absSerwis"
      p:b="12"
  />

which does create the bean defSerwis containing a deep copy of the bean absSerwis; in particular the contents of m are copied. Now, I would like to define beans like defSerwis using @Bean annotations, like

@Autowired
@Qualifier("absSerwis")
private Serwis absSerwis;

@Bean
public Serwis cccSerwis() {
    Serwis s = new Serwis();
    BeanUtils.copyProperties(absSerwis, s); //wrong; does shallow copy
    return s;
}

what is the proper way to do it?


原文:https://stackoverflow.com/questions/39724566
更新时间:2022-03-31 08:03

最满意答案

这包括在Node-RED文档的配置写入功能部分的末尾

您需要将npm模块添加到settings.js文件中。 你可以在~/.node-red/

您正在寻找的部分是functionGlobalContext部分。

...
functionGlobalContext: {
   fft: require('fft')
},
...

然后您可以通过以下方式访问功能节点中的模块:

var FFT = context.global.get('fft');
var fft = new FFT(n, inverse);
...

在安装fft模块的地方也要小心,它需要在~/.node-red/node_modules或全局安装,以便可以访问Node-RED


This is covered towards the end of the Configuration and the Writing Functions sections of the Node-RED docs

You need to add npm modules to the settings.js file. You can find this file in ~/.node-red/

The section you are looking for is the functionGlobalContext section.

...
functionGlobalContext: {
   fft: require('fft')
},
...

You would then access the module in the function node with the following:

var FFT = context.global.get('fft');
var fft = new FFT(n, inverse);
...

Also be careful where you installed the fft module, it needs to be either in ~/.node-red/node_modules or installed globally so it is accessable to Node-RED

相关问答

更多
  • 要POST XML,您需要确保将content-type标头设置为application/xml 。 例如,使用Function节点: msg.payload = 'foobardoit'; msg. ...
  • 不,设置不会暴露给Function节点,只会暴露在functionGlobalContext中的值。 对于大多数用户来说,这不是必需的,但如果您能够将一个引人注目的用例发布到邮件列表中,它将被视为功能请求。 这已在过去的邮件列表中进行了讨论,因此您可能希望在以后的任何讨论中引用它。 No, the settings are not exposed to the Function node, only the values in the functionGlobalContext. For the major ...
  • 为了在ui_template代码中使用外部js库,最好将src链接和html / css / svg代码放入单独的模板中。 在第一个中,选择“ 添加到网站部分 ”选项,并包含您的库引用: