Я пытаюсь просто попробовать весну, но я, кажется, что-то упускаю. Кажется, что загружает Spring и бобов очень хорошо, однако, когда дело доходит до инъекции этих бобов с автоподзаводом, это не работает. Кто-нибудь знает?Весна не впрыскивает бобы, что я делаю неправильно?
Часть ибо web.xml для весны и MainServlet:
<welcome-file-list>
<welcome-file>/login.jsp</welcome-file>
</welcome-file-list>
<!-- Spring Dependency Injection -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
<!-- Login Filter -->
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.nortal.pirs.presentation.web.LoginFilter</filter-class>
<init-param>
<param-name>secretParameter</param-name>
<param-value>8392</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/MainServlet</url-pattern>
<servlet-name>MainServlet</servlet-name>
</filter-mapping>
<!-- Main Servlet -->
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.nortal.pirs.presentation.web.MainServlet</servlet-class>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/main/*</url-pattern>
</servlet-mapping>
Spring контекст файл приложения (хотя я думаю, что я загрузил его со слишком большим количеством ненужного дерьма, но он был в отчаянии, потому что он не работал):
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config />
<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />
<context:component-scan base-package="com.nortal.pirs.test.independent" />
<context:component-scan base-package="com.nortal.pirs.businesslogic.logic" />
<context:component-scan base-package="com.nortal.pirs.presentation.vaadin" />
<context:component-scan base-package="com.nortal.pirs.presentation.vaadin.views" />
<context:component-scan base-package="com.nortal.pirs.presentation.web" />
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
</beans>
UserManagerLogic (Компонент, который должен быть введен в MainServlet с @autowired позже):
@Component("UserManager")
public class UserManagerLogic implements UserManagerInterface {
MainServlet:
@Service
public class MainServlet extends HttpServlet {
@Autowired
@Qualifier("UserManager")
private UserManagerInterface userManager;
Logger log;
public MainServlet() {
log = Logger.getLogger(getClass());
}
public boolean userLoggedIn(String username, String password) {
return SecurityManager.getInstance().credentialsValid(username, password);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
processRequest(req, resp);
}
public void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = ((HttpServletRequest) request).getSession();
String username = (String) session.getAttribute("username");
boolean authenticated = (boolean) session.getAttribute("authenticated");
User user = userManager.getUserByEmail(username);
WelcomeGenerator welcomeGenerator = new WelcomeGenerator();
if (authenticated) {
generateResponse(response, welcomeGenerator.WelcomeMessage(user), "The secret code is " + session.getAttribute("secretParameter"));
} else {
generateResponse(response, welcomeGenerator.wrongCredentialsMessage(username), "Secret code is hidden, because authentication failed");
}
}
public void generateResponse(HttpServletResponse response, String welcomeMessage, String additionalData) throws IOException {
HtmlGenerator generator = new HtmlGenerator("PIRS");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write(generator.printHeader());
out.write(generator.printCenter(welcomeMessage));
out.write(generator.printCenter(additionalData));
out.write(generator.printFooter());
}
public UserManagerInterface getUserManager() {
return userManager;
}
public void setUserManager(UserManagerInterface userManager) {
this.userManager = userManager;
}
}
И исход является нулевым указателем исключения на вызывать для userManager, конечно, что должно было быть введено весной?
java.lang.NullPointerException
at com.nortal.pirs.presentation.web.MainServlet.processRequest(MainServlet.java:58)
at com.nortal.pirs.presentation.web.MainServlet.doPost(MainServlet.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
Однако весна загружает бобы, она только их не вводит и не вызывает никакой ошибки, почему?
2013-03-08 03:48:42,834 [localhost-startStop-1] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.s[email protected]6a4ac9fb: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.config.internalBeanConfigurerAspect,mainController,SecurityManager,**UserManager**,VisitManager,**mainServlet**,org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor#0,org.springframework.context.annotation.CommonAnnotationBeanPostProcessor#0,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy
почему бы вам не использовать контроллер вместо сервлет –
'@' обслуживания аннотаций не предназначен для servlet.Check это out.Should быть такая же проблема, как ваша .http: //stackoverflow.com/questions/14283750/instantiationexception-using-spring-injection – SRy