Home > Mobile >  Java Migration Application context xml to @Beans
Java Migration Application context xml to @Beans

Time:06-28

Im trying to convert my context xml of hikari db setting to Beans method

<bean id="proxyConfig"
      factory-bean="proxyConfigSupport"
      factory-method="create"/>

<bean id="proxyConfigSupport" >
    <property name="dataSourceName" value="dataSourceDefaultName"/>
    <property name="queryListener" ref="queryListener"/>
</bean>

Im trying to understand how to convert it so I tried this way

@Bean(name = "proxyConfigSupport")
public ProxyConfigSpringXmlSupport proxyDataSource(ChainListener queryListener) {
    ProxyConfigSpringXmlSupport proxyConfigSpringXmlSupport = new ProxyConfigSpringXmlSupport();
    proxyConfigSpringXmlSupport.setDataSourceName("dataSourceDefaultName");
    proxyConfigSpringXmlSupport.setQueryListener(queryListener);
    return proxyConfigSpringXmlSupport;
}

But what does it mean "proxyConfig" factory-bean="proxyConfigSupport" How do i set it the same ?

@Bean(name = "proxyConfig")
    public ProxyConfig proxyConfig(ProxyConfigSpringXmlSupport proxyConfigSpringXmlSupport) {
        ProxyConfig proxyConfig = new ProxyConfig();
        return proxyConfig;
            /*
        <bean id="proxyConfig"
          factory-bean="proxyConfigSupport"
          factory-method="create"/>

     */
    }

CodePudding user response:

Lets divide the XML up into sections.

<bean id="proxyConfigSupport" >
    <property name="dataSourceName" value="dataSourceDefaultName"/>
    <property name="queryListener" ref="queryListener"/>
</bean>

You already got that bean method

@Bean
public ProxyConfigSpringXmlSupport proxyConfigSupport(ChainListener queryListener) {
    ProxyConfigSpringXmlSupport proxyConfigSpringXmlSupport = new ProxyConfigSpringXmlSupport();
    proxyConfigSpringXmlSupport.setDataSourceName("dataSourceDefaultName");
    proxyConfigSpringXmlSupport.setQueryListener(queryListener);
    return proxyConfigSpringXmlSupport;
}

Now the second one

<bean id="proxyConfig"
      factory-bean="proxyConfigSupport"
      factory-method="create"/>

Is nothing more then a description of what should take place. It defines a FactoryBean and specifies it do delegate the getObject() to the proxyConfigSupport bean and call the create() method. This is also explained in the Spring Framework Reference Guide.

As an @Bean method is also, kind of, a factory for beans, you can do something like this.

@Bean
public ProxyConfig proxyConfig(ProxyConfigSpringXmlSupport proxyConfigSupport) {
  return proxyConfigSupport.create();
}

Nothing more and nothing less.

  • Related