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

创建名为“user serviceimpl”的bean时出错:通过字段“user dao”表示的依赖项不满足;

葛越泽
2023-03-14

项目结构图像

import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackages={"com.app.maven.dto"})
@EnableTransactionManagement
public class UserConfig {

    // Change the below based on the DBMS you choose
        private final static String DATABASE_URL = "jdbc:mysql://localhost:3306/SpringMAVEN?createDatabaseIfNotExist=true";
        private final static String DATABASE_DRIVER = "com.mysql.jdbc.Driver";
        private final static String DATABASE_DIALECT = "org.hibernate.dialect.MySQLDialect";
        private final static String DATABASE_USERNAME = "root";
        private final static String DATABASE_PASSWORD = "root";

        // dataSource bean will be available
        @Bean
        public DataSource getDataSource() {

            BasicDataSource dataSource = new BasicDataSource();

            // Providing the database connection information
            dataSource.setDriverClassName(DATABASE_DRIVER);
            dataSource.setUrl(DATABASE_URL);
            dataSource.setUsername(DATABASE_USERNAME);
            dataSource.setPassword(DATABASE_PASSWORD);


            return dataSource;

        }

        // sessionFactory bean will be available

        @Bean
        public SessionFactory getSessionFactory(DataSource dataSource) {

            LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource);

            builder.addProperties(getHibernateProperties());
            builder.scanPackages("com.app.maven.dto");

            return builder.buildSessionFactory();

        }



        // All the hibernate properties will be returned in this method 
        private Properties getHibernateProperties() {

            Properties properties = new Properties();


            properties.put("hibernate.dialect", DATABASE_DIALECT);      
            properties.put("hibernate.show_sql", "true");
            properties.put("hibernate.format_sql", "true");

            properties.put("hibernate.hbm2ddl.auto", "update");


            return properties;
        }

        // transactionManager bean
        @Bean
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
            HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
            return transactionManager;
        }   
}
<?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-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

   <context:component-scan base-package="com.app.maven"></context:component-scan>


   <mvc:resources location="/resources/" mapping="/resources/**"/>

      <bean id="IRVR" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/"></property>
      <property name="suffix" value=".jsp"></property>

    </bean>


   </beans>
@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    private UserService service;

    @RequestMapping(value="/create",method=RequestMethod.POST)
    public ModelAndView add(@ModelAttribute User user)
    {
        System.out.println("add controller");
        service.add(user);
        ModelAndView mv=new ModelAndView("views/sucess");
        mv.addObject("info",user.getlName());
        return mv;
    }
}
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    public void add(User user) {
        userDAO.add(user);

    }

}
@Repository("userDAO")
@Transactional
public class UserDAOImpl implements UserDAO{

    @Autowired
    private SessionFactory sf;
    public void add(User user) {

        try {
            sf.getCurrentSession().persist(user);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

获取此错误

unsatisfiedDependencyException:创建名为“user controller”的bean时出错:通过字段“service”表示的不满足依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“user serviceimpl”的bean时出错:通过字段“user dao”表示的不满足依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“user dao”的bean时出错:通过字段“sf”表示的未满足的依赖项;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建com.app.maven.config.UserConfig中定义的名为“Get SessionFactory”的bean时出错:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanInstantiationException:无法实例化[org.hibernate.sessionFactory]:工厂方法“get sessionFactory”引发异常;嵌套异常为java.lang.NullPointerException org.SpringFramework.Beans.Factory.Annotation.AutoWiredAnnotationBeanPostProcessor$AutoWiredFieldElement.Inject(AutoWiredAnnotationBeanPostProcessor.java:587)

unsatisfiedDependencyException:创建名为“user serviceimpl”的bean时出错:通过字段“user dao”表示的未满足的依赖项;嵌套异常为org.springframework.beans.factory.unsatisfieddependencyexception:创建名为“user dao”的bean时出错:通过字段“sf”表示的未满足的依赖项;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建com.app.maven.config.UserConfig中定义的名为“Get SessionFactory”的bean时出错:通过工厂方法实例化bean失败;嵌套异常为org.springframework.beans.beanInstantiationException:无法实例化[org.hibernate.sessionFactory]:工厂方法“get sessionFactory”引发异常;嵌套异常是java.lang.NullPointerException

共有1个答案

李新霁
2023-03-14

userDAOIMPL中未实现userDAO

 类似资料: