Reputation: 678
I have this Repository class which I wish to Autowire in a unit test. I'm currently getting the "no default constructor" error when running the test.
The class in question has no default constructor, I'm new to spring so may not have created the Bean correctly in the config class.
Below is the Bean in question (has no default constructor)
@Repository
public class GenericDaoImpl<T extends AbstractEntity> implements GenericDao<T> {
The config class
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.example")
public class AppConfig {
@Bean
GenericDaoImpl<AbstractEntity> genericDoaIpm(final Class<AbstractEntity> tClass) {
return new GenericDaoImpl<AbstractEntity>(tClass);
}
}
And in the test I have:
@Autowired
private GenericDaoImpl<AbstractEntity> genericDaoImpl;
Is there something I'm missing or doing wrong here?
Upvotes: 1
Views: 3638
Reputation: 24433
According to this and this, you only need to mark your constructor with @Autowired
.
GenericDaoImpl.java
@Autowired
public GenericDaoImpl(Class<?> tClass) {
...
}
You can apply @Autowired to constructors as well. A constructor @Autowired annotation indicates that the constructor should be autowired when creating the bean, even if no elements are used while configuring the bean in XML file
Upvotes: 2