Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 652 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UnsatisfiedDependencyException: Error creating bean with name

#1
For several days I have been trying to create a Spring CRUD application. I'm confused.
I can't solve these errors.

> org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientController': Unsatisfied dependency expressed through method 'setClientService' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kopylov.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

And this:

> org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.kopylov.repository.ClientRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

**ClientController**

@Controller
public class ClientController {
private ClientService clientService;

@Autowired
@Qualifier("clientService")
public void setClientService(ClientService clientService) {
this.clientService = clientService;
}

@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client) {
this.clientService.addClient(client);
return "home";
}
}

**ClientServiceImpl**

@Service("clientService")
public class ClientServiceImpl implements ClientService {

private ClientRepository clientRepository;

@Autowired
@Qualifier("clientRepository")
public void setClientRepository(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}

@Transactional
public void addClient(Client client) {
clientRepository.saveAndFlush(client);
}
}

**ClientRepository**

public interface ClientRepository extends JpaRepository<Client, Integer> {

}

I looked through a lot of similar questions, but not one answer to them helps me.
Reply

#2
Add @Repository annotation to the Spring Data JPA repo
Reply

#3
According to [documentation][1] you should set XML configuration:

<jpa:repositories base-package="com.kopylov.repository" />


[1]:

[To see links please register here]

Reply

#4
Considering that your package scanning is correctly set either through XML configuration or annotation based configuration.

You will need a `@Repository` on your `ClientRepository` implementation as well to allow Spring to use it in an `@Autowired`. Since it's not here we can only suppose that's what's missing.

As a side note, it would be cleaner to put your `@Autowired`/`@Qualifier` directly on your member if the setter method is only used for the `@Autowired`.

@Autowired
@Qualifier("clientRepository")
private ClientRepository clientRepository;

Lastly, you don't need the `@Qualifier` is there is only one class implementing the bean definition so unless you have several implementation of `ClientService ` and `ClientRepository ` you can remove the `@Qualifier`
Reply

#5
The ClientRepository should be annotated with `@Repository` tag.
With your current configuration Spring will not scan the class and have knowledge about it. At the moment of booting and wiring will not find the ClientRepository class.

**EDIT**
If adding the `@Repository` tag doesn't help, then I think that the problem might be now with the `ClientService` and `ClientServiceImpl`.

Try to annotate the `ClientService` (interface) with `@Service`. As you should only have a single implementation for your service, you don't need to specify a name with the optional parameter `@Service("clientService")`. Spring will autogenerate it based on the interface' name.

Also, as Bruno mentioned, the `@Qualifier` is not needed in the `ClientController` as you only have a single implementation for the service.

**ClientService.java**

@Service
public interface ClientService {

void addClient(Client client);
}


**ClientServiceImpl.java** (option 1)

@Service
public class ClientServiceImpl implements ClientService{

private ClientRepository clientRepository;

@Autowired
public void setClientRepository(ClientRepository clientRepository){
this.clientRepository=clientRepository;
}

@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}


**ClientServiceImpl.java** (option 2/preferred)

@Service
public class ClientServiceImpl implements ClientService{

@Autowired
private ClientRepository clientRepository;

@Transactional
public void addClient(Client client){
clientRepository.saveAndFlush(client);
}
}

**ClientController.java**

@Controller
public class ClientController {
private ClientService clientService;

@Autowired
//@Qualifier("clientService")
public void setClientService(ClientService clientService){
this.clientService=clientService;
}

@RequestMapping(value = "registration", method = RequestMethod.GET)
public String reg(Model model){
model.addAttribute("client", new Client());
return "registration";
}

@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
this.clientService.addClient(client);
return "home";
}
}
Reply

#6
Try adding @EntityScan(basePackages = "insert package name here") on top of your main class.
Reply

#7
That might happen because the pojos you are using lack of the precise constructor the service needs. That is, try to generate all the constructors for the pojo or objects (model object) that your serviceClient uses, so that the client can be instanced correctly. In your case,regenerate the constructors (with arguments)for your client object (taht is your model object).
Reply

#8
The application needs to be placed in the same directory as the scanned package:

![enter image description here][1]

[1]:
Reply

#9
I had the exactly same issue, with a very very long stack trace.
At the end of the trace I saw this:

> InvalidQueryException: Keyspace 'mykeyspace' does not exist

I created the keyspace in cassandra, and solved the problem.

CREATE KEYSPACE mykeyspace
WITH REPLICATION = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
Reply

#10
I was facing the same issue, and it was, as i missed marking my DAO class with Entity annotations. I tried below and error got resolved.

/**
*`enter code here`
*/
@Entity <-- was missing earlier
public class Topic {
@Id
String id;
String name;
String desc;

.
.
.
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through