java—考虑在配置中定义“com.example.transaction.manager.properties.transactionManagerProperties”类型的bean

6l7fqoea  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(276)

事务管理应用程序

package com.example.transaction.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Profile;

import com.comvia.transaction.manager.properties.TransacationManagerProperties;

@SpringBootApplication
@ComponentScan("com.example")
public class TransactionManagerApplication {
    @Autowired 
    private TransacationManagerProperties yamlFooProperties;

    public static void main(String[] args) {
        SpringApplication.run(TransactionManagerApplication.class, args);
        TransactionManagerApplication manager = new TransactionManagerApplication();
        manager.print();
    }
    public void  print() {
        System.out.println("yamlFooProperties :: "+yamlFooProperties.getPassword());
    }

}

TransactionManager属性

package com.example.transaction.manager.properties;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;

import com.comvia.transaction.manager.properties.factory.YamlPropertySourceFactory;

import lombok.Data;

@Configuration
@PropertySource(value = "transaction_manager.yml", factory = YamlPropertySourceFactory.class)
@Profile("development")
@Data
public class TransacationManagerProperties {

    @Value("${db.userName}")
    private String userName;
    @Value("${db.password}")
    private String password;
    @Value("${list}")
    private List<String> list;

}

启动应用程序时出错:


***************************

APPLICATION FAILED TO START

***************************

Description:

Field yamlFooProperties in com.comvia.transaction.manager.TransactionManagerApplication required a bean of type 'com.example.transaction.manager.properties.TransacationManagerProperties' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.example.transaction.manager.properties.TransacationManagerProperties' in your configuration.
q3qa4bjr

q3qa4bjr1#

从启动应用程序的类中删除bean的自动连接,因为除非这行代码 SpringApplication.run(<SomeClass>.class, args) 跑。
修改方式如下:
更新的transactionmanagerapplication类:

@SpringBootApplication
public class TransactionManagerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TransactionManagerApplication.class, args);
    }

}

创建一个实现commandlinerunner的示例类(commandlinerunner是一个接口,它告诉spring当这个bean出现在springapplication中时应该运行)。简单地说,这将在 SpringApplication.run(<SomeClass>.class, args) 已执行。

@Component
public class TransactionManagerRunner implements CommandLineRunner {

    @Autowired 
    private TransacationManagerProperties yamlFooProperties;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("yamlFooProperties :: "+yamlFooProperties.getPassword());
    }
}

相关问题