简体   繁体   中英

Java Migration Application context xml to @Beans

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" class="net.ttddyy.dsproxy.support.ProxyConfigSpringXmlSupport">
    <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"/>

     */
    }

Lets divide the XML up into sections.

<bean id="proxyConfigSupport" class="net.ttddyy.dsproxy.support.ProxyConfigSpringXmlSupport">
    <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM