Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:23:58+00:00 2026-06-07T01:23:58+00:00

I have weird situation. I tried to create new bean while using the @Configuration

  • 0

I have weird situation.
I tried to create new bean while using the @Configuration an @Bean this way:

package com.spring.beans.ParkingCar;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CarMaker
{
    @Bean
    public CarBean createNewCar()
    {
        CarBean carBean=new CarBean();
        return carBean;

    }

}

package com.spring.beans.ParkingCar;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CarBean
{
    private int x = 3;
    static Logger logger = Logger.getLogger(CarBean.class);


    public void driveCar()
    {
        logger.debug("I am driving my car" + x);
    }
}

and then my test class looks like this:

public static void execute()
    {
        try
        {
            PropertyConfigurator.configure("log4j.properties");

            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


            CarMaker carMaker = (CarMaker) context.getBean("carMaker");
            CarBean carBean = carMaker.createNewCar();
            carBean.driveCar();
        }
        catch (Throwable e)
        {
            logger.error(e);
        }

    }

my applicationContext.xml looks like this:

<?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:context="http://www.springframework.org/schema/context"


    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- Must for auto wiring 
    <context:annotation-config />
    -->

    <context:component-scan
         base-package="com.spring.beans.ParkingCar">
   </context:component-scan>

    <aop:aspectj-autoproxy />

    <bean id="Spring3HelloWorldBean" class="com.spring.aspect.Spring3HelloWorld">
        <property name="myname" value="idan" />
    </bean>

    <bean id="SecurityAspect" class="com.spring.aspect.SecurityAspect">
    </bean>


    <bean id="CalculateStrategyBean" class="com.spring.beans.calculator.CalculateStrategyBean">
        <constructor-arg value="plus" />
    </bean>

    <bean id="CalculatorBean" class="com.spring.beans.calculator.CalculatorBean">
        <constructor-arg ref="CalculateStrategyBean" />
        <constructor-arg ref="CalculateNumbersHolderBean" />
    </bean>

    <bean id="CalculateNumbersHolderBean" class="com.spring.beans.calculator.CalculateNumbersHolderBean">
        <constructor-arg value="10" />
        <constructor-arg value="20" />
    </bean>



    <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" />


    <!--  Wiring without annotations-->
    <!--

        <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" /> <bean
        id="Transit" class="com.spring.beans.ParkingCar.FourWheelsVechile"
        p:lisenceDrive-ref="TransitlisenceDrive" autowire="constructor">
    -->


    <bean id="Ambulance" class="com.spring.beans.ParkingCar.FourWheelsVechile"
        p:modelName="GMC" p:numOfWheels="4" p:year="1997" p:lisenceDrive-ref="AmbulancelisenceDrive" />

    <!--  Wiring with annotations

    <bean id="Bike" class="com.spring.beans.ParkingCar.TwoWheelsVechile"
        autowire="byName" p:modelName="T-BIRD" p:numOfWheels="2" p:year="2012" />



    <bean id="Transit" class="com.spring.beans.ParkingCar.FourWheelsVechile"
        p:lisenceDrive-ref="TransitlisenceDrive">
    </bean>
    -->

</beans>

Now when I ran it on MyEclipse thru main it worked just fine. but when I uploaded it to my stand alone application which is running on Linux I got:

2012-07-01 14:11:21,152 com.spring.test.Spring3HelloWorldTest [ERROR] org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'carMaker' is defined

Any idea why it didn’t work there?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:24:01+00:00Added an answer on June 7, 2026 at 1:24 am

    @Configuration indicates that a class declares one or more @Bean methods while @Bean creates a new bean which have same name as the name of the method annotated with @Bean .
    In your code sample bean with name createNewCar will be created as method createNewCar() method is annotated by @Bean.

    Try this

    CarBean carBean = (CarBean) context.getBean("createNewCar");
    carBean.driveCar();   
    

    Few things to look at

    1. Why to annotate class CarBean as @Configuration? It dont contain any bean defination ie. method with @Bean annotation.
    2. Why to invoke createNewCar() method as it is already annotated with @Bean? (Let Spring do its work)

    Check this link it will help you to get familiar with @Configuration and @Bean annotation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have encountered a weird situation while updating/upgrading some legacy code. I have a
Hi this may seem like a weird question, but here's my situation: I have
I have this weird situation. I have these two classes: Public Class Entry End
I have a really weird situation, I am calling my javascript function like this...
have a weird situation. I'm using Glassfish server for my Enterprise application. In that
I have a weird situation. A client of mine has a website having this
I am working on a program built in MFC. I have this weird situation
I have a weird situation. I have a dict, self.containing_dict . Using the debug
I have this weird situation where my form doesn't send any data, but the
I have a weird situation here, i have the following grid: @(Html.Telerik().Grid(this.Model). Name(grdEscolas). Groupable().

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.