Reputation: 4934
I have a class that starts like this:
import javax.mail.Session;
//... more imports
@Component("eMailUtility")
public class MailUtility {
@Autowired
Session mailSession;
//...
}
My IDE tells me "Could not autowire. No beans of 'Session' type found."
This message doesn't surprise me, but I'm not sure how to fix it. Session is a final class with factory methods but no public constructors. I can easily instantiate a Session somewhere, but I don't know what I need to do to make it recognizable as a target of an autowired injection. All the examples I've found on the internet show how to autowire an instance of a class that I wrote, which doesn't help me here.
(A detailed explanation of exactly how autowire works, which doesn't gloss over anything, would be very helpful, but I can't seem to find one. If you know of a good link, that would be helpful.)
Upvotes: 1
Views: 1100
Reputation: 798
You'd have to create a method in a class that is annotated with @Configuration
that returns a Session object and annotate that method with @Bean
. In your case something like this:
@Bean
public Session session() {
return <instance>;
}
If it was one of your own classes you could also annotate it with @Component
, or other annotations that are itself annotated with @Component
. Spring would then find the class with this annotation and automatically create the bean for you.
For an explanation about @Autowired
you can look at this answer: Understanding Spring @Autowired usage
Upvotes: 1