在Spring MVC webapp中添加系统属性 [英] Adding system properties in Spring MVC webapp

查看:409
本文介绍了在Spring MVC webapp中添加系统属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Spring MVC webapp中使用Hibernate 4.3.7.Final和Log4j2,通过Tomcat 7发布。所有配置都通过JavaConfig完成(即没有web.xml或其他XML配置文件)。



默认情况下,由于 Apache wiki 。为了解决这个问题,我需要创建一个系统设置,如下所示:

  System.setProperty(org.jboss.logging。提供者,slf4j); 

由于我的应用程序是一个web应用程序,因此没有主线程,因此我不确定在哪里把这个 System.setProperty 调用。任何建议将不胜感激。

解决方案

您可以在上下文侦听器中定义此系统属性,它是第一个入口点,如下所示: / p>

  @WebListener 
public class ContextListenerExample实现ServletContextListener {
public void contextInitialized(ServletContextEvent e){
System.setProperty(org.jboss.logging.provider,slf4j);




$ b你甚至可以使用spring定义系统属性如下:

 < bean id =setupJBossLoggingProperty
class =org.springframework.batch.support.SystemPropertyInitializer
p:keyName =org.jboss.logging.providerp:defaultValue =slf4j/>

然后你可以这样说:

 < bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
depends-on =setupJBossLoggingProperty>
...

所以这意味着系统属性会先设置,然后hibernate bean是将被初始化。


I am using Hibernate 4.3.7.Final and Log4j2 in my Spring MVC webapp, published via Tomcat 7. All configuration is done via JavaConfig (i.e. there is no web.xml or other XML config files).

By default the Hibernate logging does not go through Log4j, for reasons explained in the Apache wiki. In order to resolve this I need to create a system setting as follows:

System.setProperty("org.jboss.logging.provider", "slf4j");

As my application is a webapp there is no Main thread, and as a result I an unsure where to put this System.setProperty call. Any advice would be appreciated.

解决方案

You could define this system property in context listener which is the first entry point as below:

@WebListener
public class ContextListenerExample implements ServletContextListener {
    public void contextInitialized(ServletContextEvent e){
        System.setProperty("org.jboss.logging.provider", "slf4j");
    }
}

You could even define system property using spring as below:

<bean id="setupJBossLoggingProperty"
    class="org.springframework.batch.support.SystemPropertyInitializer"
       p:keyName="org.jboss.logging.provider" p:defaultValue="slf4j"/>

And then you could say something like:

<bean id="entityManagerFactory"
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
     depends-on="setupJBossLoggingProperty">
 ...

So this means system property will be setted first and then hibernate bean is going to be initialised.

这篇关于在Spring MVC webapp中添加系统属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