将应用程序上下文转换为 Java 配置 Spring Boot

Convert application context to Java Config Springboot

寻找一些关于将下面现有 applicationContext.xml 转换为新 Springboot 应用程序的 Java 配置的指导。

<bean id="InitialHandler" class="package.InitialHandler" 
  scope="prototype">
  <property name="rawResponseTemplate" 
   ref="rawResponseRestTemplate"/>
  <property name="mimeMapper" ref="initialMimeMapper"/>
</bean>

<bean id="rawResponseRestTemplate" 
   class="org.springframework.web.client.RestTemplate">
  <property name="messageConverters">
    <list>
        <bean class="package.CommonDocumentRawMessageConverter"/>
    </list>
  </property>
</bean>

<bean id="initialMimeMapper" 
class="package.ConfigurationBasedMapper">
  <property name="configuration" ref="initialMimeConfig"/>
</bean>

<bean id="initialMimeConfig" 
class="org.apache.commons.configuration.XMLPropertiesConfiguration" 
scope="singleton">
  <constructor-arg type="java.net.URL" 
 value="classpath:FileINeedLoaded.xml"/>
  <property name="reloadingStrategy" ref="ReloadingStrategy"/>
</bean>

// snippet of CommonDocumentRawMessageConverter below
import org.springframework.http.converter.HttpMessageConverter;
public class CommonDocumentRawMessageConverter implements 
  HttpMessageConverter<CommonDocument>  {}

这是我的:

@Bean
public RestTemplate rawResponseRestTemplate() {
    RestTemplate rawResponseRestTemplate = new RestTemplate();
    rawResponseRestTemplate.setMessageConverters(msgConverters());    
    return rawResponseRestTemplate;
}

private List<HttpMessageConverter<?>> msgConverters() {
    List<HttpMessageConverter<?>> newList = new ArrayList<HttpMessageConverter<?>>();
    newList.add(new CommonDocumentRawMessageConverter());
    return newList;
}

服务编译正常,但出现如下运行时异常:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under 'prop.mime-mapper' to 
package.util.Mapper<java.lang.String>:

Property: prop.mime-mapper
Value: propMimeMapper
Origin: "prop.mimeMapper" from property source "Config resource 'class 
path resource [application.yml]' via location 'optional:classpath:/' 
(document #0)"
Reason: org.springframework.core.convert.ConverterNotFoundException: No 
converter found capable of converting from type [java.lang.String] to 
type [package.util.Mapper<java.lang.String>]

Action:

Update your application's configuration

我的配置片段:

@Getter
@Setter
public class propConfig {
   private String randomProp;
   private Mapper<String> mimeMapper;
   private RestTemplate rawResponseTemplate;

服务:

@Slf4j
@Service
public class PropService extends DocumentManagementService {

private final RestTemplate rawResponseTemplate;
private final Mapper<String> mimeMapper;

public PropService(@Autowired PropConfig propConfig) {
    this.rawResponseTemplate = propConfig.getRawResponseTemplate();
    this.mimeMapper = propConfig.getMimeMapper();
}

似乎在运行时,定义为 RestTemplate 和 Mapper 的属性由于某种原因没有得到转换。

更新一个 我能够通过从 application.yaml 中完全删除这些属性来解决上述问题。

private Mapper<String> mimeMapper;
private RestTemplate rawResponseTemplate;

我意识到这些属性在原来的 applicationContext.xml 中实际上是 'refs',因此在新的 application.yaml 中不需要。我确实有新问题但是与我将分享的此转换相关。我会保留原来的配置以防其他人有同样的问题。

我能够通过从 application.yaml 中完全删除这些属性来解决上述问题。

private Mapper<String> mimeMapper;
private RestTemplate rawResponseTemplate;

我意识到这些属性在原来的 applicationContext.xml 中实际上是 'refs',因此在新的 application.yaml 中不需要。