-2

以前にSpringフレームワークを作成してからデータベース接続に置き換えましたが、Beanの作成に問題があります。

展開中に以下のエラーも受け取ります。

DEFAULT=C:\Users\gopc\Documents\NetBeansProjects\HelloSpringJDBC\build\web&name=HelloSpringJDBC&contextroot=/HelloSpringJDBC&force=true GlassFish Server 3.1.2 で失敗しました 展開中にエラーが発生しました: アプリのロード中に例外が発生しました: java.lang.IllegalStateException: ContainerBase .addChild: 開始: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: ServletContext リソース [/WEB-INF/applicationContext.xml] で定義された名前 'productManager' の Bean の作成中にエラーが発生しました: プロパティ値の設定中にエラーが発生しました; ネストされた例外は org.springframework.beans.NotWritablePropertyException: Invalid property 'productDao' of bean class [SimpleProductManager]: Bean プロパティ 'productDao' is not writable or has an invalid setter method. セッターのパラメーターの型は、ゲッターの戻り値の型と一致していますか? 詳細については、server.log を参照してください。C:\Users\gopc\Documents\NetBeansProjects\HelloSpringJDBC\nbproject\build-impl.xml:1029: モジュールがデプロイされていません。詳細については、サーバー ログを参照してください。

ソース

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


    <bean id="externalDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" scope="singleton"     destroy-method="close">
        <property name="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
        <property name="url" value="jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=C://Users//gopc//Documents//odbc_sql.accdb"/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>

    <bean id="productManager" class="SimpleProductManager">
        <property name="productDao" ref="productDao"/>
    </bean> 
    <bean id="productDao" class="JdbcProductDao">
        <property name="dataSource" ref="externalDataSource"/>
    </bean> 

</beans>

spirngapp-servlet.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">


    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
       <property name="basename" value="messages"/>
    </bean> 

    <bean name="/hello.htm" class="HelloController"> 
            <property name="productManager" ref="productManager"/>    
    </bean>       


    <!-- we can prefix="/" 
    http://localhost:8080/HelloSpring/hello.htm
    specify the path in  modelview from the controller 
                        OR
    Decouple the view from the controller                    
    prefix="/WEB-INF/jsp/"
    -->

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">  
            <property name="defaultLocale" value="en_US"/>  
    </bean> 
            <bean id="viewResolver"
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"
              p:viewClass="org.springframework.web.servlet.view.JstlView"
              p:prefix="/WEB-INF/jsp/"
              p:suffix=".jsp" />
</beans>

JdbcProductDao.java

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;


public class JdbcProductDao extends SimpleJdbcDaoSupport implements ProductDao {

    protected final Log logger = LogFactory.getLog(getClass()); 

     public List<Product> getProductList() {
        logger.info("Getting products!");
        List<Product> products = getSimpleJdbcTemplate().query(
                "select id, description, price from products", 
                new ProductMapper());
        return products;
    } 



      public void saveProduct(Product prod) {
        logger.info("Saving product: " + prod.getDescription());
        int count = getSimpleJdbcTemplate().update(
            "update products set description = :description, price = :price where id = :id",
            new MapSqlParameterSource().addValue("description", prod.getDescription())
                .addValue("price", prod.getPrice())
                .addValue("id", prod.getId()));
        logger.info("Rows affected: " + count);
    }

      private static class ProductMapper implements ParameterizedRowMapper<Product> { 
        public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
            Product prod = new Product();
            prod.setId(rs.getInt("id"));
            prod.setDescription(rs.getString("description"));
            prod.setPrice(new Double(rs.getDouble("price")));
            return prod;
        } 
    } 
}

SimpleProductManager.java

import java.util.List; 
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SimpleProductManager implements ProductManager { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    private ProductDao productDao;

    public List<Product> getProductDao() {
        //throw new UnsupportedOperationException();
        return productDao.getProductList();
    } 
    public void increasePrice(int percentage) {
        //throw new UnsupportedOperationException();        

            List <Product> products = productDao.getProductList();
            for (Product product : products) {
                double newPrice = product.getPrice() * (100 + percentage)/100;
                product.setPrice(newPrice);
                productDao.saveProduct(product);
            }
    } 

    public void setProductDao(ProductDao productDao) {
        //throw new UnsupportedOperationException();     
        logger.info("inside the setProducts in SimpleProductManager");
        this.productDao = productDao;

    } 
}

HelloController.java

import java.util.Date;
import java.util.Map;
import java.util.HashMap;

import org.springframework.web.servlet.mvc.Controller;

import org.springframework.web.servlet.ModelAndView; 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse; 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory; 

import java.io.IOException;



public class HelloController implements Controller { 

    private ProductManager productManager; 
    protected final Log logger = LogFactory.getLog(getClass()); 

    /*
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException { 

        logger.info("Returning hello view"); 
        String now = ( new Date().toString());
        logger.info("time now:"+ now);

        //return new ModelAndView("hello");


        //return new ModelAndView("WEB-INF/jsp/hello","now",now);

        //decouple the view from the controller
        return new ModelAndView("hello","now",now);
    } 
    */

    //Writing some business logic in the controller

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String now = (new java.util.Date()).toString();

        logger.info("returning hello view with " + now); 
        Map<String, Object> myModel = new HashMap<String, Object>();

        myModel.put("now", now);
        myModel.put("products", this.productManager.getProductDao()); 
        return new ModelAndView("hello", "model", myModel);
    }

    public void setProductManager(ProductManager productManager) 
    {
        this.productManager = productManager;
    } 

}
4

1 に答える 1

-1

hey the problem got resovled, after i had modified, the following..

1) applicationContext with mapping for the bean "productManager" with ref productDao.

2) ProductManager interface with new method call getProducts(), then implemenent in the SimpleProductManager and which call the ProductDao.getProducts(), where the sql query is being defined.

于 2012-12-21T08:42:36.507 に答える