问题描述
下面是我的类:
包com.abc.trade.util;公共类StockTraderLogger { 静态记录器记录= Logger.getLogger(StockTraderLogger); @Autowired
的ConfigService的ConfigService;
公共静态无效的调试(对象类名,对象的LogMessage){
尝试{
的System.out.println(调试..);
StockTraderLogger STL =新StockTraderLogger();
stl.addMessage(+ convertToString(类名)+\\ t+ convertToString(的LogMessage));
的System.out.println(在debug..post);
}赶上(DataAccessException的E){
的System.out.println(捕获异常...);
e.printStackTrace();
}
} 公共无效方法addMessage(字符串消息)抛出的DataAccessException {
的System.out.println(在UTIL添加消息。);
的System.out.println(字符串:+ ConfigService的); configService.addMessage(消息);
}
}
@Autowire
标注不工作。它显示的ConfigService
的值空时调用方法addMessage
方法。但它是正确的注入我的一些控制器类,但不是在这里。
谁能解释一下什么是问题呢?如何解决这个问题呢?
code为XML是:(beansdefinition.xml)
<?XML版本=1.0编码=UTF-8&GT?;
<豆的xmlns =http://www.springframework.org/schema/beans
XMLNS:XSI =http://www.w3.org/2001/XMLSchema-instance
的xmlns:AOP =http://www.springframework.org/schema/aop
的xmlns:TX =http://www.springframework.org/schema/tx
的xmlns:上下文=http://www.springframework.org/schema/context
XSI:的schemaLocation =
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd\"> <背景:组件扫描基包=com.abc.trade.util/>
<背景:组件扫描基包=com.abc.trade.service/> <! - Hibernate配置 - >
<豆的id =SessionFactory的阶级=org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean> <属性名=annotatedClasses>
<列表>
< VALUE> com.abc.trade.model.Order< /值>
< VALUE> com.abc.trade.model.Profile< /值>
< VALUE> com.abc.trade.model.Log< /值>
< /列表>
< /性>
< /豆> < TX:注解驱动/> <豆的id =transactionManager的
类=org.springframework.orm.hibernate3.HibernateTransactionManager>
<属性名=SessionFactory的REF =SessionFactory的/>
< /豆> <豆的id =commonService级=com.abc.trade.framework.service.CommonServiceImplementor>
<属性名=commonDaoREF =commonDao/>
< /豆> <豆的id =commonDao级=com.abc.trade.framework.dao.HibernateDAO>
<属性名=SessionFactory的><参考当地=SessionFactory的/>< /性> < /豆> <豆ID =的ConfigService级=com.abc.trade.service.ConfigServiceImplementor父=commonService>
< /豆> <进口来源=../背景/ springws-servlet.xml文件/>
< /豆>
另一个XML是:(用SpringMVC-servlet.xml中)
<?XML版本=1.0编码=UTF-8&GT?;
<豆的xmlns =http://www.springframework.org/schema/beans
XMLNS:XSI =http://www.w3.org/2001/XMLSchema-instance
的xmlns:Webflow的=http://www.springframework.org/schema/webflow-config
的xmlns:上下文=http://www.springframework.org/schema/context
XSI:的schemaLocation =
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd\"> <豆的id =ViewResolver的阶级=org.springframework.web.servlet.view.InternalResourceViewResolver>
<属性名=viewClass类VALUE =org.springframework.web.servlet.view.JstlView/>
<属性名=preFIXVALUE =/ JSP //>
<JSP属性名=后缀值= />
< /豆>
<背景:组件扫描基包=com.abc.trade.controller/>
<背景:组件扫描基包=com.abc.trade.util/>
<豆的id =为messageSource级=org.springframework.context.support.ResourceBundleMessageSource>
<属性名=基名价值=消息/>
< /豆> <! - 异常解析器 - >
<豆的id =exceptionResolver
类=org.springframework.web.servlet.handler.SimpleMappingExceptionResolver>
<属性名=exceptionMappings>
<道具及GT;
<支撑键=com.abc.trade.framework.exception.DataAccessException>
的errorPage< /道具>
<支撑键=java.sql.SQLException中的>的errorPage< /道具>
<支撑键=java.lang.Exception的>的errorPage< /道具>
< /道具>
< /性>
< /豆>< /豆>
感谢您提前。
的ConfigService
:
包com.abc.trade.service;
进口org.springframework.stereotype.Service;
进口com.abc.trade.framework.exception.DataAccessException;公共接口的ConfigService { 公共无效方法addMessage(字符串消息)抛出DataAccessException的;
}
配置服务实现:
包com.abc.trade.service;进口org.springframework.stereotype.Service;
进口org.springframework.transaction.annotation.Transactional;进口com.abc.trade.framework.exception.DataAccessException;
进口com.abc.trade.framework.service.CommonServiceImplementor;
进口com.abc.trade.model.Log;
进口com.abc.trade.model.Mode;
进口com.abc.trade.util.StockTraderLogger;@Service(ConfigService的)
公共类ConfigServiceImplementor扩展CommonServiceImplementor实现的ConfigService { 串的errorMessage =; @覆盖
公共无效方法addMessage(字符串消息){
的System.out.println(服务添加消息...........);
日志中记录=新的日志();
尝试{
log.setMessage(消息);
的System.out.println(消息是:+消息);
INT I =保存(日志);
}赶上(例外五)
{
的errorMessage =节约调试信息错误;
e.printStackTrace();
//抛出新的DataAccessException(的errorMessage);
} }}
StockTraderLogger未声明为春豆在Spring上下文不存在,因为这个原因注入将无法工作。
<豆的id =StockTraderLogger级=com.abc.trade.util.StockTraderLogger/>
或
@Component
公共类StockTraderLogger {/ ** /}
following is my class:
package com.abc.trade.util;
public class StockTraderLogger {
static Logger logger = Logger.getLogger("StockTraderLogger");
@Autowired
ConfigService configService;
public static void debug(Object className, Object logMessage) {
try {
System.out.println("in debug.. ");
StockTraderLogger stl =new StockTraderLogger();
stl.addMessage(""+convertToString(className)+"\t"+convertToString(logMessage));
System.out.println("in debug..post ");
} catch (DataAccessException e) {
System.out.println("Caught exception...");
e.printStackTrace();
}
}
public void addMessage(String message) throws DataAccessException {
System.out.println("in add message of util. ");
System.out.println("String: " + configService);
configService.addMessage(message);
}
}
@Autowire
annotation is not working. It is displaying value of configService
as null when called addMessage
method. however it is properly injected in some of my Controller classes but not here.
Can anyone explain what is problem? and how to resolve this issue?
Code for XML is:(beansdefinition.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.abc.trade.util"/>
<context:component-scan base-package="com.abc.trade.service"/>
<!-- Hibernate Configuration -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>com.abc.trade.model.Order</value>
<value>com.abc.trade.model.Profile</value>
<value>com.abc.trade.model.Log</value>
</list>
</property>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="commonService" class="com.abc.trade.framework.service.CommonServiceImplementor">
<property name="commonDao" ref="commonDao"/>
</bean>
<bean id="commonDao" class="com.abc.trade.framework.dao.HibernateDAO">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<bean id="configService" class="com.abc.trade.service.ConfigServiceImplementor" parent="commonService">
</bean>
<import resource="../context/springws-servlet.xml"/>
</beans>
Another XML is:(Springmvc-servlet.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:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<context:component-scan base-package="com.abc.trade.controller" />
<context:component-scan base-package="com.abc.trade.util"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
<!-- Exception Resolver -->
<bean id="exceptionResolver"
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="com.abc.trade.framework.exception.DataAccessException">
errorPage</prop>
<prop key="java.sql.SQLException">errorPage</prop>
<prop key="java.lang.Exception">errorPage</prop>
</props>
</property>
</bean>
</beans>
Thank you in advance.
ConfigService
:
package com.abc.trade.service;
import org.springframework.stereotype.Service;
import com.abc.trade.framework.exception.DataAccessException;
public interface ConfigService {
public void addMessage(String message) throws DataAccessException;
}
Config Service Implementor:
package com.abc.trade.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.abc.trade.framework.exception.DataAccessException;
import com.abc.trade.framework.service.CommonServiceImplementor;
import com.abc.trade.model.Log;
import com.abc.trade.model.Mode;
import com.abc.trade.util.StockTraderLogger;
@Service("configService")
public class ConfigServiceImplementor extends CommonServiceImplementor implements ConfigService{
String errorMessage = "";
@Override
public void addMessage(String message) {
System.out.println("in add message of service...........");
Log log = new Log();
try{
log.setMessage(message);
System.out.println("Message is: "+message);
int i=save(log);
}catch(Exception e)
{
errorMessage = "Error in saving debug message";
e.printStackTrace();
//throw new DataAccessException(errorMessage);
}
}
}
StockTraderLogger is not declared as spring bean and doesn't exist in spring context and for that reason the injection won't work.
<bean id="StockTraderLogger" class="com.abc.trade.util.StockTraderLogger"/>
or
@Component
public class StockTraderLogger { /**/ }
这篇关于@Autowired注解问题,而不是在注射级bean,使用Spring3.0,休眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!