【问题标题】:spring - use beans from config.xml in SpringConfig.classspring - 在 SpringConfig.class 中使用 config.xml 中的 beans
【发布时间】:2017-04-24 15:40:16
【问题描述】:

我有一些从部署到部署的上下文变化,有些对于所有部署都是不变的。与部署相关的位位于 jar 或 war 外部的 config.xml 文件中。静态上下文内容位于注释配置类中。诀窍是注释配置使用来自 config.xml 的 bean。对于应用程序,它被导入到配置类中。但是对于 webapp,它找不到那个文件。

SpringConfig 类

@Component
@Configuration
@ImportResource("file:./config.xml")
public class SpringConfig {

   @Autowired private String local_timezone_string;
   @Autowired private String startDateFormatString;
   @Autowired private String startDateString;

   @Bean
   public TimeZone localTimeZone() {
      return TimeZone.getTimeZone(local_timezone_string);
   }

   @Bean
   public SimpleDateFormat startDateFormat() {
      SimpleDateFormat sdf = new SimpleDateFormat(startDateFormatString);
      sdf.setTimeZone(localTimeZone());
      return sdf;
   }

   @Bean
   public long jaceThreadStartDate() throws ParseException {
      return startDateFormat().parse(jaceThreadStartDateString).getTime();
   }

   ... a bunch of other beans
}

和 config.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd">

   <bean id="local_timezone_string" class="java.lang.String"><constructor-arg value="Canada/Atlantic"/></bean>
   <bean id="startDateFormatString" class="java.lang.String"><constructor-arg value="yyyy-MM-dd"/></bean>
   <bean id="startDateString" class="java.lang.String"><constructor-arg value="2017-04-06"/></bean>

   ... other beans

</beans>

config.xml 不包含在 jar 中,因此 SpringConfig 在部署文件夹中查找 config.xml 并使用特定于部署的上下文。但是当通过 WebApplicationInitializer 在 webapp 中部署 SpringConfig 时 - 它再也找不到 config.xml。当然。那么,如何将依赖于部署的上下文中的 bean 加载到 webapp 中的静态上下文中呢?有没有办法告诉 AnnotationConfigApplicationContext 扫描包以查找 @Configuration 和 @Components 从 webapp 中的 xml 配置注册 bean?

我在想可能是从 AnnotationConfigWebApplicationContext 派生的类 - 在构造函数中 - 调用 setParent(config.xml context) 然后调用 AnnotationConfigWebApplicationContext(SpringConfig.class)。

甚至:不是调用 AnnotationConfigApplicationContext(String... basePackages),而是调用 AnnotationConfigApplicationContext(),然后是 setParent(...),然后是 scan(...)。

【问题讨论】:

    标签: xml spring web-applications import spring-annotations


    【解决方案1】:

    所以有一种方法可以使用 setParent() 和 scan()。

    @Component
    @Configuration
    public class SpringConfig {
    
        private static final Logger log = Logger.getLogger(SpringConfig.class);
        @Autowired private DriverManagerDataSource pgDataSource;
        @Autowired private String local_timezone_string;
        @Autowired private String startDateFormatString;
        @Autowired private String startDateString;
    
        private static ApplicationContext getVariableContext() throws Exception {
    
            ApplicationContext variableContext = null;
            try {
                variableContext = new FileSystemXmlApplicationContext("./config.xml");
                log.info("got config.xml from file system");
            } catch (Exception e) {
                try {
                    variableContext = new ClassPathXmlApplicationContext("config.xml");
                    log.info("got config.xml from class path");
                } catch (Exception ex) {
                    log.info("failed to get config.xml");
                    throw new Exception(...);
                }
            }
            return variableContext;
        }
    
        public static WebApplicationContext getWebApplicationContext() throws ServletException {
    
            try {
                ApplicationContext variableContext = getVariableContext();
                AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
                appContext.setParent(variableContext);
                appContext.scan(
                        "com.us.systemdiagnostics",
                        "com.us.systemdiagnostics.webservice"
                );
                appContext.refresh();
                return appContext;
    
            } catch (Exception e) {
                throw new ServletException(...);
            }
        }
    
        public static ApplicationContext getStandAloneApplicationContext() throws ServletException {
    
            try {
                ApplicationContext variableContext = getVariableContext();
                AnnotationConfigApplicationContext appContext = new AnnotationConfigApplicationContext();
                appContext.setParent(variableContext);
                appContext.scan(
                        "com.us.systemdiagnostics",
                        "com.us.systemdiagnostics.webservice"
                );
                appContext.refresh();
                return appContext;
    
            } catch (Exception e) {
                throw new Exception(...);
            }
        }
    
        ... beans, etc ...
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 2012-10-29
      • 2018-01-06
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多