GVKun编程网logo

我将XML bean放在Spring Boot应用程序的哪里?(java xml bean)

7

在本文中,我们将带你了解我将XMLbean放在SpringBoot应用程序的哪里?在这篇文章中,我们将为您详细介绍我将XMLbean放在SpringBoot应用程序的哪里?的方方面面,并解答javax

在本文中,我们将带你了解我将XML bean放在Spring Boot应用程序的哪里?在这篇文章中,我们将为您详细介绍我将XML bean放在Spring Boot应用程序的哪里?的方方面面,并解答java xml bean常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator、java – 确保SpringBoot应用程序的单个实例、Spring Boot应用程序的外部配置、Spring Boot应用程序的默认日志文件

本文目录一览:

我将XML bean放在Spring Boot应用程序的哪里?(java xml bean)

我将XML bean放在Spring Boot应用程序的哪里?(java xml bean)

我要回到Spring(目前是v4)。@SpringBootApplication和其他注释现在都很棒,但是所有文档似乎都忘了提到我如何用XML定义其他bean!

例如,我想创建一个“ SFTP会话工厂”,其定义如下:http : //docs.spring.io/spring-
integration/reference/html/sftp.html

有很多XML可以定义bean,但是我到底将它放在哪里以及如何链接它呢?以前我做过:

ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:applicationContext.xml");

指定文件名和位置,但是现在我要使用:

ApplicationContext ctx = SpringApplication.run(Application.class);

我将XML文件放在哪里?有一个神奇的弹簧名字可以称呼它吗?

java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator

具有生产信息端点的 Spring Boot的Actuator库对任何服务器应用程序都非常有用.但问题是我找不到集成到传统Spring应用程序(不是Spring BOOT应用程序)的方法.

必须有一些方法来使用执行器的端点,但我无法将它们连接起来.

我有一个JavaConfig类,如下所示

@Configuration
@ComponentScan(basePackages = { "com.company.helper","org.springframework.boot" })
@EnableWebMvc
@Import({ DbConfig.class })

public class AppConfig extends WebMvcConfigurerAdapter {

}

但是此配置在部署期间会引发错误.

没有Spring Boot应用程序可以完成这种连线吗?

解决方法

我在此博客文章中添加了有关如何在非启动应用程序中添加spring boot执行器的信息

http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html

在应用程序的build.gradle中,我添加了以下依赖项

compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
    exclude group: 'org.springframework.boot',module:'spring-boot-starter-logging'}

在应用程序的Spring Config类中,我添加了以下内容:

import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;  
 import org.springframework.boot.actuate.endpoint.BeansEndpoint;  
 import org.springframework.boot.actuate.endpoint.HealthEndpoint;  
 import org.springframework.boot.actuate.endpoint.InfoEndpoint;  
 import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;  
 import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;  
 import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;  
 import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;  
 import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;  

 @Configuration  
 @Import(EndpointAutoConfiguration.class)  
 public class MyAppSpringConfig {  

   @Bean  
   @Autowired  
   //Define the HandlerMapping similar to RequestHandlerMapping to expose the endpoint  
   public EndpointHandlerMapping endpointHandlerMapping(  
     Collection<? extends MvcEndpoint> endpoints  
   ){  
     return new EndpointHandlerMapping(endpoints);  
   }  

   @Bean  
   @Autowired  
   //define the HealthPoint endpoint  
   public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate){  
     return new HealthMvcEndpoint(delegate,false);  
   }  

   @Bean  
   @Autowired  
   //define the Info endpoint  
   public EndpointMvcAdapter infoMvcEndPoint(InfoEndpoint delegate){  
      return new EndpointMvcAdapter(delegate);  
   }  

   @Bean  
   @Autowired  
   //define the beans endpoint  
   public EndpointMvcAdapter beansEndPoint(BeansEndpoint delegate){  
     return new EndpointMvcAdapter(delegate);  
   }  

   @Bean  
   @Autowired  
   //define the mappings endpoint  
   public EndpointMvcAdapter requestMappingEndPoint(  
     RequestMappingEndpoint delegate  
   ){  
     return new EndpointMvcAdapter(delegate);  
  }  
}

