创建 bean 时出错,自动装配依赖项注入失败,无法自动装配字段
Error creating bean, Injection of autowired dependencies failed, Could not autowire field
遇到异常,下面是日志:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'speaker': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate); nested exception is java.lang.NoSuchMethodError: org.springframework.core.MethodParameter.getNestedParameterType()Ljava/lang/Class
java 网络服务:
@WebService
public class voiceRecognition extends SpringBeanAutowiringSupport {
@Autowired
private Speaker speaker;
@WebMethod
public void test() {
String userid = "111";
String enrollmentid = "111";
try{
String test1 = speaker.getEnrollmentId(userid);
System.out.println(test1);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Speaker.java:
package ph.com.aub.mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
@Service
public interface Speaker {
public String getEnrollmentId(@Param("userid") String userid);
}
Speaker.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "ph.com.aub.mapper.Speaker">
<select id = "getEnrollmentId" resultType = "string" parameterType = "string">
Select enrollmentid from speakerids where userid = #{userid}
</select>
</mapper>
web.xml:
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>voiceRecognitionPort</servlet-name>
<!--<servlet-class>ph.com.aub.domain.voiceRecognition</servlet-class>-->
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>voiceRecognitionPort</servlet-name>
<url-pattern>/voiceRecognitionPort</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="ph.com.aub.domain.voiceRecognition"/>
<context:component-scan base-package="ph.com.aub.mapper"/>
<context:annotation-config/>
<bean id="dataSourceSpeaker" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="Speakerdata"/>
</bean>
<bean id="sqlSessionFactorySpeaker" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceSpeaker"/>
<property name="typeAliasesPackage" value="ph.com.aub.domain"/>
<property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
</bean>
<bean id="mapperSpeaker" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ph.com.aub.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactorySpeaker" />
</bean>
</beans>
我认为您在 MapperScannerConfigurer
中配置 basePackage
不正确。它应该是包含映射器 class 而不是映射器 class 本身的包。所以尝试将 basePackage
更改为:
<bean id="mapperSpeaker" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ph.com.aub.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactorySpeaker" />
</bean>
遇到异常,下面是日志:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'speaker': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate); nested exception is java.lang.NoSuchMethodError: org.springframework.core.MethodParameter.getNestedParameterType()Ljava/lang/Class
java 网络服务:
@WebService
public class voiceRecognition extends SpringBeanAutowiringSupport {
@Autowired
private Speaker speaker;
@WebMethod
public void test() {
String userid = "111";
String enrollmentid = "111";
try{
String test1 = speaker.getEnrollmentId(userid);
System.out.println(test1);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
Speaker.java:
package ph.com.aub.mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
@Service
public interface Speaker {
public String getEnrollmentId(@Param("userid") String userid);
}
Speaker.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace = "ph.com.aub.mapper.Speaker">
<select id = "getEnrollmentId" resultType = "string" parameterType = "string">
Select enrollmentid from speakerids where userid = #{userid}
</select>
</mapper>
web.xml:
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>voiceRecognitionPort</servlet-name>
<!--<servlet-class>ph.com.aub.domain.voiceRecognition</servlet-class>-->
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>voiceRecognitionPort</servlet-name>
<url-pattern>/voiceRecognitionPort</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="ph.com.aub.domain.voiceRecognition"/>
<context:component-scan base-package="ph.com.aub.mapper"/>
<context:annotation-config/>
<bean id="dataSourceSpeaker" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="Speakerdata"/>
</bean>
<bean id="sqlSessionFactorySpeaker" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceSpeaker"/>
<property name="typeAliasesPackage" value="ph.com.aub.domain"/>
<property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
</bean>
<bean id="mapperSpeaker" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ph.com.aub.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactorySpeaker" />
</bean>
</beans>
我认为您在 MapperScannerConfigurer
中配置 basePackage
不正确。它应该是包含映射器 class 而不是映射器 class 本身的包。所以尝试将 basePackage
更改为:
<bean id="mapperSpeaker" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="ph.com.aub.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactorySpeaker" />
</bean>