java - 在 Spring Boot 应用程序中使用 @Bean 声明 bean

标签 java spring-boot

我刚刚开始使用 Spring(启动)。

我的“主要”类(class)位于 abc.de ,注释为@SpringApplication .

到目前为止,一切正常。我只使用了构造型注释。

现在我想使用@Bean@Configuration类,只是为了看看它是如何工作的。

我的@Configuration类:

@Configuration
public class BeanConfiguration {

    @Bean
    public XslDataFileLoader dataSource() {
        return new XslDataFileLoader();
    }
}

类(class)XslDataFileLoader位于同一个包中。

我用@Autowired声明这个bean在 Controller 类中。

所以我的“主要”类(class)在 abc.de ,配置类位于 abc.de.config XslDataFileLoader 位于 abc.de.config 中也是如此。

当我启动应用程序时,spring 无法注入(inject) bean。没找到。

我试过scanPackages = abc.de.config :这样,我的其他 bean 就找不到了。

我需要如何在最新的 Spring Boot 中声明这一点?

编辑

堆栈跟踪:

2017-05-19 13:52:03 ERROR o.s.b.d.LoggingFailureAnalysisReporter - 

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

Description:

Field dataSource in abc.de.controllers.LoginController required a bean of type 'abc.de.config.XslDataFileLoader' that could not be found.


Action:

Consider defining a bean of type 'abc.de.config.XslDataFileLoader' in your configuration.

XslDataFileLoader:

package abc.de.config;

public class XslDataFileLoader {
    public XslDataFileLoader() {

    }
}

登录 Controller :

package abc.de.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import abc.de.config.XslDataFileLoader;

@Controller
public class LoginController {

    @Autowired
    XslDataFileLoader loader;

    @GetMapping("/login")
    public String login() {
        System.out.println(loader);
        return "login";
    }

    @PostMapping("/login")
    public String loginTry() {
        return "redirect:dashboard";
    }
}

第二次编辑

MySpringBootApplication:

package abc.de;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@EnableJpaRepositories
public class MySpringBootApplication{

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

application.properties:

server.port=5566
spring.application.name=@project.name@
# data source
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=
# Session
spring.session.store-type=none
# Security
security.basic.enabled=false

# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.SQL=debug
#logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.=error

最佳答案

首先,请发布您的所有代码和堆栈跟踪。

BeanConfiguration 类配置正确,因此您的注入(inject)可能有问题。

您对 XslDataFileLoader 类有注释吗?这可能是双bean声明的问题,而不是找不到bean(如果您在XslDataFileLoader上有一个@Component,并且同时在BeanConfiguration中声明它)

关于java - 在 Spring Boot 应用程序中使用 @Bean 声明 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44069175/

相关文章:

java - 如何在 IntelliJ 中查看 JDK 中的特定 Java 源代码文件?

spring-boot - 如何查看在 Spring boot 中使用 Cassandra 时生成的 CQL

neo4j - 如何在 Spring Data Neo4j (SDN4) 中进行集成测试

java - Spring Boot - 配置文件激活其他配置文件

java - 如何使上下文菜单在 javaFX 的 TreeView 上工作?

java - 不要在 JLabel 中使用 XML 文件读取 '<'

C++ system() 调用未正确记录 Java 返回码

java - 如何将 Double 中的秒数转换为两个 int 秒数和毫秒数?

hibernate - 如何使用JPA + MySQL在Spring Boot中实现全文搜索

java - 使用thymeleaf从URL访问数据到java?