I am trying to write a SOAP service using Spring, however I receive a Dependency Injection issue. I\'m having problems using @Autowired
through the Service like
I added @Service
before impl class and the error is gone.
@Service
public class FCSPAnalysisImpl implements FCSPAnalysis
{}
We face this issue but had different reason, here is the reason:
In our project found multiple bean entry with same bean name. 1 in applicationcontext.xml & 1 in dispatcherServlet.xml
Example:
<bean name="dataService" class="com.app.DataServiceImpl">
<bean name="dataService" class="com.app.DataServiceController">
& we are trying to autowired by dataService name.
Solution: we changed the bean name & its solved.
In my case I had to move the @Service
annotation from the interface to the implementation class.
When you use @DataJpaTest you need to load classes using @Import explicitly. It wouldn't load you for auto.
Add the annotation @Repository
to the implementation of UserDaoImpl
@Repository
public class UserDaoImpl implements UserDao {
private static Log log = LogFactory.getLog(UserDaoImpl.class);
@Autowired
@Qualifier("sessionFactory")
private LocalSessionFactoryBean sessionFactory;
//...
}
I just solved this error happening in my tests.
Just make sure your test class has all dependencies in the attributes of the class being tested annotated with @MockBean
so SpringBoot test context can wire your classes correctly.
Only if you are testing controller: you need also class annotations to load the Controller context propertly.
Check it out:
@DisplayName("UserController Adapter Test")
@WebMvcTest(UserController.class)
@AutoConfigureMockMvc(addFilters = false)
@Import(TestConfig.class)
public class UserControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private CreateUserCommand createUserCommand;
@MockBean
private GetUserListQuery getUserListQuery;
@MockBean
private GetUserQuery getUserQuery;