![](/img/trans.png)
[英]Consider defining a bean of type 'com.example.conexion.repository.ClienteRepository' in your configuration
[英]Consider defining a bean of type 'com.example.transaction.manager.properties.TransacationManagerProperties' in your configuration
事務管理器應用程序
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());
}
}
事務管理器屬性
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.
從您啟動應用程序的 class 中刪除 bean 的自動裝配,因為除非此代碼行SpringApplication.run(<SomeClass>.class, args)
運行,否則不會注冊 bean。
用下面給出的這種方式修改:
更新了 TransactionManagerApplication class:
@SpringBootApplication
public class TransactionManagerApplication {
public static void main(String[] args) {
SpringApplication.run(TransactionManagerApplication.class, args);
}
}
創建一個示例 class 實現 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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.