简体   繁体   English

Spring启动和配置文件

[英]Spring boot and configuration files

I am learning Spring, and in particular Spring Boot. 我正在学习Spring,尤其是Spring Boot。

I am trying to do some basic injection, but I'm failing to do a basic one. 我正在尝试进行一些基本的注射,但是我没有进行基本的注射。 It is probably a quite stupid error, but I can't figure it out - might be because I just woke up :) 这可能是一个非常愚蠢的错误,但我无法弄清楚-可能是因为我刚刚醒来了:)

This is my application class 这是我的应用程序类

package it.myapp.console.spring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import it.myapp.console.menus.MainMenu;

@SpringBootApplication
@EnableAutoConfiguration
public class Application {

   public static void main(String[] args) {
      SpringApplication app = new SpringApplication(MainMenu.class);

      app.run(args);
   }
}

This is the MainMenu class 这是MainMenu类

package it.myapp.console.menus;

import javax.annotation.Resource;

import org.springframework.boot.CommandLineRunner;

import it.myapp.console.test.TestBean;


public class MainMenu implements CommandLineRunner {

   @Resource
   private TestBean testBean;

   public void run(String... strings) throws Exception {

      System.out.println(testBean.doTest());
   }
}

This is my configuration class 这是我的配置类

package it.myapp.console.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import it.myapp.console.test.TestBean;
import it.myapp.console.test.TestBeanImpl;

@Configuration
public class ConsoleSpringConfig {

   @Bean
   public TestBean getTest() {
      return new TestBeanImpl();
   }
}

TestBean and TestBeanImpl are quite trivial TestBean和TestBeanImpl非常琐碎

package it.myapp.console.test;

public interface TestBean {
   String doTest();
}

and

package it.myapp.console.test;

public class TestBeanImpl implements TestBean {
   public String doTest() {
      return "aaa";
   }
}

What I receive is a really sad 我收到的是一个非常难过的

***************************
APPLICATION FAILED TO START
***************************

Description:

A component required a bean of type '...TestBean' that could not be found.


Action:

Consider defining a bean of type '...TestBean' in your configuration.

I swear I've seen quite a few guides, they seem to do everything automagically but I am apparently missing the last bit. 我发誓我已经看过很多指南,它们似乎可以自动完成所有工作,但我显然遗漏了最后一点。

I've tried to launch the application with the --debug option, but it doesn't mention the ConsoleSpringConfig class in any way, so I suppose I'm missing some way to declare that config should be found there. 我尝试使用--debug选项启动该应用程序,但是它没有以任何方式提及ConsoleSpringConfig类,因此我想我缺少某种声明应该在此处找到配置的方法。

Thanks for your help! 谢谢你的帮助! Lorenzo 洛伦佐

By default, the @SpringBootApplication allows to trigger auto-configuration and component scanning for the package where the class declaring this annotation is used. 默认情况下, @SpringBootApplication允许触发使用声明此注释的类的包的自动配置和组件扫描。
Of course, you can change it by specifying the scanBasePackages attribute of the annotation. 当然,您可以通过指定注释的scanBasePackages属性来更改它。

In your case, you declare the annotation in the it.myapp.console.spring.Application class. 在您的情况下,您可以在it.myapp.console.spring.Application类中声明注释。

So only beans declared in the it.myapp.console.spring package and subpackage of it will be scanner by Spring. 因此,只有在it.myapp.console.spring包及其子包中声明的bean才会被Spring扫描。

But TestBeanImpl is not located in this base package : 但是TestBeanImpl不在此基本包中:

package it.myapp.console.test;

public class TestBeanImpl implements TestBean {
   public String doTest() {
      return "aaa";
   }
}

So, it will not be scanned. 因此,将不会对其进行扫描。

To solve your problem, you could move the TestBeanImpl class in the it.myapp.console.spring.test for example. 为了解决你的问题,你可以移动TestBeanImpl在类it.myapp.console.spring.test的例子。

And as a general advise, all your beans should be located in the base package or subpackage of the Spring Boot Application to avoid this kind of problem. 作为一般建议,所有bean都应位于Spring Boot Application的基本包或子包中,以避免此类问题。

Solved the problem by changing 通过更改解决问题

  SpringApplication app = new SpringApplication(MainMenu.class);

into 进入

  SpringApplication app = new SpringApplication(MainMenu.class, MailchimpConsoleSpringConfig.class);

I admit I copied that line and adapted it to my class name without reflecting on its semantics. 我承认我复制了该行并将其改编成我的班级名称,却没有考虑到它的语义。 It lists the first beans to load in the application, and the Java config bean must be in that list of course :) 它列出了要在应用程序中加载的第一个bean,并且Java config bean当然必须在该列表中:)

Thanks for everyone help anyway! 谢谢大家的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM