2011-05-10 43 views
6

以下是我的课:@Autowired注解问题,而不是注射类豆,使用Spring3.0,休眠

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注释不工作。当调用addMessage方法时,它显示configService的值为null。但是它在我的一些Controller类中正确注入,但不在这里。

任何人都可以解释什么是问题吗?以及如何解决这个问题?

代码XML是:(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> 

另一个XML是:(用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> 

预先感谢您。

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; 
} 

配置服务实现:

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); 
     } 

    } 

} 
+0

什么是ConfigService类的完整包类名称?我认为默认注射是按类型,而不是按名称。 – DwB 2011-05-10 13:35:21

+0

我在控制器中自动装配了这个bean,它工作正常,但如果我在其他类中自动装配,那么它不会自动装配。 – Sagar 2011-05-10 13:42:42

+0

你是否碰巧解决了问题sagar? – 2012-04-27 15:34:54

回答

1

我认为你缺少

<context:annotation-config /> 

另外,还要确保您的ConfigService类有

@Service("configService") 

注释,它会使这个类的候选人自动装配。

和原因,你应该使用

<context:component-scan base-package="package" /> 

对ConfigService的包名。

+0

我试了两次,但它仍然显示null作为输出。 – Sagar 2011-05-10 14:05:07

+0

组件扫描存在于我的XML中,您可以看到在我自己共享的XML代码中。我需要改变它吗? – Sagar 2011-05-10 14:08:16

+0

你需要改变或添加一个新的引用configService包 – 2011-05-10 14:10:57

8

StockTraderLogger没有被声明为spring bean,并且在spring上下文中不存在,因此注入将不起作用。

<bean id="StockTraderLogger" class="com.abc.trade.util.StockTraderLogger"/> 

@Component 
public class StockTraderLogger { /**/ } 
+0

两者都无法正常工作 – Sagar 2011-05-11 10:25:20

2

一下添加到applicationContext.xml

xmlns:mvc="http://www.springframework.org/schema/mvc"

http://www.springframework.org/schema/mvcxsi:schemalocation

MVC-注释需要注释的控制器和其他特征从动:

<mvc:annotation-driven />

+0

我也在我的应用程序中尝试过这种方式,而不是这是一个严重的问题。尽可能地关闭它。 – Akash5288 2014-04-18 07:52:55

3

这里的问题是在调试方法:

StockTraderLogger stl =new StockTraderLogger(); 

这不是Spring管理。您可以通过两种方式将Spring托管bean注入非托管bean。 这里您可以在StockTraderLogger注入的ConfigService为:

1)通过AutowireCapableBeanFactory:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beansdefinition.xml"); 
    StockTraderLogger stl = new StockTraderLogger(); 
    ctx.getAutowireCapableBeanFactory().autowireBeanProperties(stl, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); 

2)通过使用弹簧AOP @Configurable注解这标志着一类可以通过Spring驱动的配置(如实例化的对象与'新'运营商)。

@Configurable 
    public class StockTraderLogger { 
    ... 
    } 

and specifying this <context:spring-configured/> in beansdefinition.xml. 

你可以找到更多关于这个春天aop方式here的信息。