简体   繁体   English

Spring可以初始化“非bean”字段吗?

[英]Can Spring initialize “non-bean” fields?

I have initialized a java.util.logging.Logger bean and now want to add a Handler which I've also created a bean for. 我已经初始化java.util.logging.Logger绿豆,现在想添加一个Handler ,我还创建了一个bean的。 The trouble is the Logger method is called addHandler instead of something like setHandler . 麻烦的是Logger方法被调用addHandler ,而不是像setHandler

How can I inject the handler into the logger? 如何将处理程序注入记录器?

Do I need to wrap Logger in a Spring-friendly bean class? 我是否需要将Logger包装在对Spring友好的bean类中?

EDIT 编辑

If someone can also tell me how to pass in the logging level (which is a static value) I'd be much obliged. 如果有人还可以告诉我如何传递日志记录级别(这是一个静态值),我将非常有义务。

UPDATE: Changed to use java.util.logging, and show how to set level. 更新:更改为使用java.util.logging,并显示如何设置级别。

You can use this by having a FactoryBean implementation: 您可以通过具有FactoryBean实现来使用它:

package foo.bar;

import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Logger;

import org.springframework.beans.factory.FactoryBean;

public class LoggerFactoryBean implements FactoryBean {

    private final Logger logger;

    public LoggerFactoryBean(java.util.logging.Logger logger,
            List<Handler> handlers) {
        this.logger = logger;
        for (Handler handler : handlers) {
            logger.addHandler(handler);
        }
    }

    @Override
    public Object getObject() throws Exception {
        return logger;
    }

    @Override
    public Class getObjectType() {
        return Logger.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

Then define your bean in XML like: 然后像下面这样用XML定义bean:

  <!-- returns logger with handlers -->
  <bean id="logger" class="foo.bar.LoggerFactoryBean">
    <constructor-arg>
      <!-- raw logger without handlers -->
      <bean class="java.util.logging.Logger" factory-method="getAnonymousLogger">
        <property name="level">
          <value>SEVERE</value>
        </property>
      </bean>
    </constructor-arg>
    <constructor-arg>
      <util:list>
        <bean class="java.util.logging.FileHandler" />
      </util:list>
    </constructor-arg>
  </bean>

Example unit test: 单元测试示例:

package foo.bar;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations = { "classpath:/foo/bar/test-config.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class HandlerTest {

    @Autowired
    private Logger logger;

    @Test
    public void testLogger() {
        assertNotNull(logger);
        assertEquals(Level.SEVERE, logger.getLevel());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM