Witam,
Mam bardzo dziwny problem z kontekstem Springa. Wstrzykiwanie nie działa. Servlet otrzymuje null. Korzystam z Apache Tomcat 8.
- Aplikacja skutecznie deployuje się (czyli czyta kontekst, inaczej Tomcat by krzyczal).
- Uruchomiłem autoskanowanie komponentów.
- Zarejestrowałem servlet w springu przez @Component.
- Wstrzyknąłem za pomocą @Inject. Próbowałem też przez @Autowired i nie udało się.
- Otrzymałem null. Co robię źle?
- Dokonałem walidacji XML w spring.xml. Przechodzi, raczej nie ma błędu składniowego.
Podejrzewam, że o czymś zapomniałem.
Moja konfiguracja Springa:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/p http://www.springframework.org/schema/p.xsd">
<!-- scan for spring components -->
<context:component-scan base-package="pl.hellospringioc" />
<!-- end scan for spring components -->
</beans>
web.xml;
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!-- spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring.xml
</param-value>
</context-param>
<!-- end spring -->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Testowy servlet:
package pl.hellospringioc;
import java.io.IOException;
import java.io.PrintWriter;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import pl.hellospringioc.injectable.IBusinessService;
@WebServlet(name = "HelloWorldServlet", urlPatterns = {"/"})
@Component
public class HelloWorldServlet extends HttpServlet {
@Inject
private IBusinessService srv;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HelloWorldServlet</title>");
out.println("</head>");
out.println("<body>");
out.println(srv.getHelloWorld());
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Testowy service:
package pl.hellospringioc.injectable;
public interface IBusinessService {
String getHelloWorld();
}
package pl.hellospringioc.injectable.impl;
import org.springframework.stereotype.Service;
import pl.hellospringioc.injectable.IBusinessService;
@Service
public class BusinessService implements IBusinessService {
@Override
public String getHelloWorld() {
return "Hello from service";
}
}
Mam podejrzenie, ze autoscan nie zadzialal. Nie rozumiem dlaczego.