简体   繁体   English

弹簧-无法自动装配豆

[英]spring--Cannot autowire bean

I'm unable to autowire the bean and while running i'm getting output as: 我无法自动装配Bean,并且在运行时得到的输出为:

Jul 28, 2013 1:21:42 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17aece8: startup date [Sun Jul 28 13:21:42 CDT 2013]; root of context hierarchy
Jul 28, 2013 1:21:44 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Jul 28, 2013 1:21:50 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1dc696e: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,prod,type,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy


Id is: 1 Name is: LED Type is: null

Here is my code: 这是我的代码:

package org.autowire;

import org.springframework.beans.factory.annotation.Autowired;
import org.autowire.action1.Type;

public class Product {
    private int id;
    private String name;

    private Type type;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    @Autowired
    public void setName(String name) {
        this.name = name;
    }


    public void display(){
        System.out.println("Id is: "+ id +" Name is: " + name +" Type is: " +type);
    }


}

Type.java Type.java

package org.autowire.action1;

public class Type {

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

spring.xml spring.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: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">

     <!-- Add your classes base package here -->          
     <context:component-scan base-package="org.autowire.Product"/>

    <bean id="prod" class="org.autowire.Product" >
        <property name="id" value="1"></property>
        <property name="name" value="LED"></property>
    </bean>


    <bean id="type" class="org.autowire.action1.Type">
        <property name="type" value="import"></property>
    </bean>



</beans>

TestApp: TestApp:

package org.autowire.action1;

import org.autowire.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApp {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        Product product=(Product)context.getBean("prod");
        product.display();
    }

}

Thanks in advance!!! 提前致谢!!!

You need the @Autowired annotation on your type attribute, not the name one: 您需要在type属性上使用@Autowired批注,而不是名称1:

public class Product {
    private int id;
    private String name;
    @Autowired
    private Type type;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public void display(){
        System.out.println("Id is: "+ id +" Name is: " + name +" Type is: " +type);
    }

}

It's not a bad idea to have setters/getters for Type as well, if only for consistency. 如果只是为了保持一致性,也为Type提供setters / getters并不是一个坏主意。

If you are new to Spring IOC, try to define bean configurations from xml file. 如果您不熟悉Spring IOC,请尝试从xml文件定义bean配置。 It will be easier to understand. 这将更容易理解。 The IoC container IoC容器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM