gpt4 book ai didi

java - Spring @Autowired - 没有 'com.pc.visitor.visitorrepository' 类型的合格 bean

转载 作者:行者123 更新时间:2023-12-01 16:15:38 26 4
gpt4 key购买 nike

我正在使用 @Autowire 注释(Spring Maven 项目)。以下 webservice/api 工作正常。直到我在post.java中添加@Autowire注释。

项目路径/文件:

  • pcbackend > 访客 > pom.xml
  • pcbackend > 访问者 > src > main > 访问者 > Application.java
  • pcbackend > 访客 > src > 主要 > 访客 > newvisitor.java
  • pcbackend > 访客 > src > 主要 > 访客 > Visitorrepository.java
  • pcbackend > 访客 > src > main > 访客 > post.java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<groupId>com.pc</groupId>
<artifactId>visitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>


<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<source>14</source>
<target>14</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.pc.visitor;

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


@SpringBootApplication()
public class Application{

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


}

}
package com.pc.visitor;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;

@Entity
@Table(name = "visitors")
public class newvisitor {

@NotBlank
private String firstname;
@NotBlank
private String lastname;
@NotBlank
private String month;
@NotBlank
private String day;
@NotBlank
private String year;
@NotBlank
private String socialsecuritynumber;
@NotBlank
private String street1;
private String street2;
@NotBlank
private String city;
@NotBlank
private String state;
@NotBlank
private String zip;
@NotBlank
private String phone;
@NotBlank
private String email;

public newvisitor(){
super();
}

public newvisitor(String firstname, String lastname, String month, String day, String year, String socialsecuritynumber, String street1, String street2, String city, String state, String zip, String phone, String email) {
super();

this.firstname = firstname;
this.lastname = lastname;
this.month = month;
this.day = day;
this.day = year;
this.day = socialsecuritynumber;
this.day = street1;
this.day = street2;
this.day = city;
this.day = state;
this.day = zip;
this.day = phone;
this.day = email;
}




/**
* @return String return the firstname
*/
public String getFirstname() {
return firstname;
}

/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}

/**
* @return String return the lastname
*/
public String getLastname() {
return lastname;
}

/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}

/**
* @return String return the month
*/
public String getMonth() {
return month;
}

/**
* @param month the month to set
*/
public void setMonth(String month) {
this.month = month;
}

/**
* @return String return the day
*/
public String getDay() {
return day;
}

/**
* @param day the day to set
*/
public void setDay(String day) {
this.day = day;
}

/**
* @return String return the year
*/
public String getYear() {
return year;
}

/**
* @param year the year to set
*/
public void setYear(String year) {
this.year = year;
}

/**
* @return String return the socialsecuritynumber
*/
public String getSocialsecuritynumber() {
return socialsecuritynumber;
}

/**
* @param socialsecuritynumber the socialsecuritynumber to set
*/
public void setSocialsecuritynumber(String socialsecuritynumber) {
this.socialsecuritynumber = socialsecuritynumber;
}

/**
* @return String return the street1
*/
public String getStreet1() {
return street1;
}

/**
* @param street1 the street1 to set
*/
public void setStreet1(String street1) {
this.street1 = street1;
}

/**
* @return String return the street2
*/
public String getStreet2() {
return street2;
}

/**
* @param street2 the street2 to set
*/
public void setStreet2(String street2) {
this.street2 = street2;
}

/**
* @return String return the city
*/
public String getCity() {
return city;
}

/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}

/**
* @return String return the state
*/
public String getState() {
return state;
}

/**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
}

/**
* @return String return the zip
*/
public String getZip() {
return zip;
}

/**
* @param zip the zip to set
*/
public void setZip(String zip) {
this.zip = zip;
}

/**
* @return String return the phone
*/
public String getPhone() {
return phone;
}

/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}

/**
* @return String return the email
*/
public String getEmail() {
return email;
}

/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}

}

package com.pc.visitor;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface visitorrepository extends JpaRepository<newvisitor, Long> {
}
package com.pc.visitor;


