我正在创建一个购物车,我得到一些错误

时间:2017-01-30 09:34:33

标签: java spring hibernate

我收到了这样的输出错误:

  

加载应用程序时出现异常:java.lang.IllegalStateException:   ContainerBase.addChild:start:org.apache.catalina.LifecycleException:   org.springframework.beans.factory.BeanCreationException:错误   创建名为'myDBAuthenticationService'的bean:注入   自动连接依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:私有   org.o7planning.springmvcshoppingcart.dao.AccountDAO   org.o7planning.springmvcshoppingcart.authentication.MyDBAuthenticationService.accountDAO;   嵌套异常是       org.springframework.beans.factory.BeanCreationException:创建名为'accountDAO'的bean时出错:注入自动装配   依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:private org.hibernate.SessionFactory   org.o7planning.springmvcshoppingcart.dao.impl.AccountDAOImpl.sessionFactory;   嵌套异常是   org.springframework.beans.factory.BeanCreationException:       在org.o7planning.springmvcshoppingcart.config.ApplicationContextConfig中定义名为'sessionFactory'的bean时出错:   通过工厂方法进行Bean实例化失败;嵌套异常是       org.springframework.beans.BeanInstantiationException:无法实例化[org.hibernate.SessionFactory]:Factory方法   'getSessionFactory'抛出异常;嵌套异常是       java.lang.NoSuchMethodError:org.jboss.logging.Logger.debugf(Ljava / lang / String; I)V

<!-- language: none -->



