Spring Application无法启动

时间:2017-05-08 21:44:07

标签: java mysql spring repository

我是春天的初学者,并希望这样做示例项目..我有一个数据库,我想连接到它..我使用了像AutoWired& amp;服务..但有问题我无法解决

ControllerGroup 中的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;
	}

}

MainController类

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

现在我有这个错误:

申请失败

说明

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

动作:

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

1 个答案:

答案 0 :(得分:0)

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

private DataManagement dm;

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

和现场注入只需输入

@Autowired
private DataManagement dm;