【问题标题】:Spring Application failed to startSpring 应用程序启动失败
【发布时间】:2017-10-07 01:13:36
【问题描述】:

我是 spring 的初学者,想这样做示例项目 .. 我有一个要连接到它的数据库 .. 我使用了 AutoWired 和服务之类的注释 .. 但有一个问题我无法解决

RegisterationController 在控制器包中有一个我想调用的服务:

@RestController
public class RigesterationController {
	@Autowired
	private DataManagment dm =new DataManagment() ;
	
	@CrossOrigin(origins = "http://localhost:8060")
	@GetMapping("/SignUp/{email}/{UName}/{Pass}/{gender}/{UserType}")
	@ResponseBody
	public void SignUp(@PathVariable String email , @PathVariable String UName , 
					   @PathVariable String Pass , @PathVariable char gender , @PathVariable String UserType )
	{		
		boolean valid = dm.Validate(email , Pass ) ; // validate email & Pass
		if (valid)
		{
			if (UserType.equals("s"))
			{
				StudentAccount studentaccount = new StudentAccount(email , Pass , UName , gender) ;
				dm.add(studentaccount) ;
				System.out.println("Account Created Successfully"  );
			}
		}
		else 
		{	
			System.out.println("Invalid Data, Please Try Again" );
		}
		
		}

DataManagement 类在第一个包中

@Service
public class DataManagment {
	
	@Autowired
	private StudentAccountRepository StudentAccount1 ;
	
  public boolean Validate (String email , String Pass )
	{
		
		if(!email.contains("@") || email.contains(" ") || (Pass.length()<8) || !email.contains(".com") )
			return false;
		else
			return true;
	}
  
	public void add(StudentAccount studentaccount2) {
		StudentAccount1.save(studentaccount2) ;
	}

StudentAccountRepository 在第一个包中

package first;

import org.springframework.data.repository.CrudRepository;

public interface StudentAccountRepository extends CrudRepository<StudentAccount,String > {

}

学生帐户在第一个包中 .. 参考数据库中的表:

package first;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="studentaccount")
public class StudentAccount  {
	@Id
	private String email;
	private String password;
	private String name ;
	private char gender;
	
	public StudentAccount(String email, String password , String name , char gender ) {
		super();
		this.email = email;
		this.password = password;
		this.name = name;
		this.gender = gender;
		
	}
	
	public StudentAccount() {
		super();
		this.email = "";
		this.password = "";
		this.name = "";
		this.gender = ' ';
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

}

主控制器类

package controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
public class MainController {
	public static void main(String[] args) {
		SpringApplication.run(MainController.class, args);
	}
}

现在我有这个错误:


应用程序启动失败


说明:

controller.RigesterationController 中的字段 dm 需要一个无法找到的“first.DataManagment”类型的 bean。

行动:

考虑在您的配置中定义一个“first.DataManagment”类型的 bean。

【问题讨论】:

  • 你能展示你的主要课程吗?
  • 检查帖子 .. 现在正在编辑

标签: java mysql spring repository


【解决方案1】:

如果你在private DataManagment 上使用@Autowired,你就不能使用new,因为Spring 无法自动装配它,因为它对你的新对象一无所知。 @Autowired 顾名思义,Spring 使用它来自动查找已经存在的东西。此外,如果您尝试通过构造函数进行注入,则应该这样做

private DataManagement dm;

@Autowired
public RigesterationController(DataManagment dm)
{
   this.dm = dm;
}  

对于字段注入,只需键入

@Autowired
private DataManagement dm;

【讨论】:

猜你喜欢
  • 2017-05-12
  • 2019-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 2018-08-14
  • 2021-06-06
  • 2021-07-21
相关资源
最近更新 更多