私はSpring(起動)を使い始めています。春の起動アプリケーションで@Beanを使用してbeanを宣言する
私はabc.de
に私の "main"クラスを持っていて、@SpringApplication
と注釈がついています。
これまでのところ、すべて正常に動作します。ステレオタイプの注釈しか使用していません。
@Configuration
クラスで@Bean
を使用したいのですが、それはどのように動作するか見るだけです。
マイ@Configuration
クラス:
@Configuration
public class BeanConfiguration {
@Bean
public XslDataFileLoader dataSource() {
return new XslDataFileLoader();
}
}
クラスXslDataFileLoader
は、同じパッケージです。
@Autowired
でこのBeanをコントローラクラスに宣言します。
私の "main"クラスはabc.de
で、configクラスはであり、XslDataFileLoaderはabc.de.config
です。
私がアプリケーションを起動するとき、springはBeanを挿入できません。見つけられない。
私は試しましたscanPackages = abc.de.config
:そう、私の他の豆は見つかりません。
最新のスプリングブートでこれを宣言する必要はありますか?
EDIT
スタックトレース:
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() {
}
}
LoginController:
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";
}
}
2ND EDIT
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
[email protected]@
# 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
XslDataFileLoaderに@configurationまたは@ componentを入れましたか? – pvpkiran
@pvpkiranいいえ、私はしていません。 – user3813234
これらの注釈がない場合、Springはその注釈をBeanとして認識できません。したがって、それはオートワイヤできません – pvpkiran