如何将记录器注入示例Spring Boot应用程序的字段中? [英] How do I inject a logger into a field in the sample spring boot application?

查看:56
本文介绍了如何将记录器注入示例Spring Boot应用程序的字段中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是使spring自动接线成为记录器.因此,换句话说,我想让这个工作正常:

What I want is to make spring autowire a logger. So, in other words, I want to have this working:

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MainController {

    @Autowired
    private Logger logger;
    
    @RequestMapping("/")
    public String enterSite(HttpServletResponse response) {
        logger.info("site entered");
        return "welcome";
    }
}

现在,它在启动时抛出异常:未找到类型为[org.slf4j.Logger]的合​​格Bean ...".

Right now it throws an exception at startup: "No qualifying bean of type [org.slf4j.Logger] found for dependency...".

我的pom.xml依赖项:

My pom.xml dependencies:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.0.M1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.1-901.jdbc4</version>
        </dependency>
        <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> 
            </dependency> -->
    </dependencies>

我阅读了以下内容: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-logging

它说如果您使用启动程序poms之一(我愿意),则使用Logback-但用于内部日志记录.可以在我的课堂上自动接线吗?

It says if you use one of the starter poms (I do) Logback is used - but for internal logging. Can it be autowired in my classes?

推荐答案

尽管这不是通常的方法,但是您可以在上下文中直接添加记录器bean来复制经典绑定:

Although it is not the usual way you can add a logger bean directly in your context reproducing the classic bind:

private final Logger logger = LoggerFactory.getLogger(MainController.class);

只需在spring上下文中插入:

simply inserting in the spring context:

<bean id="logger" scope="prototype" class="org.slf4j.LoggerFactory" factory-method="getLogger">
    <constructor-arg name="name" value="youLoggerName" />
</bean>

然后您只需注入记录器即可:

then you can simply inject your logger:

@Autowired
private Logger logger;

这篇关于如何将记录器注入示例Spring Boot应用程序的字段中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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