如果你想摆脱一个额外的依赖,那么请参阅博客帖子.

UPDATE

此外,您需要确保为RequestMappingHandlerAdapter定义了一个bean,如果您没有它,Servletdispatcher将无法为您的HealthMvcEndpoint的处理程序获取适配器.

如果你没有它只是将它添加到你的bean配置文件

xml配置:

<bean>
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter"/>
            </list>
        </property>
    </bean>

    <bean id="jsonConverter">
        <property name="supportedMediaTypes" value="application/json" />
        <property name="prettyPrint" value="true" />
    </bean>

java – 确保SpringBoot应用程序的单个实例

java – 确保SpringBoot应用程序的单个实例

我怎样才能确保只运行我的 SpringBoot应用程序的单个实例.

我正在启动我的春季启动应用程序
java -jar target / myproject-0.0.1-SNAPSHOT.jar

我需要确保如果有人在同一个JVM上第二次运行此命令,我的应用程序不应该启动.
一种方法是在应用程序启动时创建一个锁文件,并在应用程序关闭时删除锁文件.但这需要大量的锅炉板代码.
有没有更好的方法来做这个Spring Boot?

解决方法

控制应用程序的实例数(可以在不同的计算机上运行).我使用过这些技术.

>要跟踪的数据库表:例如,可以记录IP,实例名称等的MY_APP_STATUS表.如果相同的IP,相同的实例名称已经运行,应用程序将关闭自身.

我假设您的应用已经进行了一些数据访问,在这种情况下,它是spring-data中的单行方法.

Spring Boot应用程序的外部配置

Spring Boot应用程序的外部配置

我有一个要使用外部配置文件运行的spring-
boot应用程序。当我将其作为jar(带有嵌入式servlet容器)运行时,一切都很好。但是我想在外部servlet容器(Tomcat)下运行它,在这里我对外部配置有问题。我尝试了@PropertySource,但是在这种情况下,应用程序仅获得war文件配置中不存在的属性:外部配置不会覆盖内部配置。那么问题来了:如何配置将覆盖内部配置的外部配置?

答案1

小编典典

application.properties当您以jar
形式运行应用程序时,您可能正在当前目录中以形式使用外部配置。但是,在外部雄猫中以战争方式部署时,“当前目录”不是很有用。即使您知道当前目录是什么,在该tomcat中运行的所有应用程序的位置也很可能在同一位置,因此,当您运行多个应用程序时,这将无法很好地工作。

我们在这里所做的是PropertySources在我们的应用程序上声明两个:

@PropertySources({@PropertySource(value={"classpath:internal.properties"}), @PropertySource(value={"file:${application.properties}"})})

internal.properties包含属性的“内置”默认值。第二个PropertySource是包含外部配置的文件。请注意,文件名本身就是一个属性。

我们在Context应用程序的元素中(tomcat中)在外部定义:

<Context docBase="/path/to/your/war/your.war">    <Parameter name="application.properties" value="/path/to/your/properties/application.properties"/></Context>

这使您可以在tomcat中运行多个应用程序,每个应用程序都使用其自己的外部属性文件。您甚至可以使 同一 应用程序的多个实例以不同的属性运行。

Spring Boot应用程序的默认日志文件

Spring Boot应用程序的默认日志文件

我已经在spring.boot应用程序的application.yml中将日志记录级别设置为:logging.level.com.Myapplicationname
= DEBUG

该应用程序打包并部署为tomcat上的war。除此之外,我还没有设置logback.xml来定义日志文件等。请告诉我,当某些用户通过浏览器使用应用程序时,在哪里可以看到控制台日志。框架是否创建了任何默认文件。

关于我将XML bean放在Spring Boot应用程序的哪里?java xml bean的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于java – 在没有Spring Boot应用程序的情况下使用Spring Boot Actuator、java – 确保SpringBoot应用程序的单个实例、Spring Boot应用程序的外部配置、Spring Boot应用程序的默认日志文件等相关内容,可以在本站寻找。

本文标签: