I'am working on a project(using spring boot) in which i need to send real time email notification, the email service is invoked in other class not in controller. I receive an error that my service mail is null but when i put it in controller(RestController) it's working. Where is my mistake? is it possible to call for mail service in other class(services)? For the process using RestController i used this example and as i said it's working, here is the link: http://therealdanvega.com/blog/2016/01/13/sending-async-emails-in-spring
2 Answers
I receive an error that my service mail is null but when i put it in controller(RestController) it's working
It means that Spring didn't inject instance of the service into the controller. It's hard to help you in this case because you didn't provide neither error message nor controller code. I suggest to check the following
- class member with service annotated with
@Autowired
(or@Inject
) - bean with the mail service is defined in the application context
See also: Why is my Spring @Autowired field null?

- 1
- 1

- 14,904
- 7
- 53
- 69
-
in the controller it's working...i have problem when i call the service from other class – Cifer Ulquiorra Mar 30 '17 at 13:36
-
Because you mentioned about null, the problem is the same, so my is answer still correct. But without code and error messages with stack traces no one would be able to help you. – Slava Semushin Mar 30 '17 at 13:42
@Autowire annotation will not work if you are calling the Service from another class, which is not manged by Spring Container. Below link is having the same problem like yours.
@Autowired and @Service working from controller but not from a different package
If you want to call this Service class from another class which is not managed by Spring, use a getter/setter method in Service class. If you share the code, it will be helpful to debug more.

- 1
- 1

- 51
- 2