0

iam beginner with spring and want to do so sample project .. i have a database which i want to connect to it .. i used annotaions like AutoWired & service .. But there is problem i can't solve

RegisterationController in Controller Package which have a service i want to call :

@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 Class in first Package :

@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 in first Package:

package first;

import org.springframework.data.repository.CrudRepository;

public interface StudentAccountRepository extends CrudRepository<StudentAccount,String > {

}

Student Account in first Package .. which refer to the table in database :

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 Class

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

now i have this Error :


APPLICATION FAILED TO START


Description:

Field dm in controller.RigesterationController required a bean of type 'first.DataManagment' that could not be found.

Action:

Consider defining a bean of type 'first.DataManagment' in your configuration.

Ahmed Hassan
  • 65
  • 1
  • 2
  • 12

1 Answers1

0

if you use @Autowired on private DataManagment you can't use new because that way Spring is unable to autowire it because it doesn't know anything about your new object. @Autowired as name suggests is used by Spring to auto find something that already exists. Besides if you're trying to inject by constructor it should be done like this

private DataManagement dm;

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

and for field injection just type

@Autowired
private DataManagement dm;
Colonder
  • 1,556
  • 3
  • 20
  • 40
  • I don't Understand the edited part .. do you mean replace service annotaion by Component annotaion .. i wrote as you said and made constructor but there is another error :- Parameter 0 of constructor in controller.RigesterationController required a bean of type 'first.DataManagment' that could not be found. – Ahmed Hassan May 08 '17 at 22:03
  • Oh sorry didn't notice that `@Service` annotation. Did you have a look at these? http://stackoverflow.com/questions/20333147/autowired-no-qualifying-bean-of-type-found-for-dependency – Colonder May 08 '17 at 22:07
  • http://stackoverflow.com/questions/28547665/autowired-no-qualifying-bean-of-type-found-for-dependency-at-least-1-bean – Colonder May 08 '17 at 22:07
  • http://stackoverflow.com/questions/8961275/no-matching-bean-of-type-found-for-dependency – Colonder May 08 '17 at 22:08
  • I looked At these .. But I think these are for another problems – Ahmed Hassan May 09 '17 at 19:47