import javax.validation.Valid;

import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;


@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class post {

//@Autowired
visitorrepository visitorrepository;

@PostMapping("/post")
public void insert(@Valid @RequestBody newvisitor newvisitor) {

}

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

Description:

Field visitorrepository in com.pc.visitor.post required a bean of type 'com.pc.visitor.visitorrepository' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.pc.visitor.visitorrepository' in your configuration.

我有一种感觉,我应该使用一些其他注释,例如 @service、@component、@controller、@configuration,甚至 @componentscan 等。我只是不知道在哪里列出这些东西.

关于我需要做什么有什么想法吗?就像我说的,@Autowired 注释掉后一切都很完美。

想法?

最佳答案

我的 newvisitor.java 文件/类中需要 @Id 注释。

package com.pc.visitor;

import javax.validation.constraints.NotBlank;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "visitors")
public class newvisitor {

@Id
private long id;
@NotBlank
private String firstname;
@NotBlank
private String lastname;
@NotBlank
private String month;
@NotBlank
private String day;
@NotBlank
private String year;
@NotBlank
private String socialsecuritynumber;
@NotBlank
private String street1;
private String street2;
@NotBlank
private String city;
@NotBlank
private String state;
@NotBlank
private String zip;
@NotBlank
private String phone;
@NotBlank
private String email;

public newvisitor(){
super();
}

public newvisitor(String firstname, String lastname, String month, String day, String year, String socialsecuritynumber, String street1, String street2, String city, String state, String zip, String phone, String email) {
super();

this.firstname = firstname;
this.lastname = lastname;
this.month = month;
this.day = day;
this.day = year;
this.day = socialsecuritynumber;
this.day = street1;
this.day = street2;
this.day = city;
this.day = state;
this.day = zip;
this.day = phone;
this.day = email;
}




/**
* @return String return the firstname
*/
public String getFirstname() {
return firstname;
}

/**
* @param firstname the firstname to set
*/
public void setFirstname(String firstname) {
this.firstname = firstname;
}

/**
* @return String return the lastname
*/
public String getLastname() {
return lastname;
}

/**
* @param lastname the lastname to set
*/
public void setLastname(String lastname) {
this.lastname = lastname;
}

/**
* @return String return the month
*/
public String getMonth() {
return month;
}

/**
* @param month the month to set
*/
public void setMonth(String month) {
this.month = month;
}

/**
* @return String return the day
*/
public String getDay() {
return day;
}

/**
* @param day the day to set
*/
public void setDay(String day) {
this.day = day;
}

/**
* @return String return the year
*/
public String getYear() {
return year;
}

/**
* @param year the year to set
*/
public void setYear(String year) {
this.year = year;
}

/**
* @return String return the socialsecuritynumber
*/
public String getSocialsecuritynumber() {
return socialsecuritynumber;
}

/**
* @param socialsecuritynumber the socialsecuritynumber to set
*/
public void setSocialsecuritynumber(String socialsecuritynumber) {
this.socialsecuritynumber = socialsecuritynumber;
}

/**
* @return String return the street1
*/
public String getStreet1() {
return street1;
}

/**
* @param street1 the street1 to set
*/
public void setStreet1(String street1) {
this.street1 = street1;
}

/**
* @return String return the street2
*/
public String getStreet2() {
return street2;
}

/**
* @param street2 the street2 to set
*/
public void setStreet2(String street2) {
this.street2 = street2;
}

/**
* @return String return the city
*/
public String getCity() {
return city;
}

/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}

/**
* @return String return the state
*/
public String getState() {
return state;
}

/**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
}

/**
* @return String return the zip
*/
public String getZip() {
return zip;
}

/**
* @param zip the zip to set
*/
public void setZip(String zip) {
this.zip = zip;
}

/**
* @return String return the phone
*/
public String getPhone() {
return phone;
}

/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}

/**
* @return String return the email
*/
public String getEmail() {
return email;
}

/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}

}

关于java - Spring @Autowired - 没有 'com.pc.visitor.visitorrepository' 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62396625/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com