簡體   English   中英

Spring無法通過屬性標簽注入值

[英]Spring not able to inject values through property tag

我正在使用簡單的setter注入將值設置為類中的變量,但是當我嘗試運行該應用程序時,該對象將返回空值,這可能是什么原因? 以下是文件。

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

我試圖復制問題陳述。 這是我的包結構:

在此處輸入圖片說明

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文件的位置存在spring.xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM