Spring 3.1.0中使用DI注入记录器对象时的空指针
我打算将logger(log4J和slf4j)与springdi一起使用 我试图使用DI初始化主记录器类,并在测试类中自动连接该实例。 但那个实例总是空的。我不确定我做错了什么。 请注意,它是部署在IBM WAS 6.1上的web应用程序 这就是我正在做的 所有类都在com.test包中Spring 3.1.0中使用DI注入记录器对象时的空指针,spring,web-applications,dependency-injection,log4j,slf4j,Spring,Web Applications,Dependency Injection,Log4j,Slf4j,我打算将logger(log4J和slf4j)与springdi一起使用 我试图使用DI初始化主记录器类,并在测试类中自动连接该实例。 但那个实例总是空的。我不确定我做错了什么。 请注意,它是部署在IBM WAS 6.1上的web应用程序 这就是我正在做的 所有类都在com.test包中 @Repository public class TestLogger { @Autowired MainLogg
@Repository
public class TestLogger
{
@Autowired
MainLogger error; // this instance of Mainlogger is always null
//if **MainLogger error = new MainLogger();** is used this is working fine!!
public void test(){
error.logerror("test"); //null pointer here
}
}
主记录器类
public class MainLogger {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(MainLogger.class);
String str = "Logger check check";
logger.info(str);
}
public void loginfo(String para)
{
Logger logger = LoggerFactory.getLogger(VpayCoreLogger.class);
logger.info(para);
}
public void logdebug(String para)
{
Logger logger = LoggerFactory.getLogger(VpayCoreLogger.class);
logger.debug(para);
}
public void logerror(String para)
{
Logger logger = LoggerFactory.getLogger(VpayCoreLogger.class);
logger.error(para);
}
}
}
应用程序上下文XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean id="error" class="com.test.MainLogger">
</bean>
<context:component-scan base-package="com.test"/>
</beans>
WEB XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>testApp</display-name>
<welcome-file-list>
<welcome-file>testDAO.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
特斯塔普
testDAO.jsp
上下文配置位置
/WEB-INF/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
使用@组件对TestLogger
进行注释,使其成为bean。否则,它将不会被元件扫描拾取,从而使其不符合自动布线/DI的资格
@Component
public class TestLogger
{
//code goes here
}
嗯,我不确定这是否是答案,但我还是设法找到了答案。
问题是使用setter或构造函数自动连接字段
@Repository
public class TestLogger
{
private static MainLogger error;
@Autowired
public TestLogger(MainLogger error)
{
this.error=error;
}
public TestLogger()
{
}
public void test(){
error.logerror("test");
}
}
它仍然是空的,我忘了加上我已经在testlogger上使用了@Repository,你在web.xml中引导应用程序上下文了吗?是的,我正在用web xml加载应用程序上下文。更新了webxml的代码。您使用哪种方法进行测试?在哪个类中?如果使用构造函数初始化测试类DI将失败。类工具本身必须是DI容器的一部分。好吧,我只是使用一个JSP来调用TestLogger类。新建TestLogger().test()。我做错了什么?我想,当我的服务器发布web应用程序时,它会加载并注入所有bean?如果不是,这实际上是如何工作的。或者如何使用DI测试我的记录器?