Spring无法通过属性标记注入值

时间:2014-06-17 06:08:27

标签: java spring spring-mvc

我使用简单的setter注入来将值设置为类中的变量 但是当我尝试运行应用程序时,该对象返回null值 可能是什么原因? 以下是文件。

Spring.XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
    "http://www.springframework.org/dtd/spring-beans2.0.dtd">

<beans>
    <bean id = "triangle" class="com.test.Triangle">
        <property name = "type" value = "Equilateral" />
    </bean>
</beans> 

Triangle.java

public class Triangle {

    private String type;

    public String getType() {
        return type;
    }

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

    public void draw() {
        System.out.println(getType() + " Triangle drawn");
    }
}

DrawingApplication.java

package com.test;

public class DrawingApplication {

    public static void main(String[] args) {
        ApplicationContext context =  new ClassPathXmlApplicationContext("Spring.xml");
        Triangle t = (Triangle) context.getBean("triangle");
        t.draw();
    }
}

1 个答案:

答案 0 :(得分:1)

我试图复制问题陈述。这是我的包结构:

enter image description here

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id = "triangle" class="com.test.Triangle">
        <property name = "type" value = "Equilateral" />
    </bean>
</beans>

DrawingApplication.java:

package com.test;

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

public class DrawingApplication {

  public static void main(String[] args) {
    ApplicationContext context =  new ClassPathXmlApplicationContext("Spring.xml");
    Triangle t = (Triangle) context.getBean("triangle");
    t.draw();
  }
}

Triangle.java:

package com.test;
public class Triangle {

  private String type;

  public String getType() {
    return type;
  }

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

  public void draw() {
    System.out.println(getType() + " Triangle drawn");
  }
}

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>Example</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Example</name>
  <url>http://maven.apache.org</url>

  <properties>
    <spring.version>3.0.5.RELEASE</spring.version>
  </properties>

  <dependencies>
    <!-- Spring 3 dependencies -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
  </dependencies>
</project>

我运行了上面的代码并获得了以下输出:

Jun 17, 2014 12:17:21 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@693a317a: startup date [Tue Jun 17 12:17:21 GMT+05:30 2014]; root of context hierarchy
Jun 17, 2014 12:17:21 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Spring.xml]
Jun 17, 2014 12:17:21 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5954864a: defining beans [triangle]; root of factory hierarchy
Equilateral Triangle drawn

我得到了预期的输出,所以看起来classpathspring.xml文件的位置存在一些问题。