When i call a service layer from controller,data are being saved in database.
My controller is:
@RestController
@RequestMapping("/api")
public class ApiController {
@Autowired(required = true)
PersonService personService; //This is service layer class
@RequestMapping("/add")
public void add(){
person.setLastName("Rahim");
person.setFirstName("Uddin");
person.setAddress("Dhaka");
person.setCity("Dhaka");
personService.add(person);
}
}
Service layer is:
@Service
@Transactional
public class PersonServiceImpl implements PersonService {
@Autowired
PersonDao personDao;
@Override
public void addPerson(Person person) {
personDao.addPerson(person);
}
}
Till now everything is ok.
But when i call the service layer through another class, null pointer exception is being shown. At that time: My controller is:
@RestController
@RequestMapping("/api")
public class ApiController {
@Autowired(required = true)
PersonService personService; //This is service layer class
@RequestMapping("/add")
public void add(){
MiddleClass m=new MiddleClass();
m.create();
}
}
My MiddleClass is:
public class MiddleClass {
@Autowired
PersonService personService;
public void create(){
Person person=new Person();
person.setLastName("Rahim");
person.setFirstName("Uddin");
person.setAddress("Dhaka");
person.setCity("Dhaka");
personService.addPerson(person);//this time same service layer
//is showing null pointer exception here
}
}
WHY?????????? is it for lacking of any annotation in MiddleClass?