Spring Bean Auto Connect Error

I try to implement email features in my application, but I keep getting

No matching bean of type [org.springframework.mail.javamail.JavaMailSenderImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 

Can someone point out what I'm doing wrong?

Xml configuration for bean:

 <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <context:annotation-config/> //...other stuff <beans:bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean"> <beans:property name="jndiName" value="EmailServer" /> </beans:bean> <beans:bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <beans:property name="session" ref="mailSession"/> </beans:bean> 

EmailServiceImpl Class:

 @Service public class EmailServiceImpl implements EmailService { @Autowired private JavaMailSenderImpl emailSender; //more code.. } 

project_structure

+6
source share
6 answers

Thanks to everyone for their answers. I was unable to get auto services, but I got a general email solution by following these steps:

  • configure mail forwarding in weblogic named jndi "myMailSession"

add to servlet-context.xml:

 <beans:bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean"> <beans:property name="jndiName" value="myMailSession" /> </beans:bean> <beans:bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <beans:property name="session" ref="mailSession"/> </beans:bean> <beans:bean id="emailServiceImpl" class="com.name.here.business.EmailServiceImpl"> <beans:property name="mailSender" ref="mailSender"/> </beans:bean> 

add to web.xml:

 <resource-ref> <description>the email session</description> <res-ref-name>myMailSession</res-ref-name> <res-type>javax.mail.Session</res-type> <res-auth>Container</res-auth> </resource-ref> 

add to weblogic.xml:

 <resource-description> <res-ref-name>myMailSession</res-ref-name> <jndi-name>myMailSession</jndi-name> </resource-description> 

EmailServiceImpl:

 @Service public class EmailServiceImpl implements EmailService { private JavaMailSender mailSender; public void setMailSender(JavaMailSender mailSender) { this.mailSender = mailSender; } //..other code } 
+2
source

I struggled with this very problem for the email service class, encoded as follows:

 @Service("emailService") public class EmailService { @Autowired private JavaMailSenderImpl mailSender; ... public void send(...) { // send logic } } 

I stumbled upon a solution while reading about a related topic. The key point is that the JavaMailSender interface JavaMailSender defined in applicationContext.xml as the Spring class JavaMailSenderImpl .

Step 1: The application context file has been modified to include the following bean definition:

 <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" p:host="myMailserver.mycompany.com" /> 

Step 2: The email service class has been modified to look like this:

 @Service("emailService") public class EmailService { @Autowired private JavaMailSender mailSender; // Observe the change in the type ... 

Voila! Spring is happy. I was , although I would like to hear the correct explanation of the original error.

+4
source

You need to add <context:annotation-config/> to your config file so that Spring auto-negotiates annotated beans.

http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config

+1
source

From the error message, I can conclude that the automatic installation works, but cannot find the required bean.

Be sure to download all bean definition files.

+1
source

Do you have @Service or similar annotation in your JavaMailSenderImpl class? This will cause the Spring component scanner to place an instance of it in the Spring container, which can then auto-amplify it to EmailServiceImpl.

+1
source

Here is how I fixed it:

I also ran into this problem, I tried to run simple tutorials on the Internet that worked fine during testing by downloading the app-context.xml file manually, but when I tried to start the spring mvc application, it always showed this error:

 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.mail.javamail.JavaMailSender] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:952) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) ... 42 more 

After trying all kinds of things, I had to move these two lines from my JPA / DB configuration file to the end of my root config file.

 <context:annotation-config/> <context:component-scan base-package="my.app.service.layer"/> 

I am still studying spring, but I think there was a problem with the order in which they appear.

Edit: This question seems to clarify the order issue:

Difference between applicationContext.xml and spring -servlet.xml in spring Framework

+1
source

Source: https://habr.com/ru/post/904546/


All Articles