I am using Spring, i am not using JSF, using ajax etc.
I have a service
@Service(value="mailService")
public class MailService{
//an autowired dao, etc here
}
I have a bean, i want this bean have one MailService.
//Here i have tried so far individually @Service, @Component, @Controller
public class EmailAgent{
private MailService mailService;
public MailService getMailService() {
return mailService;
}
@Autowired
public void setMailService(MailService mailService) {
this.mailService = mailService;
}
}
I have below in applicationContext.xml
<context:component-scan base-package="mypack.containing.emailagent.path.too" />
<context:annotation-config/>
mailService in EmailAgent is null on calling one EmailAgent method.
I request some advise, and i want to understand why i do not get any exception during server starting up, like, “i could not autowire mailService”. Awhile ago i was using JSF and i was be able to see such warning while server start up.
PROBLEM IS SPECIFIED, HOW TO SOLVE
i was generating ea with EmailAgent ea=new EmailAgent();
It is my sillyness but,
when i try to get one context like:
ClassPathXmlApplicationContext cont=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
mailService=()cont.getBean("mailService");
i am faced with the problem non unique cache etc, problems about generating one more context.
So how can i “get” context, preventing “generating” one more?
SOLVED
I added
<bean class="email.EmailService" id="emailService">
<dwr:remote javascript="emailService">
<dwr:include method="sendMail"/>
<dwr:include method="readAllMails"/>
<dwr:include method="removeMailsById"/>
</dwr:remote>
</bean>
to dwr.xml and deleted annotation from emailService about dwr.
dwr s and spring s annotations were really conflicting as i can see.
Thanks to @NimChimpsky for idea on detecting the problem.
You don’t need another context, unless you are integration testing.
Where is emailagent being used ? Just inject it as normal there. As you do with mailservice.
You can annotate it with Component or Service, both are appropriate.