Announcement Announcement Module
Collapse
No announcement yet.
Postconstruct and PreDestroy problems Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Postconstruct and PreDestroy problems

    hi,

    i'm trying to use @PostConstruct and @PreDestroy annotations in my class

    i tried to register CommonAnnotationBeanPostProcessor but the @PostConstruct and the @PreDestroy annotated methods aren't invoked in the specified bean

    so i switched to <annotation-config/> tag but didn't work either

    so i switched to <bean init-method="" destroy-method=""/> where the prost construction method is invoked but the pre destroy didnt

    can you tell me what could be the problem?

    here is the code:

    Product class
    Code:
    
    package springtest;
    
    public class Product {
    
        private String name;
        private float price;
    
        public Product() {
            name="";
            price=0.0f;
        }
    
        public Product(String name, String price) {
            this.name = name;
            this.price = Float.parseFloat(price);
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public float getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = Float.parseFloat(price);
        }
    
    
        @Override
        public String toString(){
            return name;
        }
    
    }
    ShoppingCart class
    Code:
    
    package springtest;
    
    import java.util.List;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    
    public class ShoppingCart {
    
        private List<Product> items;
    
        public void setItems(List<Product> items){
            this.items=items;
        }
    
    
        public void addItem(Product item){
            items.add(item);
        }
    
        public List<Product> getItems(){
            return items;
        }
    
        @PreDestroy
        public void  eliminate(){
            System.out.println("Before Bean Destruction");
        }
    
        @PostConstruct
        public void construct(){
            System.out.println("After Bean Initialization");
        }
    }

    Main class
    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package springtest;
    
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;
    
    /**
     *
     * @author User
     */
    public class Main {
    
        
        public static void main(String[] args) {
    
            Resource rc=new ClassPathResource("beans.xml");
            BeanFactory factory=new XmlBeanFactory(rc);
    
            ShoppingCart cart1=(ShoppingCart)factory.getBean("shoppingCart");
            //ShoppingCart cart2=(ShoppingCart)factory.getBean("shoppingCart");
    
            //cart2.addItem(new Product("T-Shirt", "20.99"));
            System.out.println("Cart1: "+cart1.getItems());
            //System.out.println("Cart2: "+cart2.getItems());
    
    
    
        }
    
    }
    Beans.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!--
        Document   : beans.xml
        Created on : November 2, 2009, 10:36 PM
        Author     : User
        Description:
            Purpose of the document follows.
    -->
    
    <beans  xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
            xmlns:util='http://www.springframework.org/schema/util'
            xmlns:context='http://www.springframework.org/schema/context'
            xmlns='http://www.springframework.org/schema/beans'
            xsi:schemaLocation='http://www.springframework.org/schema/util spring-util-2.5.xsd
                                http://www.springframework.org/schema/context spring-context-2.5.xsd
                                http://www.springframework.org/schema/beans spring-beans-2.5.xsd' >
        <description>Apress Spring Recipes Tests</description>
    
      <!--<context:annotation-config/>-->
        <util:list list-class="java.util.ArrayList" id="cartItems" scope="prototype">
           <ref local="aaa"/>
           <ref local="cdr"/>
           <ref local="cdrw"/>
           <ref local="dvd"/>
           <ref local="dvdrw"/>
        </util:list>
    
        <bean id="aaa" class="springtest.Product">
            <constructor-arg index="0" value="BATTERY"/>
            <constructor-arg index="1" value="1.5"/>
        </bean>
    
        <bean id="cdr" class="springtest.Product">
            <constructor-arg index="0" value="CD-R"/>
            <constructor-arg index="1" value="7.5"/>
        </bean>
    
        <bean id="cdrw" class="springtest.Product">
            <constructor-arg index="0" value="CD-RW"/>
            <constructor-arg index="1" value="10.8"/>
        </bean>
    
        <bean id="dvd" class="springtest.Product">
            <constructor-arg index="0" value="DVD"/>
            <constructor-arg index="1" value="15.99"/>
        </bean>
    
        <bean id="dvdrw" class="springtest.Product">
            <constructor-arg index="0" value="DVD-RW"/>
            <constructor-arg index="1" value="29.13"/>
        </bean>
    
        <bean id="shoppingCart" class="springtest.ShoppingCart" scope="prototype" ><!-- init-method="construct" destroy-method="eliminate" >-->
            <property name="items" ref="cartItems"/>
        </bean>
    
        <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
        
        
    </beans>


    THANX
    Last edited by katatonia911; Nov 2nd, 2009, 05:58 PM.

  • #2
    Use an ApplicationContext and not a BeanFactory.

    Next time use the search as this question has been answered numerous times before.

    Comment


    • #3
      Hi Marten,

      Just curious if there is a reference to this in the documentation?

      Cheers

      Comment


      • #4
        Chapter 3 explains the differences.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          Use an ApplicationContext and not a BeanFactory.

          Next time use the search as this question has been answered numerous times before.
          ok...i though im free to ask questions..this is my third time to use the forum..im not a senior member or site admin to know that.

          anyway thanx for your answer but it only solved the PostConstruct method...the PreDestroy is still not invoked
          Last edited by katatonia911; Nov 3rd, 2009, 06:05 PM.

          Comment


          • #6
            yep. the predestory seems not working with scope prototype beans, any alternative ways?

            Comment

            Working...
            X