Results 1 to 3 of 3

Thread: problem with a constructor called ./RentABike-context.xml

  1. #1
    Join Date
    Nov 2011
    Posts
    2

    Default problem with a constructor which is called from ./RentABike-context.xml

    Could you tell me how to execute a constructor
    public ArrayListRentABike(String storeName)
    instead of public ArrayListRentABike()

    I have a line
    <property name="storeName"><value>BruceBikes</value></property>
    so why a constructor without parameters is executed?




    java -cp .:../war/WEB-INF/lib/dist/*:../war/WEB-INF/lib/dist/commons-logging-1.1.1/commons-logging-1.1.1.jar com/springbook/RentABikeAssembler
    lis 14, 2011 7:01:07 AM org.springframework.context.support.AbstractApplic ationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlAp plicationContext@fde0d5: display name [org.springframework.context.support.ClassPathXmlAp plicationContext@fde0d5]; startup date [Mon Nov 14 07:01:07 CET 2011]; root of context hierarchy
    lis 14, 2011 7:01:07 AM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [RentABike-context.xml]
    lis 14, 2011 7:01:08 AM org.springframework.context.support.AbstractApplic ationContext obtainFreshBeanFactory
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlAp plicationContext@fde0d5]: org.springframework.beans.factory.support.DefaultL istableBeanFactory@1347747
    lis 14, 2011 7:01:08 AM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL istableBeanFactory@1347747: defining beans [rentaBike,commandLineView]; root of factory hierarchy
    hello worldcom.springbook.RentABike: BruceBikes
    com.springbook.Bike : manufacturer -- 11Shimano
    : model -- Roadmaster
    : frame -- 20
    : serialNo -- 11111
    : weight -- 15.0
    : status -- Fair.

    com.springbook.Bike : manufacturer -- 11Cannondale
    : model -- F2000 XTR
    : frame -- 18
    : serialNo -- 22222
    : weight -- 12.0
    : status -- Excellent.

    com.springbook.Bike : manufacturer -- 11Trek
    : model -- 6000
    : frame -- 19
    : serialNo -- 33333
    : weight -- 12.4
    : status -- Fair.



    My project from a book about Spring:

    ./RentABike-context.xml
    ========================
    <?xml version="1.0"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
    <bean id="rentaBike" class="com.springbook.ArrayListRentABike">
    <property name="storeName"><value>BruceBikes</value></property>
    </bean>

    <bean id="commandLineView" class="com.springbook.CommandLineView">
    <property name="rentaBike"><ref bean="rentaBike"/></property>
    </bean>

    ========================





    ./com/springbook/ArrayListRentABike.java
    ==============================
    package com.springbook;

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Iterator;

    public class ArrayListRentABike implements RentABike {
    private String storeName;
    final List bikes = new ArrayList();

    public ArrayListRentABike() {
    bikes.add(new Bike("11Shimano", "Roadmaster", 20, "11111", 15,
    "Fair"));
    bikes.add(new Bike("11Cannondale", "F2000 XTR", 18, "22222",12,
    "Excellent"));
    bikes.add(new Bike("11Trek","6000", 19, "33333", 12.4,
    "Fair"));
    }

    public void setStoreName(String name) {
    this.storeName = name;
    }

    public String getStoreName() {
    return storeName;
    }

    public ArrayListRentABike(String storeName) {
    this.storeName = storeName;
    }

    public String toString() { return "com.springbook.RentABike: " + storeName; }

    public List getBikes() { return bikes; }

    public Bike getBike(String serialNo) {
    Iterator iter = bikes.iterator();
    while(iter.hasNext()) {
    Bike bike = (Bike)iter.next();
    if(serialNo.equals(bike.getSerialNo())) return bike;

    }

    return null;
    }
    }

    ==============================




    ./com/springbook/RentABike.java
    =============================
    package com.springbook;

    import java.util.List;

    public interface RentABike {
    List getBikes();
    Bike getBike(String serialNo);
    void setStoreName(String name);
    String getStoreName();
    }

    =============================



    ./com/springbook/RentABikeAssembler.java
    =============================
    package com.springbook;

    import org.springframework.context.support.ClassPathXmlAp plicationContext;


    public class RentABikeAssembler {
    public static final void main(String[] args) {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("RentABike-context.xml");
    CommandLineView clv = (CommandLineView)ctx.getBean("commandLineView");
    clv.printAllBikes();
    }
    }
    ==============================



    ./com/springbook/Bike.java
    ===================
    package com.springbook;

    public class Bike {
    private String manufacturer;
    private String model;
    private int frame;
    private String serialNo;
    private double weight;
    private String status;

    public Bike(String manufacturer, String model, int frame, String serialNo, double weight, String status) {
    this.manufacturer = manufacturer;
    this.model = model;
    this.frame = frame;
    this.serialNo = serialNo;
    this.weight = weight;
    this.status = status;
    }

    public String toString() {
    return "com.springbook.Bike : " +
    "manufacturer -- " + manufacturer +
    "\n: model -- " + model +
    "\n: frame -- " + frame +
    "\n: serialNo -- " + serialNo +
    "\n: weight -- " + weight +
    "\n: status -- " + status +
    ".\n"; }

    public String getManufacturer() { return manufacturer; }

    public void setManufacturer(String manufacturer) {
    this.manufacturer = manufacturer;
    }

    public String getModel() { return model; }

    public void setModel(String model) { this.model = model; }

    public int getFrame() { return frame; }

    public void setFrame(int frame) { this.frame = frame; }

    public String getSerialNo() { return serialNo; }

    public void setSerialNo(String serialNo) { this.serialNo = serialNo; }

    public double getWeight() { return weight; }

    public void setWeight(double weight) { this.weight = weight; }

    public String getStatus() { return status; }

    public void setStatus(String status) { this.status = status; }

    }
    ======================







    ./com/springbook/CommandLineView.java
    ============
    package com.springbook;

    import com.springbook.Bike;

    import java.util.Iterator;


    public class CommandLineView {
    private RentABike rentaBike;
    public CommandLineView() {}

    public void setRentaBike(RentABike rentaBike) {
    this.rentaBike = rentaBike;
    }

    public RentABike getRentaBike() { return this.rentaBike; }

    public void printAllBikes() {
    System.out.println("hello world" + rentaBike.toString());
    Iterator iter = rentaBike.getBikes().iterator();
    while(iter.hasNext()) {
    Bike bike = (Bike)iter.next();
    System.out.println(bike.toString());
    }
    }
    }
    ===========
    Last edited by juyer; Nov 14th, 2011 at 01:22 AM. Reason: error in title

  2. #2
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    So <property> tags are to call the setter methods style of Dependency Injection. For constructor, the tag is <constructor-arg>

    So, if you want the constructor to be called then use the <constructor-arg> tag instead.

    Good Luck

    Mark

  3. #3
    Join Date
    Nov 2011
    Posts
    2

    Default

    Thank you bytor99999
    Amazing, that works

    <!-- <property name="storeName"><value>BruceBikes</value></property>
    -->

    <constructor-arg index="0" type="java.lang.String" value="BruceBikesyeahhhh"/>

Posting Permissions

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