Spring NoSuchMethodException(我创建了一个构造函数)

时间:2016-01-18 04:06:35

标签: java spring hibernate spring-mvc

我想问一下我在哪里出错了。我刚刚使用Spring和Hibernate 4启动了一个简单的登录功能,但似乎我似乎总是得到NoSuchMethodException。试着到处查看我出错的地方,但这就像我从来没有做到这一点。任何帮助将非常感谢。以下是代码。

bean.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"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager" />
    <property name="transactionAttributes">
        <props>
            <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> 
            <prop key="try*">PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,-Exception</prop>
        <!--  <prop key="try*">PROPAGATION_REQUIRED,ISOLATION_SERIALIZABLE,-Exception</prop> -->    
        </props>
    </property>
</bean> 

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url"
        value="jdbc:oracle:thin:@db01.aprdev.com:1521:aprdev" />
    <property name="username" value="training" />
    <property name="password" value="training" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
</bean> -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
    <property name="url"
        value="jdbc:sqlserver://localhost:1433;databaseName=test" />
    <property name="username" value="sa" />
    <property name="password" value="pass123" />
    <property name="initialSize" value="5" />
    <property name="maxActive" value="10" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="mappingResources">
        <list> 
            <value>com/model/User.hbm.xml</value> 
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> -->
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
        </props>
    </property>
</bean>

<bean id="userDAO" class="com.dao.impl.UserDAOImpl">
    <property name="sessionFactory" ref="sessionFactory" />
</bean> 


<bean id="serviceProxyTemplate" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="interceptorNames">
        <list>
            <value>transactionInterceptor</value> 
        </list>
    </property>
</bean>

<bean id="userServiceProxy" parent="serviceProxyTemplate">
    <property name="target">
        <bean id="userService" class="com.services.impl.UserServiceImpl">
            <property name="userDAO" ref="userDAO" />
        </bean>
    </property>
</bean>

User.java

package com.model;


public class User {
private String id;
private String name;
private String password;

public User() {}

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
}

UserServiceImpl.java

package com.services.impl;

import java.util.List;

import com.dao.UserDAO;
import com.model.User;
import com.services.UserService;

public class UserServiceImpl implements UserService {

private UserDAO userDAO;

public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
}

@Override
public List validate(String name, String password) {
    User user = new User();
    user.setName(name);
    user.setPassword(password);
    return this.userDAO.validate(user);
}

}

UserDAOImpl.java

package com.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.dao.UserDAO;
import com.model.User;

public class UserDAOImpl implements UserDAO{

private SessionFactory sessionFactory;

public UserDAOImpl(SessionFactory session) {
    this.sessionFactory = session;
}

public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
}

@Override
public List validate(User user) throws HibernateException {
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery("from User where name = :name and password = :pass ");
    query.setParameter("name", user.getName());
    query.setParameter("pass", user.getPassword());
    return query.list();
}

}

Test.java

package com.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.services.UserService;



public class SpringUtil {
    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext(){
    if(applicationContext==null){
        applicationContext = new ClassPathXmlApplicationContext("com/factory/bean.xml");
    }
    return applicationContext;
}

    public static UserService getUserService(){
        return (UserService) getApplicationContext().getBean("userServiceProxy");
    }
}

非常感谢帮助。非常感谢你!

1 个答案:

答案 0 :(得分:0)

不确定我是否已经回答了问题。

这是我的测试

package com.services.impl;

import static org.junit.Assert.assertEquals;

import java.util.List;

import javax.transaction.Transactional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.model.User;
import com.model.UserDAO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class StudyPointRepositoryTest {

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserDAO dao;

    @Test
    @Transactional
    public void test1() {

        dao.save(makeUser("Fred", "pass1"));
        dao.save(makeUser("Bill", "pass2"));
        dao.save(makeUser("Nobby", "pass3"));

        List<User> validate = userService.validate("Fred", "pass1");
        assertEquals(1, validate.size());

        validate = userService.validate("Fred", "wrongpass");
        assertEquals(0, validate.size());
    }

    private User makeUser(String name, String password) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        return user;
    }

}

这是我的测试配置

<?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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com" />
    <jdbc:embedded-database id="dataSource" type="H2" />
    <tx:annotation-driven />
    <jpa:repositories base-package="com.model" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.model" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

</beans>

这是我的DAO

package com.model;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserDAO extends CrudRepository<User, Integer> {

    List<User> findByNameAndPassword(String name, String password);
}

这是我的服务

package com.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.model.User;
import com.model.UserDAO;

@Service
public class UserServiceImpl {

    @Autowired
    private UserDAO userDAO;

    public List<User> validate(String name, String password) {
        return userDAO.findByNameAndPassword(name, password);
    }

}

这是我的pom,可能的东西,不需要

<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>com.greg</groupId>
    <artifactId>jpa</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <name>fmis Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.2.4.RELEASE</spring.version>
        <spring.data.version>1.9.2.RELEASE</spring.data.version>
        <hibernate.version>5.0.6.Final</hibernate.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring.data.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.190</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.13</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>jpaTest</finalName>
    </build>

</project>