当前位置: 首页 > 知识库问答 >
问题:

BeanCreationException:创建名为“User Controller”的bean时出错:注入autowired依赖项失败

唐健
2023-03-14

我浏览了很多相关的主题,尝试了不同的方式,但错误仍然存在。环境:Spring data jpa、Hibernate、maven、tomcat7

错误堆栈:

<beans xmlns="http://www.springframework.org/schema/beans" ... ... >
<context:component-scan base-package="com.zzz">
    <context:exclude-filter type="annotation"
        expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<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/zzz" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="packagesToScan" value="com.zzz.entity" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
    </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="dataSource" ref="dataSource" />
 </bean>

 <jpa:repositories base-package="com.zzz.repository" />
<beans xmlns="http://www.springframework.org/schema/beans" ... ... >
<context:component-scan base-package="com.zzz.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/static/**" location="static/" />

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/defs/templates.xml</value>
        </list>
    </property>
</bean>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles3.TilesView" />
</bean>
</beans>
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/users")
    public String users(Model model) {
        model.addAttribute("users", userService.findAll());
        return "users";
    }
}
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public List<User> findAll() {
        return userRepository.findAll();
    }
}

userrepository.java

public interface UserRepository extends JpaRepository<User, Integer> {

}

UDPATE 1

有2个autowired错误:

不能autowire字段:private com.zzz.repository.userrepository com.zzz.service.userservice.userrepository

共有1个答案

常波
2023-03-14

另一种猜测是:在dispatcher xml中,您只扫描控制器包...因此,如果没有加载application.xml,它不会扫描服务包,因此用户服务也不会加载...如果在dispatcher.xml中扫描整个com.zzz包,会发生什么,我会很感兴趣

 类似资料: