Spring4+SpringMVC+Hibernate5整合时候,无法注入相关的类
 严重: StandardWrapper.Throwable
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'userService': 
Unsatisfied dependency expressed through field 'userDao'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userDao': Unsatisfied dependency expressed through field 'sessionFactory';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'org.hibernate.SessionFactory' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=sessionFactory)}

Dao层

 @Repository("userDao")
public class UserDaoImpl implements UserDao {

    @Autowired
    @Qualifier("sessionFactory")
    private SessionFactory sessionFactory;

    public void addUser(User user) {
        sessionFactory.getCurrentSession().save(user);
    }
}

Service层

 @Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void addUser(User user) {
        userDao.addUser(user);
    }

}

Controller层

 @Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @RequestMapping(value = "sucess.do")
    public String add(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username + " " + password);
        User user = new User();
        user.setName(username);
        user.setPassword(password);
        userService.addUser(user);
        return "sucess";
    }
}

applicationContext.xml

    <context:component-scan base-package="com.zlm.controller"
        use-default-filters="false" />
    <context:property-placeholder location="classpath:config/db.properties" />
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
    </bean>
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.zlm.entity" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect </prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>com/zlm/entity/User.hbm.xml</value>
            </list>
        </property>
    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>

springmvc.xml

    <mvc:annotation-driven />
    <mvc:default-servlet-handler/> 
    <context:component-scan base-package="com.zlm.service,com.zlm.dao" />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

6个回答

报错主要是userDao,userService,userController这三者之间注入关系,求大神帮我看一下!

我晕,代码贴不上 ,直接说吧
配置文件applicationContext中的包扫描路径由com.zlm.controller改成com.zlm试试

derrickie
derrickie 回复doumanggu2230: 反正应该就是这个问题,扫描的包路径不对。例如你的包路径是类似com.zlm.controller/com.zlm.service之类的。你的扫描路径就应该是com.zlm
大约 3 年之前 回复
doumanggu2230
doumanggu2230 m没用。。
大约 3 年之前 回复

“<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->

扫描包的路径改成com.zlm,
com.zlm.controller这样应该没扫描到service,dao等

useraxian
曾经是BBoy的程序猿 org.hibernate.SessionFactory没有实现类,跟xml配置的SessionFactory(org.springframework.orm.hibernate5.LocalSessionFactoryBean)没有关系。可以用LocalSessionFactoryBean.getObject()的方法来获取SessionFactory。
大约 3 年之前 回复
useraxian
曾经是BBoy的程序猿 回复doumanggu2230: 那你的applicationContext.xml有加载吗
大约 3 年之前 回复
doumanggu2230
doumanggu2230 改了没用,还是一样的错误
大约 3 年之前 回复

"System.out.println("");"

alert(“")

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问