首页 文章

创建名为'userServiceImpl'的bean时出错:通过字段'userDAO'表示的不满意的依赖性;

提问于
浏览
0

project Structure Image

UserConfig

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;
        }   
}

beans.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-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>

UserController

@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;
    }
}

UserServiceImpl

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

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

    }

}

UserDAOImpl

@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();
        }

    }

}

getting this error

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'userController'的bean时出错:通过字段'service'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'userServiceImpl'的bean时出错:通过字段'userDAO'表示的不满意依赖;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'userDAO'的bean时出错:通过字段'sf'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:在com.app.maven.config.UserConfig中定义名为'getSessionFactory'的bean时出错:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.hibernate.SessionFactory]:工厂方法'getSessionFactory'抛出异常;嵌套异常是java.lang.NullPointerException org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'userServiceImpl'的bean时出错:通过字段'userDAO'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'userDAO'的bean时出错:通过字段'sf'表示的不满意的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:在com.app.maven.config.UserConfig中定义名为'getSessionFactory'的bean时出错:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.hibernate.SessionFactory]:工厂方法'getSessionFactory'抛出异常;嵌套异常是java.lang.NullPointerException

1 回答

  • 1

    您错过了在 UserDAOImpl 中实施 UserDAO

相关问题