gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:44:05 25 4
gpt4 key购买 nike

我正在使用简单的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com