Results 1 to 2 of 2

Thread: Error while trying to initiate object with DI by non default constructor

  1. #1

    Default Error while trying to initiate object with DI by non default constructor

    Hi All,


    I dont understand why. I made everything by the docs in order to init my class by non-default constructor

    thats my code:


    Code:
    public abstract class Vechile implements VechileInterface
    {
    	protected int numOfWheels;
    	protected String modelName;
    	protected String year;
    	protected LisenceDrive lisenceDrive;
    
    	static Logger logger = Logger.getLogger(Spring3HelloWorldTest.class);
    
    	public Vechile(int numOfWheels, String modelName, String year)
    	{
    		this.numOfWheels = numOfWheels;
    		this.modelName = modelName;
    		this.year = year;
    	}
    	
    
    	public Vechile(VechileDetails vechileDetails)
    	{
    		this.numOfWheels = vechileDetails.getNumOfWheels();
    		this.modelName = vechileDetails.getModelName();
    		this.year = vechileDetails.getYear();
    	}
    
    	public Vechile()
    	{
    		int z = 0;
    	}
    
    	public int getNumOfWheels()
    	{
    		return numOfWheels;
    	}
    
    	public void setNumOfWheels(int numOfWheels)
    	{
    		this.numOfWheels = numOfWheels;
    	}
    
    	public String getModelName()
    	{
    		return modelName;
    	}
    
    	public void setModelName(String modelName)
    	{
    		this.modelName = modelName;
    	}
    
    	public String getYear()
    	{
    		return year;
    	}
    
    	public void setYear(String year)
    	{
    		this.year = year;
    	}
    
    	public LisenceDrive getLisenceDrive()
    	{
    		return lisenceDrive;
    	}
    
    	public void setLisenceDrive(LisenceDrive lisenceDrive)
    	{
    		this.lisenceDrive = lisenceDrive;
    	}
    
    	@Override
    	public String toString()
    	{
    		return "Vechile [modelName=" + modelName + ", numOfWheels=" + numOfWheels + ", year=" + year + "]";
    	}
    
    }

    Code:
    package com.spring.beans.ParkingCar;
    
    public class FourWheelsVechile extends Vechile implements VechileInterface
    {
    	
    	@Override
    	public void drive()
    	{
    		logger.debug("I am driving an FourWheelsVechile and my details:" + this.toString() + " and my lisence details:"
    				+ this.getLisenceDrive().toString());
    
    	}
    }
    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"
    
    
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    	<aop:aspectj-autoproxy />
    
    	
    	<bean id="lisenceDrive" class="com.spring.beans.ParkingCar.LisenceDrive"
    		p:carLisenceNum="333" p:isValidateCar="true" />
    
    	<bean id="AmbulancelisenceDrive" class="com.spring.beans.ParkingCar.LisenceDrive"
    		p:carLisenceNum="999" p:isValidateCar="false" />
    
    	<bean id="TransitlisenceDrive" class="com.spring.beans.ParkingCar.LisenceDrive"
    		p:carLisenceNum="111" p:isValidateCar="false" />
    
    
    	<bean id="TransitVechileDetails" class="com.spring.beans.ParkingCar.VechileDetails"
    		p:modelName="Transit-AS" p:numOfWheels="4" p:year="1992" />
    
    
    
    
    	<bean id="Ambulance" class="com.spring.beans.ParkingCar.FourWheelsVechile"
    		p:modelName="GMC" p:numOfWheels="4" p:year="1997" p:lisenceDrive-ref="AmbulancelisenceDrive" />
    
    	<bean id="Bike" class="com.spring.beans.ParkingCar.TwoWheelsVechile"
    		autowire="byName" p:modelName="T-BIRD" p:numOfWheels="2" p:year="2012" />
    
    
    </beans>

    Code:
    package com.spring.beans.ParkingCar;
    
    public interface VechileInterface
    {
    	public void drive();
    
    }
    test class:


    Code:
    package com.spring.test;
    
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.spring.aspect.Spring3HelloWorld;
    import com.spring.beans.ParkingCar.FourWheelsVechile;
    import com.spring.beans.ParkingCar.TwoWheelsVechile;
    import com.spring.beans.ParkingCar.Vechile;
    import com.spring.beans.calculator.CalculateNumbersHolderBean;
    import com.spring.beans.calculator.CalculateStrategyBean;
    import com.spring.beans.calculator.CalculatorBean;
    
    public class Spring3HelloWorldTest
    {
    
    	static Logger logger = Logger.getLogger(Spring3HelloWorldTest.class);
    
    	public static void main(String[] args)
    	{
    
    		execute();
    
    	}
    
    	private static void execute()
    	{
    		PropertyConfigurator.configure("log4j.properties");
    		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    
    Vechile twoWheelsVechile = (TwoWheelsVechile) context.getBean("Bike");
    		twoWheelsVechile.drive();
    		Vechile fourWheelsVechile = (FourWheelsVechile) context.getBean("Ambulance");
    		fourWheelsVechile.drive();	
    		fourWheelsVechile = (FourWheelsVechile) context.getBean("Transit");
    		fourWheelsVechile.drive();
    			
    	}
    
    }
    error msg:

    I am getting this error:

    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'Transit' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:509)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedLocalBeanDefinition(AbstractB eanFactory.java:1041)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:273 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:189)
    at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:1044)
    at com.spring.test.Spring3HelloWorldTest.execute(Spri ng3HelloWorldTest.java:60)
    at com.spring.test.Spring3HelloWorldTest.main(Spring3 HelloWorldTest.java:24)


    thanks,
    ray.

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    As the error says, you have no bean named "Transit" in your app context, or, at least, you don't in the xml you posted.

Tags for this Thread

Posting Permissions

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