Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.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声明bean_Java_Spring Boot - Fatal编程技术网

Java 在spring引导应用程序中用@bean声明bean

Java 在spring引导应用程序中用@bean声明bean,java,spring-boot,Java,Spring Boot,我刚刚开始使用Spring Boot 我的主课是abc.de,用@SpringApplication注释 到目前为止,一切正常。我只使用了原型注释 现在我想将@Bean与@Configuration类一起使用,看看它是如何工作的 我的@Configuration类: 类XslDataFileLoader在同一个包中 我在控制器类中用@Autowired声明这个bean 因此,我的主类在abc.de中,config类在abc.de.config中,XslDataFileLoader也在abc.de

我刚刚开始使用Spring Boot

我的主课是abc.de,用@SpringApplication注释

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

现在我想将@Bean与@Configuration类一起使用,看看它是如何工作的

我的@Configuration类:

类XslDataFileLoader在同一个包中

我在控制器类中用@Autowired声明这个bean

因此,我的主类在abc.de中,config类在abc.de.config中,XslDataFileLoader也在abc.de.config中

当我启动应用程序时,spring无法注入bean。它找不到它

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

我需要如何在最新的spring boot中声明这一点

编辑

堆栈跟踪:

XslDataFileLoader:

登录控制器:

第二版

MySpringBootApplication:

application.properties:


首先,请发布所有代码和stacktrace

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


在XslDataFileLoader类上有注释吗?如果您在XslDataFileLoader上同时声明了@Component,并且在BeanConfiguration中声明了@Component,则可能存在双bean声明和not bean not found的问题。

您在XslDataFileLoader上放置了@configuration或@Component了吗?@pvpkiran没有,我没有。查看我的编辑如果没有这些注释,Spring无法将其识别为bean。所以它不能autowire@pvpkiran事实并非如此,正如他在bean配置中声明的那样,在您声明的bean上还有其他一些东西没有名称。当您使用autowire时,spring会尝试按名称加载程序进行autowire。它找不到id为加载程序的bean。请尝试为在BeanConfiguration中声明的bean命名,例如@Beanname=loader,或者将控制器中的变量重命名为xslDataFileLoaderI在我的主类中也有@EnableJPareposities。。。但这不是问题,不是吗?你能不能也为你的主@SpringBootApplication Classis发布代码?它可能是一个名为dataSource的东西,因为SpringData创建了一个名为dataSource的默认bean。尝试为您的XslDataFileLoader使用另一个名称beanAh,它来自于之前,我首先在这里声明了一个数据源。后来我确实更改了字段名。因此在Bean配置中,它被声明为公共XslDataFileLoader,而您@Autowire->XslDataFileLoader;?
@Configuration
public class BeanConfiguration {

    @Bean
    public XslDataFileLoader dataSource() {
        return new XslDataFileLoader();
    }
}
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.
package abc.de.config;

public class XslDataFileLoader {
    public XslDataFileLoader() {

    }
}
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";
    }
}
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);
    }
}
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