I'm using Eclipse and maven with Java 1.8 trying to build a spring started project, which's based on maven project so i build my own Entity underName Candidat
with this full code block
我正在使用Eclipse和maven与Java 1.8尝试构建一个基于maven项目的Spring启动项目,所以我用这个完整的代码块构建我自己的实体underName Candidat
package com.example.demo.entities;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity(name = "CandidatTable")
public class Candidat implements Serializable {
@Id @GeneratedValue
private Long id;
private String name;
private String prenom;
private String reference;
private String resumeCandidate;
public Candidat() {
super();
}
public Candidat(String name, String prenom, String reference, String resumeCandidate) {
super();
this.name = name;
this.prenom = prenom;
this.reference = reference;
this.resumeCandidate = resumeCandidate;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getResumeCandidate() {
return resumeCandidate;
}
public void setResumeCandidate(String resumeCandidate) {
this.resumeCandidate = resumeCandidate;
}
}
in normal case we should build an Interface into it we should define our services method like we said : save()
, findAllRecords()
,findSpecificRecordByID()
,updateRecordLine()
,deleteRecord()
...etc, but in my case I used Spring-data in my maven project that in my web.xml file I have this dependency:
在正常情况下,我们应该构建一个接口,我们应该定义我们的服务方法,如我们所说:save(),findAllRecords(),findSpecificRecordByID(),updateRecordLine(),deleteRecord()...等,但在我的情况下,我使用我的maven项目中的Spring-data在我的web.xml文件中我有这个依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
so there's no need to define our methods, because Spring-data use its own methods in situation we create an interface which extends the Generic Interface JpaRepository
, so my interface look like this:
因此没有必要定义我们的方法,因为Spring-data在我们创建扩展Generic Interface JpaRepository的接口的情况下使用它自己的方法,所以我的界面如下所示:
package com.example.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.entities.Candidat;
public interface ICandidate extends JpaRepository<Candidat, Long>{
//no need to define our methods, because we gonna use methods difined
// by SpringData whose comes from JPA specification.
}
Finally, the main class code is this one :
最后,主类代码是这样的:
package com.example.demo;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import com.example.dao.ICandidate;
import com.example.demo.entities.Candidat;
@SpringBootApplication
public class CatWebServiceApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(CatWebServiceApplication.class, args);
ICandidate condidateRep = context.getBean(ICandidate.class);
condidateRep.save(
new Candidat("UserName_1", "FirstName_1", "Ref_1", "/Home/file/docFile_1.docx")); //insert data using Ioc later after runing urself
condidateRep.save(
new Candidat("UserName_2", "FirstName_2", "Ref_2", "/Home/file/docFile_2.docx"));
List<Candidat> cnds = condidateRep.findAll();
cnds.forEach(p-> System.out.println(p.getResumeCandidate()));
}
}
My application SHOULD turn well, look to web.xml to manages the application dependcies, then its look into the Path src/main/resources
which contain the file application.properties
that contain this code:
我的应用程序应该转好,查看web.xml来管理应用程序依赖,然后查看包含包含此代码的文件application.properties的Path src / main / resources:
# DataSource settings:
spring.datasource.url = jdbc:mysql://localhost:3306/softherWebService
spring.datasource.username = root
spring.datasource.password = dbPassword
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
Suddenly I get this error message on my eclipse console:
突然,我在我的eclipse控制台上收到此错误消息:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '
com.example.dao.ICandidate
' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at com.example.demo.CatWebServiceApplication.main(CatWebServiceApplication.java:19)线程“main”中的异常org.springframework.beans.factory.NoSuchBeanDefinitionException:org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java)中没有“com.example.dao.ICandidate”类型的限定bean。 353)atg.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)at com.example.demo.CatWebServiceApplication 。主要(CatWebServiceApplication.java:19)
so the problem in getBean() line, I used the CommandLineRunner too but doesn't solve my problem in getting the bean.
所以在getBean()行中的问题,我也使用了CommandLineRunner但是没有解决我在获取bean时遇到的问题。
1 个解决方案
#1
0
When using @SpringBootApplication
that automatically implies a @ComponentScan
. The default behavior for @ComponentScan
is, when not explicitly having basePackages
defined, is to start scanning from the package from the class it has been defined on. For Spring Boot this also applies to all other auto-configuration like detecting entities, detecting Spring Data repositories etc.
使用自动隐含@ComponentScan的@SpringBootApplication时。 @ComponentScan的默认行为是,当未明确定义basePackages时,是从已定义的类开始从包中扫描。对于Spring Boot,这也适用于所有其他自动配置,如检测实体,检测Spring Data存储库等。
Now as your CatWebServiceApplication
is defined in the com.example.demo
and your ICandidate
in com.example.dao
the latter will not be scanned because it isn't part of the com.example.demo
package.
现在,您在com.example.demo中定义了CatWebServiceApplication,在com.example.dao中定义了ICandidate,后者将不会被扫描,因为它不是com.example.demo包的一部分。
There are several ways to fix this.
有几种方法可以解决这个问题。
First you could specify the scanBasePackages
on the @SpringBootApplication
to have components detected, however that wouldn't solve this as you would also need @EnableJpaRepositories("com.example.dao")
and @EntityScan("com.example.dao")
and probably a couple more when extending the technologies.
首先,您可以在@SpringBootApplication上指定scanBasePackages以检测组件,但这不会解决此问题,因为您还需要@EnableJpaRepositories(“com.example.dao”)和@EntityScan(“com.example.dao”)在扩展技术时可能还需要几个。
The easiest, and recommended way, is to put your CatWebServiceApplication
in the com.example
package so that all sub-packages are covered and you don't need to think about all the additional annotations you would need to add.
最简单且推荐的方法是将您的CatWebServiceApplication放在com.example包中,以便涵盖所有子包,您无需考虑需要添加的所有其他注释。
#1
0
When using @SpringBootApplication
that automatically implies a @ComponentScan
. The default behavior for @ComponentScan
is, when not explicitly having basePackages
defined, is to start scanning from the package from the class it has been defined on. For Spring Boot this also applies to all other auto-configuration like detecting entities, detecting Spring Data repositories etc.
使用自动隐含@ComponentScan的@SpringBootApplication时。 @ComponentScan的默认行为是,当未明确定义basePackages时,是从已定义的类开始从包中扫描。对于Spring Boot,这也适用于所有其他自动配置,如检测实体,检测Spring Data存储库等。
Now as your CatWebServiceApplication
is defined in the com.example.demo
and your ICandidate
in com.example.dao
the latter will not be scanned because it isn't part of the com.example.demo
package.
现在,您在com.example.demo中定义了CatWebServiceApplication,在com.example.dao中定义了ICandidate,后者将不会被扫描,因为它不是com.example.demo包的一部分。
There are several ways to fix this.
有几种方法可以解决这个问题。
First you could specify the scanBasePackages
on the @SpringBootApplication
to have components detected, however that wouldn't solve this as you would also need @EnableJpaRepositories("com.example.dao")
and @EntityScan("com.example.dao")
and probably a couple more when extending the technologies.
首先,您可以在@SpringBootApplication上指定scanBasePackages以检测组件,但这不会解决此问题,因为您还需要@EnableJpaRepositories(“com.example.dao”)和@EntityScan(“com.example.dao”)在扩展技术时可能还需要几个。
The easiest, and recommended way, is to put your CatWebServiceApplication
in the com.example
package so that all sub-packages are covered and you don't need to think about all the additional annotations you would need to add.
最简单且推荐的方法是将您的CatWebServiceApplication放在com.example包中,以便涵盖所有子包,您无需考虑需要添加的所有其他注释。