View Javadoc
1   /*
2    * Copyright 2022-2026 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.xml.spring.boot;
18  
19  import java.util.List;
20  import java.util.Optional;
21  import java.util.ServiceLoader;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.bremersee.xml.JaxbContextBuilder;
25  import org.bremersee.xml.JaxbContextDataProvider;
26  import org.springframework.boot.autoconfigure.AutoConfiguration;
27  import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
28  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29  import org.springframework.boot.context.event.ApplicationReadyEvent;
30  import org.springframework.context.annotation.Bean;
31  import org.springframework.context.event.EventListener;
32  import org.springframework.util.ClassUtils;
33  
34  /**
35   * The jaxb context builder autoconfiguration.
36   *
37   * @author Christian Bremer
38   */
39  @ConditionalOnClass(JaxbContextBuilder.class)
40  @AutoConfiguration
41  public class JaxbContextBuilderAutoConfiguration {
42  
43    private static final Log log = LogFactory.getLog(JaxbContextBuilderAutoConfiguration.class);
44  
45    /**
46     * Instantiates a new jaxb context builder autoconfiguration.
47     */
48    public JaxbContextBuilderAutoConfiguration() {
49      super();
50    }
51  
52    /**
53     * Init.
54     */
55    @EventListener(ApplicationReadyEvent.class)
56    public void init() {
57      log.info(String.format("""
58              
59              *********************************************************************************
60              * %s
61              *********************************************************************************""",
62          ClassUtils.getUserClass(getClass()).getSimpleName()));
63    }
64  
65    /**
66     * Creates jaxb context builder bean.
67     *
68     * @param configurers the configurers
69     * @return the jaxb context builder
70     */
71    @ConditionalOnMissingBean(JaxbContextBuilder.class)
72    @Bean
73    public JaxbContextBuilder jaxbContextBuilder(List<JaxbContextBuilderConfigurer> configurers) {
74      log.info(String.format("Creating bean %s with configurers %s",
75          JaxbContextBuilder.class.getSimpleName(), configurers));
76      JaxbContextBuilder jaxbContextBuilder = JaxbContextBuilder.newInstance()
77          .processAll(ServiceLoader.load(JaxbContextDataProvider.class));
78      Optional.ofNullable(configurers)
79          .ifPresent(configurerList -> configurerList
80              .forEach(configurer -> configurer.configure(jaxbContextBuilder)));
81      return jaxbContextBuilder;
82    }
83  
84  }