簡體   English   中英

創建bean時出錯

[英]Getting error while creating bean

請幫我解決這個問題。

[RMI TCP連接(3)-127.0.0.1]警告org.springframework.web.context.support.XmlWebApplicationContext-上下文初始化期間遇到異常-取消刷新嘗試org.springframework.beans.factory.BeanCreationException:創建名稱為'的bean時出錯在ServletContext資源[/WEB-INF/mvc-dispatcher-servlet.xml]中定義的transactionManager':調用init方法失敗; 嵌套的異常是org.hibernate.service.UnknownUnwrapTypeException:無法展開為請求的類型[javax.sql.DataSource]

my mvc-dispatcher-servlet.xml

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

    <context:component-scan base-package="com.springapp.mvc"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>       
    </bean>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

hibernate.cfg.xml

    <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/company</property>
        <property name="hibernate.connection.username">Kate</property>
        <property name="hibernate.connection.password">Ant0987M@+</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="show_sql">false</property>
        <mapping class="com.springapp.mvc.User"/>
    </session-factory>
</hibernate-configuration>

User.java

package com.springapp.mvc;

import javax.persistence.*;

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "firstname")
    private String firstName;

    @Column(name = "lastname")
    private String lastName;

    @Column(name = "email")
    private String email;

    public User() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String name) {
        this.firstName = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    } 
}

UserController.java

包com.springapp.mvc;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Controller
public class UserController {

    @Autowired
    private UserRepository userRepository;

    static final Logger logger = LogManager.getLogger("MyLogger");

    @RequestMapping(value = "/", method = {RequestMethod.GET, RequestMethod.HEAD})
    public String listUsers(ModelMap model) {

        logger.info("in meth listUsers");
        logger.trace("trace in listUsers");

        model.addAttribute("user", new User());
        model.addAttribute("users", userRepository.findAll());
        return "users";
    }

    @RequestMapping(value = "/add", method = {RequestMethod.POST, RequestMethod.HEAD})
    public String addUser(@ModelAttribute("user") User user, BindingResult result) {

        logger.info("in meth addUser");

        userRepository.save(user);

        return "redirect:/"/* + userRepository.getClass().getName()*/;
    }

    @RequestMapping("/delete/{userId}")
    public String deleteUser(@PathVariable("userId") Long userId) {

        logger.info("in meth deleteUser");

        userRepository.delete(userRepository.findOne(userId));

        return "redirect:/";
    }

    @RequestMapping(value = "/api/users", method = {RequestMethod.GET, RequestMethod.HEAD})
    public
    @ResponseBody
    String listUsersJson(ModelMap model) throws JSONException {
        JSONArray userArray = new JSONArray();
        for (User user : userRepository.findAll()) {
            JSONObject userJSON = new JSONObject();
            userJSON.put("id", user.getId());
            userJSON.put("firstName", user.getFirstName());
            userJSON.put("lastName", user.getLastName());
            userJSON.put("email", user.getEmail());
            userArray.put(userJSON);
        }
        return userArray.toString();
    }
}

UserRepository.java

 package com.springapp.mvc;

import org.springframework.data.jpa.repository.JpaRepository;

import org.springframework.data.repository.Repository;

import java.util.List;

public interface UserRepository extends Repository <User, Long> {

   public <S extends User> S save(S entity);

    public User findOne(Long aLong);

    public List<User> findAll();

    public void delete(User entity);

}

在您的配置中,刪除休眠狀態。 從配置

 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/company</property>
        <property name="hibernate.connection.username">Kate</property>
        <property name="hibernate.connection.password">Ant0987M@+</property>

更正的配置

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/company</property>
        <property name="connection.username">Kate</property>
        <property name="connection.password">Ant0987M@+</property>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM