Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring自动关联自动关联bean中的属性_Java_Spring_Spring Boot - Fatal编程技术网

Java Spring自动关联自动关联bean中的属性

Java Spring自动关联自动关联bean中的属性,java,spring,spring-boot,Java,Spring,Spring Boot,我是春天的新手。我的弹簧靴有问题。我正在尝试将一个字段从外部配置文件自动关联到一个自动关联bean中。我有以下课程 App.java public class App { @Autowired private Service service; public static void main(String[] args) { final SpringApplication app = new SpringApplication(App.class); //app.setSho

我是春天的新手。我的弹簧靴有问题。我正在尝试将一个字段从外部配置文件自动关联到一个自动关联bean中。我有以下课程

App.java

public class App {

@Autowired
private Service service;

public static void main(String[] args) {

    final SpringApplication app = new SpringApplication(App.class);
    //app.setShowBanner(false);
    app.run();
}

@PostConstruct
public void foo() {
    System.out.println("Instantiated service name = " + service.serviceName);
}
}
AppConfig.java

@Configuration
@ConfigurationProperties
public class AppConfig {

@Bean
public Service service()    {
    return new Service1();
}
}
服务接口

public interface Service {
    public String serviceName ="";
    public void getHistory(int days , Location location );
    public void getForecast(int days , Location location );
}
服务1

@Configurable
@ConfigurationProperties
public class Service1 implements Service {

@Autowired
@Value("${serviceName}") 
public String serviceName;
//Available in external configuration file.
//This autowiring is not reflected in the main method of the application.



public void getHistory(int days , Location location)
{
    //history code
}

public void getForecast(int days , Location location )
{
    //forecast code
}
}

我无法在App类的postconstruct方法中显示service name变量。我这样做对吗?

考虑到您的class
App
带有
@SpringBootApplication
App
类的注释,您可以将
serviceName
放在
应用程序中。属性
并使用
@Value(${serviceName}”)
注入它。不要在类上使用
@Component
,如果您已经在配置上使用
@Bean
,它将发生冲突,因此
@Autowired
@Value

有关更多信息,请参阅

你会以这样的方式结束

@Service // @Component specialization
public class Service1 implements Service {

@Value("${serviceName}") 
public String serviceName;
//Available in external configuration file.
//This autowiring is not reflected in the main method of the application.



public void getHistory(int days , Location location)
{
    //history code
}

public void getForecast(int days , Location location )
{
    //forecast code
}
}
当您有@Component/@Service/@Repository时,不需要@Bean声明

@Configuration
public class AppConfig {  //other stuff here not duplicated beans  }
你的主课呢

    package com.app;

    @SpringBootApplication // contains @EnableAutoConfiguration @ComponentScan @Configuration   
    public class App {

    @Autowired
    private Service service;

    public static void main(String[] args) {

        final SpringApplication app = new SpringApplication(App.class);
        //app.setShowBanner(false);
        app.run();
    }

    @PostConstruct
    public void foo() {
        System.out.println("Instantiated service name = " + service.serviceName);
    }
    }

您可以通过不同的方式加载属性:

想象一下下面的application.properties由spring引导自动加载

spring.app.serviceName=Boot demo
spring.app.version=1.0.0
  • 使用@Value注入值

    @Service
    public class ServiceImpl implements Service {
    
    @Value("${spring.app.serviceName}") 
    public String serviceName;
    
    }
    
  • 使用@ConfigurationProperties注入值

    @ConfigurationProperties(prefix="spring.app")
    public class ApplicationProperties {
    
       private String serviceName;
    
       private String version;
    
       //setters and getters
    }
    
  • 您可以使用
    @Autowired
    从另一个类访问此属性

    @Service
    public class ServiceImpl implements Service {
    
    @Autowired
    public ApplicationProperties applicationProperties;
    
    }
    
    正如您所注意到的,前缀将是
    spring.app
    ,然后spring引导将属性前缀与之匹配,并查找
    serviceName
    version
    ,然后将注入值