首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何使用@Component和@Value原型传递引用?

如何使用@Component和@Value原型传递引用?
EN

Stack Overflow用户
提问于 2022-09-08 09:51:54
回答 1查看 38关注 0票数 0

下面我添加了一些类,学生类取决于地址类。对象创建了如何使用@Component和@Value添加内部地址的'ad‘值。

学生班

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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;
        } 
        }

地址类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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 + "]";
        }   
    }

主班

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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"/>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-08 11:25:30

我认为您误解了Spring和Java中软件包的使用。com.spring.stereotype包是来自Spring框架的内部包。您不应该使用这样的包作为您自己的项目的基础包。特别是,当您使用Spring中的类并且在类路径上有这个框架时。

此外,您不应该在2022年将XML用于应用程序上下文配置。多年来一直不受欢迎。它可能用于遗留项目,但不应该用于学习项目和创建新的应用程序。

你应该做的是:

  1. com.spring.stereotype包替换为您自己的包。例如,com.example
  2. 了解Spring Boot及其如何用于配置应用程序上下文。通过遵循指定的链接,您将学习一些教程。

您的情况下的工作应用程序示例:

pom.xml文件:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<?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方法来分配测试地址:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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 + "]";
    }
}

主修班:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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());
    }
}

应用程序运行日志:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73653245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文