0

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?

Tom Wally
  • 542
  • 3
  • 8
  • 20
  • Is the `@Component` correctly imported from the Spring package? – dambros Apr 06 '16 at 12:01
  • Maybe not... I've just added an annotation. How I have to imported it? – Tom Wally Apr 06 '16 at 12:13
  • Check the import statement just to make sure you are importing the right one. I have imported the wrong thing before and spent a long time trying to understand what was going on. – dambros Apr 06 '16 at 12:23
  • hm... I don't understand what import do you mean...@Component annotation just import: import org.springframework.stereotype.Component; That's all... – Tom Wally Apr 06 '16 at 12:28
  • That is it, it is the correct one them – dambros Apr 06 '16 at 12:29

1 Answers1

0

In which package is class XMLConverter? If it is not below the base-package "server", the @Autowired annotation will not work, cause anotations will not be scanned.

Wakachopo
  • 149
  • 7