下面我添加了一些类,学生类取决于地址类。对象创建了如何使用@Component和@Value添加内部地址的'ad‘值。
学生班
package com.spring.stereotype;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("obj")
public class Student {
private Address ad;
public Address getAd() {
return ad;
}
public void setAd(Address ad) {
this.ad = ad;
}
}
地址类
package com.spring.stereotype;
public class Address {
private String ad;
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
@Override
public String toString() {
return "Address [ad=" + ad + "]";
}
}
主班
package com.spring.stereotype;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("com/spring/stereotype/stereo.xml");
Student st = context.getBean("obj",Student.class);
System.out.println(st.getAd());
}
}
如果我添加XML文件,将显示一个错误,在这里我键入了如何创建对象<context:component-scan base-package="com.spring.stereotype"/>
发布于 2022-09-08 11:25:30
我认为您误解了Spring和Java中软件包的使用。com.spring.stereotype
包是来自Spring框架的内部包。您不应该使用这样的包作为您自己的项目的基础包。特别是,当您使用Spring中的类并且在类路径上有这个框架时。
此外,您不应该在2022年将XML用于应用程序上下文配置。多年来一直不受欢迎。它可能用于遗留项目,但不应该用于学习项目和创建新的应用程序。
你应该做的是:
com.spring.stereotype
包替换为您自己的包。例如,com.example
。您的情况下的工作应用程序示例:
pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demotest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demotest</name>
<description>demotest</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Student
豆。我还添加了@PostConstruct
方法来分配测试地址:
package com.example.demotest;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component("student")
public class Student {
@PostConstruct
public void init() {
this.setAd("test");
}
private String ad;
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
@Override
public String toString() {
return "Address [ad=" + ad + "]";
}
}
主修班:
package com.example.demotest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class DemotestApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemotestApplication.class, args);
Student st = context.getBean("student", Student.class);
System.out.printf("\nStudent address: %s", st.getAd());
}
}
应用程序运行日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.3)
2022-09-08 22:33:59.954 INFO 44414 --- [ main] c.example.demotest.DemotestApplication : Starting DemotestApplication using Java 17.0.2
2022-09-08 22:33:59.955 INFO 44414 --- [ main] c.example.demotest.DemotestApplication : No active profile set, falling back to 1 default profile: "default"
2022-09-08 22:34:00.205 INFO 44414 --- [ main] c.example.demotest.DemotestApplication : Started DemotestApplication in 0.382 seconds (JVM running for 0.605)
Student address: test
Process finished with exit code 0
https://stackoverflow.com/questions/73653245
复制相似问题