java - Spring无法通过属性标签注入(inject)值

标签 java spring spring-mvc

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

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> 

三角形.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();
    }
}

最佳答案

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

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

三角形.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 文件的位置存在一些问题。

关于java - Spring无法通过属性标签注入(inject)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24256766/

相关文章:

java - 当 Java AWT 框架上的元素发生更改时,如何更新它?

java - ActionBarSherlock 库导入后报错

java - 始终在 @RequestMapping 函数之前调用函数

JAVA将字符串CSV文件转换为double[]数组

java - 如何获取给定键的值列表并将该值列表添加到新创建的列表中?

javascript - EL 中 window.location.href 的同义词 - 获取当前 URL

java - @Secured 函数拒绝授权用户访问

java - Spring Boot Web ui html页面错误

java - 如何使用 Spring Security 和 AngularJS 应用程序构建中央标志架构

java - System.getProperty ("user.home") 在 Tomcat 上运行时返回/root