I have such Service class:
@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();
}
}
From controller it works fine:
@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();
}
}
But from the other class (Xml parser, for example), I'm getting null pointer exception for service.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);
...
}
}
My mvc-dispatcher-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>
What Am I doing wrong?