2016-04-06 7 views
0

私は、このようなサービスクラスを持っている:なぜ私のSpring @Autowiredフィールドは非コントローラのクラスの内側にありますか?

@Service 
public class ReminderServiceImpl implements ReminderService { 

@Autowired 
private RemindRepository repository; 

public Remind save(Remind remind) { 
    return repository.saveAndFlush(remind); 
} 

public List<Remind> getAll() { 
    return repository.findAll(); 
} 

} 

コントローラから、それが正常に動作します:

@RestController 
@RequestMapping("/api") 
public class RemindController { 

@Autowired 
private ReminderService service; 

@RequestMapping(value = "/remind", method = RequestMethod.GET, headers="Accept=application/json") 
@ResponseBody 
public List<Remind> getReminder() { 
    return service.getAll(); 
} 
} 

しかし、他のクラス(例えばXMLパーサ、)から、私はサービスのために、nullポインタ例外を取得していますが.SAVE(R):

@Component 
public class XMLConverter implements ResourceLoaderAware { 

@Autowired 
private ReminderService service; 

... 


public void convertFromXMLToObject() throws XmlPullParserException, IOException { 

... 
Remind r=new Remind(); 
r.setTitle(parser.getText()); 
service.save(r); 

... 

} 

} 

マイMVC-ディスパッチャ-servlet.xml:

<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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 


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

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/"/> 
    <property name="suffix" value=".jsp"/> 
</bean> 

<jpa:repositories base-package="server.repository"/> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="myprovider"/> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager"/> 

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
    <property name="transactionManagerName" value="java:jboss/TransactionManager"/> 
</bean> 

何が間違っていますか?

+0

Springパッケージから '@ Component'が正しくインポートされていますか? – dambros

+0

おそらく...私はちょうど注釈を追加しました。どのように私はそれをインポートする必要がありますか? –

+0

importステートメントをチェックして、正しいステートメントをインポートしていることを確認してください。私は前に間違ったものを輸入し、何が起こっていたのかを理解しようと長い時間を費やしました。 – dambros

答えて

0

XMLConverterクラスはどのパッケージですか?ベースパッケージ "server"の下にない場合、@Autowiredアノテーションは機能しません。なぜならアノテーションはスキャンされないからです。

+0

このクラスは "サーバー"パッケージの内部です。 –

関連する問題