Program Tip

Spring 구성 디버깅

programtip 2020. 12. 5. 10:29
반응형

Spring 구성 디버깅


Spring과 Hibernate를 사용하고 Websphere에서 실행되는 Java 애플리케이션에서 작업 중입니다. Spring이 Dao를 내 개체에로드 할 것으로 예상하는 문제에 봉착했지만 어떤 이유로 인해 발생하지 않습니다. (거의 동일한 방식으로 지정된 또 다른 Dao는 정상적으로로드됩니다.)

질문은-Spring이 무엇을로드할지 결정하는 방법을 어떻게 디버깅 할 수 있습니까? Spring에 대한 로깅을 켤 수 있습니까?


예, Spring 프레임 워크 로깅은 매우 상세합니다. 이미 로깅 프레임 워크를 사용하고 있는지 여부는 게시물에서 언급하지 않았습니다. log4j를 사용하는 경우 log4j 구성 (즉, log4j.xml 또는 log4j.properties)에 스프링 어 펜더를 추가하기 만하면됩니다. log4j xml 구성을 사용하는 경우 다음과 같은 작업을 수행 할 수 있습니다.

<category name="org.springframework.beans">
    <priority value="debug" />
</category>

또는

<category name="org.springframework">
    <priority value="debug" />
</category>

JUnit 테스트를 사용하여이 문제를 격리하여 테스트하는 것이 좋습니다. Junit 과 함께 스프링 테스트 모듈 을 사용하면됩니다 . 스프링 테스트 모듈을 사용하는 경우 컨텍스트 구성을 기반으로 컨텍스트 파일을로드하고 컨테이너를 시작하므로 비즈니스 로직 테스트에만 집중할 수 있습니다. 여기에 작은 예가 있습니다.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:springContext.xml"})
@Transactional
public class SpringDAOTest 
{
    @Autowired
    private SpringDAO dao;

    @Autowired
    private ApplicationContext appContext;

    @Test
    public void checkConfig()
    {
        AnySpringBean bean =  appContext.getBean(AnySpringBean.class);
        Assert.assertNotNull(bean);
    }
}

최신 정보

로깅을로드하는 방법을 변경하라고 권하지는 않지만 개발 환경에서 시도해보십시오.이 코드를 web.xml 파일에 추가하십시오.

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

log4j 구성 파일 업데이트


나는 이것을 내 로컬 바람둥이에서 테스트했으며 응용 프로그램 시작시 많은 로깅을 생성했습니다. 나는 또한 수정하고 싶다 : @Rayan Stewart가 언급 한 것처럼 정보아닌 디버그를 사용하십시오 .

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Threshold" value="debug" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{HH:mm:ss} %p [%t]:%c{3}.%M()%L - %m%n" />
        </layout>
    </appender>

    <appender name="springAppender" class="org.apache.log4j.RollingFileAppender"> 
        <param name="file" value="C:/tomcatLogs/webApp/spring-details.log" /> 
        <param name="append" value="true" /> 
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{MM/dd/yyyy HH:mm:ss}  [%t]:%c{5}.%M()%L %m%n" />
        </layout>
    </appender>

    <category name="org.springframework">
        <priority value="debug" />
    </category>

    <category name="org.springframework.beans">
        <priority value="debug" />
    </category>

    <category name="org.springframework.security">
        <priority value="debug" />
    </category>

    <category
        name="org.springframework.beans.CachedIntrospectionResults">
        <priority value="debug" />
    </category>

    <category name="org.springframework.jdbc.core">
        <priority value="debug" />
    </category>

    <category name="org.springframework.transaction.support.TransactionSynchronizationManager">
        <priority value="debug" />
    </category>

    <root>
        <priority value="debug" />
        <appender-ref ref="springAppender" />
        <!-- <appender-ref ref="STDOUT"/>  -->
    </root>
</log4j:configuration>

If you use Spring Boot, you can also enable a “debug” mode by starting your application with a --debug flag.

java -jar myapp.jar --debug

You can also specify debug=true in your application.properties.

When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information. Enabling the debug mode does not configure your application to log all messages with DEBUG level.

Alternatively, you can enable a “trace” mode by starting your application with a --trace flag (or trace=true in your application.properties). Doing so enables trace logging for a selection of core loggers (embedded container, Hibernate schema generation, and the whole Spring portfolio).

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

참고URL : https://stackoverflow.com/questions/7840088/debugging-spring-configuration

반응형