【问题标题】:Spring boot beans orderSpring Boot bean 顺序
【发布时间】:2016-07-05 08:24:02
【问题描述】:

我正在尝试使用 jetty EmbeddedContainerServlet 和 jndi 数据源配置 Spring Boot 项目,代码如下

@Configuration
public class EmbeddedJettyServer {

  @Value("${jetty.http.port:8080}")
  private Integer port;
  @Value("${jetty.threadPool.maxThreads:200}")
  private String maxThreads;
  @Value("${jetty.threadPool.minThreads:8}")
  private String minThreads;
  @Value("${jetty.threadPool.idleTimeout:60000}")
  private Integer idleTimeout;

private JettyServerCustomizer jettyServerCustomizer() {
    return new JettyServerCustomizer() {

        @Override
        public void customize(Server server) {
            try {
                // Tweak the connection pool used by Jetty to handle
                // incoming HTTP connections
                final QueuedThreadPool threadPool = new QueuedThreadPool();
                threadPool.setMaxThreads(Integer.valueOf(maxThreads));
                threadPool.setMinThreads(Integer.valueOf(minThreads));
                server.getBeans().add(threadPool);
                WebAppContext webAppContext = (WebAppContext) server.getHandler();
                createConfiguration(
                    "/Users/kewnen/git/zeus-info-provider/zeus-info-provider-web/ops/resources/jetty-datasource.xml")
                        .configure(webAppContext);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        private XmlConfiguration createConfiguration(String xml) throws IOException, SAXException {
            return new XmlConfiguration(new FileInputStream(xml));
        }
    };
}

@Bean
public EmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory() {
    final JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory = new JettyEmbeddedServletContainerFactory() {
        @Override
        protected JettyEmbeddedServletContainer getJettyEmbeddedServletContainer(Server server) {
            return new JettyEmbeddedServletContainer(server);
        }
    };
    jettyEmbeddedServletContainerFactory.addServerCustomizers(jettyServerCustomizer());
    jettyEmbeddedServletContainerFactory.setPort(port);
    jettyEmbeddedServletContainerFactory.setSessionTimeout(idleTimeout);
    return jettyEmbeddedServletContainerFactory;
}

}

我定义数据源的 jetty-datasource.xml 文件的内容

  <?xml version="1.0"?>
   <Configure class="org.eclipse.jetty.webapp.WebAppContext">
     <Set name="contextPath">/</Set>

      <New id="cf" class="org.eclipse.jetty.plus.jndi.Resource">
         <Arg>jdbc/zeus-info</Arg>
         <Arg>
             <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="driverClassName">org.postgresql.Driver</Set>
            <Set name="url">jdbc:postgresql://localhost:5433/myDb</Set>
            <Set name="username">postgres</Set>
            <Set name="password">password</Set>
            <Set name="validationQuery">SELECT 1</Set>
        </New>
    </Arg>
</New>

然后我用下面的代码定义一个数据源

@Bean
public DataSource dataSource() {
    JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    return dsLookup.getDataSource("jdbc/zeus-info");
}

当我运行它时,我得到了这个异常

  Caused by: javax.naming.NameNotFoundException; remaining name 'jdbc/zeus-info'
at     org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:490)
at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:536)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:155)
at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152)
at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:179)
at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:104)
at org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup.getDataSource(JndiDataSourceLookup.java:45)

当调试时,我清楚地看到 Datasource bean 在定义我的 jndi 数据源的 JettyEmbeddedServletContainerFactory 定义之前实例化,

我尝试通过在数据源定义中添加@DependsOn 注释来强制排序,但不起作用,它总是在容器之前实例化 DataSource bean,

编辑:

提供更多细节,

我已经通过在不使用 jndi 的情况下定义数据源来测试它并且工作正常:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(DRIVER_CLASS_NAME);
    dataSource.setUrl(URL);
    dataSource.setUsername(USER);
    dataSource.setPassword(PASS);
    return dataSource;
}

但由于它是从使用 jndi 定义的数据源的经典战争迁移到 spring boot jar 打包项目,我想从现有项目中复制相同的内容,因为我不知道将 jndi 方式更改为这个项目的影响。

我试图通过在数据源定义中添加这一行来强制在容器之后实例化数据源

 @DependsOn("jettyEmbeddedServletContainerFactory")

但总是在码头容器之前实例化数据源

修复:

问题是spring-boot会自动配置一个名为jettyEmbeddedServletContainerFactory的容器,这就是为什么 @DependsOn("jettyEmbeddedServletContainerFactory") 没用,

我加了

@EnableAutoConfiguration(exclude = EmbeddedServletContainerAutoConfiguration.class)

到我的应用程序,现在它可以工作了

感谢您的帮助!

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您可以使用 Spring Boot 属性来初始化数据源 bean 而不是 JNDI:

    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.password=password
    spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
    spring.datasource.url=jdbc:postgresql://localhost:5433/myDb
    spring.datasource.username=postgres
    spring.datasource.validation-query=SELECT 1
    

    评论反应:

    所以你的开发环境应该尽可能接近 Prod。因此我不会使用嵌入式容器,而是使用独立容器进行开发。对于 CI 构建,您可以使用 maven-jetty-plugin 或Gradle Jetty plugin

    【讨论】:

    • 感谢您的回答,我编辑我的评论以添加更多详细信息,这个想法是使用我们生产环境中使用的码头 xml 文件在我的嵌入式容器中重现相同的内容,这就是我尝试用 jndi 实现
    • 为什么不推荐使用嵌入式容器,有什么原因吗?
    • 因为您在生产中使用 WAR 部署。您希望在尽可能接近生产环境的环境中进行开发。
    • 我发现为什么它之前实例化了数据源,但没有解决我的问题...我编辑我的评论
    • 需要将war打包改为spring boot的jar打包,采用独立微服务的方式,所以我们会给每个微服务对应的exploit团队jar,但我应该复制相同生产环境中的码头配置
    猜你喜欢
    • 2019-12-25
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2017-04-23
    相关资源
    最近更新 更多