Results 1 to 4 of 4
  1. #1
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default database connection Spring framework unresolved

    i had earlier created the spring framework, then replaced with the database connection, but there is problem in creating the beans.

    also receiving the below error during the deployment.

    Error


    deploy?DEFAULT=C:\Users\gopc\Documents\NetBeansPro jects\HelloSpringJDBC\build\web&name=HelloSpringJD BC&contextroot=/HelloSpringJDBC&force=true failed on GlassFish Server 3.1.2 Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'productManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'productDao' of bean class [SimpleProductManager]: Bean property 'productDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?. Please see server.log for more details. C:\Users\gopc\Documents\NetBeansProjects\HelloSpri ngJDBC\nbproject\build-impl.xml:1029: The module has not been deployed. See the server log for details.

    Source

    applicationContext.xml

    applicationContext.xml

    XML Code:
        <?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 Code:
    <?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

    Java Code:
    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

    Java Code:
    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

    Java Code:
    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;
        } 
    
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: database connection Spring framework unresolved

    The error message has said exactly what your problem was. You have defined an incompatible type for your getter and setter to ProductDao in your SimpleProductManager class.
    As you can see from the following snippet taken out from your code above:

    Java Code:
    public List<Product> getProductDao() {
    ...
    }
    
    public void setProductDao(ProductDao productDao) {
    ...
    }

  3. #3
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: database connection Spring framework unresolved

    thanks, i had changed the getter method now, but still getting the same error...

    error
    ----
    Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'productManager' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError. Please see server.log for more details.



    also i had modified the Controller and springapp-servlet.xml as below..

    springapp-servlet.xml
    ---------------------------

    XML Code:
    <bean name="/hello.htm" class="HelloController"> 
    <property name="simpleProductManager" ref="simpleProductManager"/> 
    </bean>
    HelloController.java
    -------------------------


    Java Code:
    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.simpleProductManager.getProductDao());   
            return new ModelAndView("hello", "model", myModel);  
        }  
                 
        public void setProductManager(SimpleProductManager simpleProductManager)   
        {  
            this.simpleProductManager = simpleProductManager;  
        }

  4. #4
    cgk_js is offline Member
    Join Date
    Oct 2012
    Posts
    21
    Rep Power
    0

    Default Re: database connection Spring framework unresolved

    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.

Similar Threads

  1. Spring Framework
    By NoobSaibot in forum New To Java
    Replies: 2
    Last Post: 10-10-2011, 06:47 PM
  2. Replies: 0
    Last Post: 04-01-2008, 10:44 AM
  3. How to update database using Spring framework
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 04-01-2008, 10:43 AM
  4. Replies: 0
    Last Post: 10-04-2007, 09:15 PM
  5. How to update database using Spring framework
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-28-2007, 01:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •