2014-01-29 49 views
1

我想自動裝配一些豆子一起注射失敗(我已經創建了我想要實現簡化的版本),但我一直斥罵的自動裝配Autowired豆爲空。彈簧自動裝配爲我的註解豆

Caused by: java.lang.NullPointerException 
    at bean.HelloWorld.getMessage(HelloWorld.java:15) 

TestDAO.java

package dao; 
import org.springframework.context.annotation.Primary; 
import org.springframework.stereotype.Repository; 
@Primary 
@Repository("testDAO") 
public class TestDAO { 
    public String getMessage() { 
     return "Hello World!"; 
    } 
} 

TestSOA.java

package soa; 
import dao.TestDAO; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
@Service("testSOA") 
public class TestSOA { 
    @Autowired 
    private TestDAO testDAO; 
    public String getMessage() { 
     return testDAO.getMessage(); 
    } 
} 

HelloWorld.java

package bean; 
import java.io.Serializable; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import soa.TestSOA; 
@ManagedBean(name = "helloWorld") 
@SessionScoped 
@Controller 
public class HelloWorld implements Serializable { 
    @Autowired 
    private TestSOA testSOA; 
    public String getMessage() { 
     return testSOA.getMessage(); 
    } 
} 

的applicationContext.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:p="http://www.springframework.org/schema/p" 
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.1.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:component-scan base-package="dao, soa, bean"/> 

</beans> 

然後在的index.xhtml

Message:<h:outputText value="#{helloWorld.message}"/> 

我不明白,我想我所做的一切,我猜想。 web.xml中已申報的ContextLoaderListener。我試過使用CDI和其他Spring方法,並手動接線applicationContext.xml。 什麼都沒有...

我的猜測是我沒有得到一些有關Spring或注入的基本原理。 我使用Java 1.6,春季3.1.1和Tomcat 7.0.34.0

回答

0

嘗試增加<context:annotation-config />applicationContext.xml<context:component-scan>前啓用基於註解Spring配置。見春參考Annotation-based container configuration部分。

+0

不,沒有效果,它仍然沒有注入(或它,但它插入空?)。儘管如此,謝謝你的建議。 – Huffy