UnsatisfiedDependencyException: Error creating bean with name
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";
}
}
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoRestController'
Your DemoApplication
class is in the com.ag.digital.demo.boot
package and your LoginBean
class is in the com.ag.digital.demo.bean
package. By default components (classes annotated with @Component
) are found if they are in the same package or a sub-package of your main application class DemoApplication
. This means that LoginBean
isn't being found so dependency injection fails.
There are a couple of ways to solve your problem:
- Move
LoginBean
intocom.ag.digital.demo.boot
or a sub-package. - Configure the packages that are scanned for components using the
scanBasePackages
attribute of@SpringBootApplication
that should be onDemoApplication
.
A few of other things that aren't causing a problem, but are not quite right with the code you've posted:
@Service
is a specialisation of@Component
so you don't need both onLoginBean
- Similarly,
@RestController
is a specialisation of@Component
so you don't need both onDemoRestController
DemoRestController
is an unusual place for@EnableAutoConfiguration
. That annotation is typically found on your main application class (DemoApplication
) either directly or via@SpringBootApplication
which is a combination of@ComponentScan
,@Configuration
, and@EnableAutoConfiguration
.
How to fix: Error creating bean with name : Unsatisfied dependency expressed through field
In your User
class you declare id with int
type
@Id
@Column(name = "id", unique = true)
private int id;
But in the repository interface, you declared Long
public interface UserRepository extends JpaRepository<User, Long> {}
So, in User
class change type of id like,
@Id
@Column(name = "id", unique = true)
private Long id;
And avoid your new error, use @Service
annotation at UserService
interface like
@Service
public interface UserService {
List<User> getUsers();
}
Spring Boot UnsatisfiedDependencyException Error creating bean with name unresolvable circular reference
Definitely issue is related with two annotations: @Autowired
for filters and @Bean
annotation for authenticationManagerBean
method. First of all need to understand that WebAppSecurityConfiguration
class will be ready for use when both filters were autowired, but for successful autowiring both filters have relation on AuthenticationManager
, and for that target we marked method authenticationManagerBean
by @Bean
annotation, and Spring tried to create AuthenticationManager
in following way:
public AuthenticationManager authenticationManagerBean() throws Exception {
return new AuthenticationManagerDelegator(this.authenticationBuilder, this.context);
}
as you can see for creating AuthenticationManagerDelegator
we should have initialized two fields authenticationBuilder
and context
, but we have just started creating our WebAppSecurityConfiguration
configuration bean, which will be marked as on creation until all fields are initialized, it was made specifically for avoiding of using uninitialized fields and getting NullPointerException
exceptions.
But if review the second option of beans defitions, the workflow of beans creation was changed in following way:
WebAppSecurityConfiguration
doesn't have fields for autowiring and we can easily create first of all configuration bean and unmark likein creation
- after config bean was created, all
@Bean
annotations will be processed and on creatingauthenticationManagerBean
we will not get the exception, because all fields were initialized when we was creatingWebAppSecurityConfiguration
configuration and now we can use all parent methods safely.
P.S. From my point of view, defining beans in following way:
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
is not good idea, because WebSecurityConfigurerAdapter
class will create all required components(beans) in correct order, and these methods from parent class were opened for customization implementation of bean in following way:
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return new CustomAuthenticationManager(...);
}
Related Topics
How to Send Java.Util.Logging to Log4J
Why Are Class Static Methods Inherited But Not Interface Static Methods
Remove Last Character of a Stringbuilder
Why Does Java Switch on Contiguous Ints Appear to Run Faster with Added Cases
How to Convert .Pfx File to Keystore with Private Key
How Does the Jvm Ensure That System.Identityhashcode() Will Never Change
Write a Program That Will Surely Go into Deadlock
How to Convert Iterable to Collection
How to Pass Console Arguments to Application in Eclipse
Why Maven? What Are the Benefits
How to Retrieve Ip Address from Http Header in Java
Httpget with Https:Sslpeerunverifiedexception