0
votes

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: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': 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 'sf'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.app.maven.config.UserConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NullPointerException org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': 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 'sf'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in com.app.maven.config.UserConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'getSessionFactory' threw exception; nested exception is java.lang.NullPointerException

1

1 Answers

1
votes

You missed implementing UserDAO in UserDAOImpl