In MYDBAuthenticationService.java

    package org.o7planning.springmvcshoppingcart.authentication;

    import java.util.ArrayList;
    import java.util.List;

    import org.o7planning.springmvcshoppingcart.dao.AccountDAO;
    import org.o7planning.springmvcshoppingcart.entity.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.authority.SimpleGrantedAuthority;
    import org.springframework.security.core.userdetails.User;
    import org.springframework.security.core.userdetails.UserDetails;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.core.userdetails.UsernameNotFoundException;
    import org.springframework.stereotype.Service;

    @Service
    public class MyDBAuthenticationService implements UserDetailsService {

        @Autowired
        private AccountDAO accountDAO;

        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            Account account = accountDAO.findAccount(username);
            System.out.println("Account= " + account);

            if (account == null) {
                throw new UsernameNotFoundException("User " //
                        + username + " was not found in the database");
            }

            // EMPLOYEE,MANAGER,..
            String role = account.getUserRole();

            List<GrantedAuthority> grantList = new ArrayList<GrantedAuthority>();

            // ROLE_EMPLOYEE, ROLE_MANAGER
            GrantedAuthority authority = new SimpleGrantedAuthority("ROLE_" + role);

            grantList.add(authority);

            boolean enabled = account.isActive();
            boolean accountNonExpired = true;
            boolean credentialsNonExpired = true;
            boolean accountNonLocked = true;

            UserDetails userDetails = (UserDetails) new User(account.getUserName(), //
                    account.getPassword(), enabled, accountNonExpired, //
                    credentialsNonExpired, accountNonLocked, grantList);

            return userDetails;
        }

    }

    In applicationcontextconfig.java
    package org.o7planning.springmvcshoppingcart.config;

    import java.util.Properties;

    import javax.sql.DataSource;

    import org.hibernate.SessionFactory;
    import org.o7planning.springmvcshoppingcart.dao.AccountDAO;
    import org.o7planning.springmvcshoppingcart.dao.OrderDAO;
    import org.o7planning.springmvcshoppingcart.dao.ProductDAO;
    import org.o7planning.springmvcshoppingcart.dao.impl.AccountDAOImpl;
    import org.o7planning.springmvcshoppingcart.dao.impl.OrderDAOImpl;
    import org.o7planning.springmvcshoppingcart.dao.impl.ProductDAOImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.support.ResourceBundleMessageSource;
    import org.springframework.core.env.Environment;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    import org.springframework.orm.hibernate5.HibernateTransactionManager;
    import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import org.springframework.web.multipart.commons.CommonsMultipartResolver;
    import org.springframework.web.servlet.view.InternalResourceViewResolver;

    @Configuration
    @ComponentScan("org.o7planning.springmvcshoppingcart.*")
    @EnableTransactionManagement
    // Load to Environment.
    @PropertySource("classpath:ds-hibernate-cfg.properties")
    public class ApplicationContextConfig {

        // The Environment class serves as the property holder
        // and stores all the properties loaded by the @PropertySource
        @Autowired
        private Environment env;

        @Bean
        public ResourceBundleMessageSource messageSource() {
            ResourceBundleMessageSource rb = new ResourceBundleMessageSource();
            // Load property in message/validator.properties
            rb.setBasenames(new String[] { "messages/validator" });
            return rb;
        }

        @Bean(name = "viewResolver")
        public InternalResourceViewResolver getViewResolver() {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
            viewResolver.setPrefix("/WEB-INF/pages/");
            viewResolver.setSuffix(".jsp");
            return viewResolver;
        }

        // Config for Upload.
        @Bean(name = "multipartResolver")
        public CommonsMultipartResolver multipartResolver() {
            CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();

            // Set Max Size...
            // commonsMultipartResolver.setMaxUploadSize(...);

            return commonsMultipartResolver;
        }

        @Bean(name = "dataSource")
        public DataSource getDataSource() {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();

            // See: ds-hibernate-cfg.properties
            dataSource.setDriverClassName(env.getProperty("ds.database-driver"));
            dataSource.setUrl(env.getProperty("ds.url"));
            dataSource.setUsername(env.getProperty("ds.username"));
            dataSource.setPassword(env.getProperty("ds.password"));

            System.out.println("## getDataSource: " + dataSource);

            return dataSource;
        }

        @Autowired
        @Bean(name = "sessionFactory")
        public SessionFactory getSessionFactory(DataSource dataSource) throws Exception {
            Properties properties = new Properties();

            // See: ds-hibernate-cfg.properties
            properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
            properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
            properties.put("current_session_context_class", env.getProperty("current_session_context_class"));


            LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

            // Package contain entity classes
            factoryBean.setPackagesToScan(new String[] { "org.o7planning.springmvcshoppingcart.entity" });
            factoryBean.setDataSource(dataSource);
            factoryBean.setHibernateProperties(properties);
            factoryBean.afterPropertiesSet();
            //
            SessionFactory sf = factoryBean.getObject();
            System.out.println("## getSessionFactory: " + sf);
            return sf;
        }

        @Autowired
        @Bean(name = "transactionManager")
        public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
            HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

            return transactionManager;
        }

        @Bean(name = "accountDAO")
        public AccountDAO getApplicantDAO() {
            return new AccountDAOImpl();
        }

        @Bean(name = "productDAO")
        public ProductDAO getProductDAO() {
            return new ProductDAOImpl();
        }

        @Bean(name = "orderDAO")
        public OrderDAO getOrderDAO() {
            return new OrderDAOImpl();
        }

        @Bean(name = "accountDAO")
        public AccountDAO getAccountDAO()  {
            return new AccountDAOImpl();
        }

    }

    In pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                         http://maven.apache.org/maven-v4_0_0.xsd">

        <modelVersion>4.0.0</modelVersion>
        <groupId>org.o7planning</groupId>
        <artifactId>SpringMVCAnnotationShoppingCart</artifactId>
        <packaging>war</packaging>
        <version>1.0.0</version>
        <name>SpringMVCAnnotationShoppingCart Maven Webapp</name>
        <url>http://maven.apache.org</url>


        <properties>
            <java-version>1.7</java-version>
        </properties>

        <repositories>
            <!-- Repository for ORACLE JDBC Driver -->
            <repository>
                <id>codelds</id>
                <url>https://code.lds.org/nexus/content/groups/main-repo</url>
            </repository>
        </repositories>

        <dependencies>

            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>3.8.1</version>
                <scope>test</scope>
            </dependency>

            <!-- Servlet API -->
            <!-- http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>

            <!-- Jstl for jsp page -->
            <!-- http://mvnrepository.com/artifact/javax.servlet/jstl -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>


            <!-- JSP API -->
            <!-- http://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
                <scope>provided</scope>
            </dependency>

           <!-- Apache Commons FileUpload -->
           <!-- http://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
           <dependency>
               <groupId>commons-fileupload</groupId>
               <artifactId>commons-fileupload</artifactId>
               <version>1.3.1</version>
           </dependency>        

            <!-- Spring dependencies -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>

            <!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>

            <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>

            <!-- http://mvnrepository.com/artifact/org.springframework/spring-orm -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>

            <!-- Spring Security Artifacts - START -->
            <!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-web -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-web</artifactId>
                <version>4.0.4.RELEASE</version>
            </dependency>

            <!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-config -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-config</artifactId>
                <version>4.0.4.RELEASE</version>
            </dependency>

            <!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-taglibs -->
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-taglibs</artifactId>
                <version>4.0.4.RELEASE</version>
            </dependency>

            <!-- Spring Security Artifacts - END -->

            <!-- Hibernate -->
            <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.1.0.Final</version>
            </dependency>

            <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-entitymanager</artifactId>
                <version>4.3.1.Final</version>
            </dependency>


            <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-c3p0</artifactId>
                <version>5.1.0.Final</version>
            </dependency>


            <!-- MySQL JDBC driver -->
            <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.34</version>
            </dependency>

              <!-- Email validator,... -->
            <!-- http://mvnrepository.com/artifact/commons-validator/commons-validator -->
            <dependency>
                <groupId>commons-validator</groupId>
                <artifactId>commons-validator</artifactId>
                <version>1.5.0</version>
            </dependency>

        </dependencies>


        <build>
            <finalName>SpringMVCAnnotationShoppingCart</finalName>

        </build>


    </project>

1 个答案:

答案 0 :(得分:0)

尝试,在下面评论:

        <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-c3p0 -->
      <!--  <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-c3p0</artifactId>
            <version>5.1.0.Final</version>
        </dependency> -->