1

我的情况很奇怪。我尝试以这种方式使用@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>

现在,当我通过 main 在 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

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

谢谢。

4

1 回答 1

2

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

尝试这个

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

有几件事要看

  1. 为什么要将类注释CarBean@Configuration?它不包含任何bean定义,即。带@Bean注释的方法。
  2. 为什么要调用createNewCar()方法,因为它已经用 注释了@Bean?(让 Spring 完成它的工作)

检查此链接它将帮助您熟悉@Configuration@Bean注释。

于 2012-07-01T14:32:26.170 回答