Не удалось автоматически связать поле jdbcTemplate, хотя оно определено в файле конфигурации контекста xml
У меня ошибка с аннотацией @Autowired.
Класс UserService содержит атрибут JdbcTemplate с getter/setter.
Все работает нормально, если я "связываю" этот атрибут из контекстного файла:
<bean id="userService" class="org.tuto.service.UserServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
но с Autowired я получаю сообщение об ошибке this-after.
Может кто-нибудь сказать мне, как вводить с помощью @Autowired без необходимости его настройки. Заранее спасибо.
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jdbc.core.JdbcTemplate org.tuto.service.UserServiceImpl.jdbcTemplate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] 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)}
UserServiceImpl.java:
package org.tuto.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.tuto.mapper.UserMapper; import org.tuto.model.User; @Service("userService") @Transactional public class UserServiceImpl implements UserService{ @Autowired private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public List<User> findAllUsers() { String SQL = "select * from user"; UserMapper userMapper = new UserMapper(); List<User> result = jdbcTemplate.query(SQL, userMapper); return result; } @Override public User findById(long id) { String SQL = "select * from users where id = ?"; User result = jdbcTemplate.queryForObject(SQL, new Object[] { id }, new UserMapper()); return result; } }
весна-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <context:annotation-config /> <context:component-scan base-package="org.tuto" /> <!-- bean definitions go here --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/****" /> <property name="username" value="****" /> <property name="password" value="****" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean> <bean id="userService" class="org.tuto.service.UserServiceImpl"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>