jestem nowy z wiosny, a ja staram się używać adnotacji @Autowire dla ServletContext z mojego atrybutu klasy:Nie można Autowire ServletContext
@Controller
public class ServicesImpl implements Services{
@Autowired
ServletContext context;
zdefiniowałem fasoli dla tej klasy w moim dyspozytor-servlet.xml:
<bean id="services" class="com.xxx.yyy.ServicesImpl" />
Ale gdy próbuję uruchomić test JUnit to daje następujący błąd:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Myślałem, że zastrzyk ServletContext był automatyczny ze sprężyną ... Jak mogę to rozwiązać?
Dzięki!
EDYCJA: Chcę użyć servletContext do wywołania metody getRealPath(). Czy jest jakaś alternatywa?
Tak, jeśli pochodzi z JUNit, powinieneś użyć MockServletContext – ccheneson
Przepraszamy za usunięcie posta, zdefiniowałem bean bean MockServletContext w moim test-context.xml, ale zmienna jest nadal pusta ... – qxlab
Czy ładujesz kontekst testu. xml? – ccheneson