使用@Configuration时出现NoSuchBeanDefinitionException

时间:2012-07-01 14:25:23

标签: java spring spring-annotations

我有奇怪的情况。 我尝试以这种方式使用@Configuration和@Bean创建新bean:

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;

    }

}

包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);
    }
}

然后我的测试类看起来像这样:

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);
        }

    }

我的applicationContext.xml如下所示:

<?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>

现在当我在MyEclipse上运行它直到主要它工作得很好。但当我将它上传到我在Linux上运行的独立应用程序时,我得到了:

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

知道为什么它不起作用吗?

感谢。

1 个答案:

答案 0 :(得分:2)

@Configuration表示一个类声明一个或多个@Bean方法,而@Bean创建一个新bean,其名称与使用@Bean注释的方法的名称相同。
在您的代码中,将创建名为createNewCar的示例bean,方法createNewCar()方法由@Bean注释。

试试这个

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

很少看

  1. 为什么要将课程CarBean注释为@Configuration?它不包含任何豆类定义,即。带有@Bean注释的方法。
  2. 为什么要调用createNewCar()方法,因为它已经使用@Bean进行了注释? (让Spring做它的工作)
  3. 选中此link,它可以帮助您熟悉@Configuration@Bean注释。