我对春天很新。我有动态Web应用程序项目。在里面我有一个接收请求的servlet。请求附带请求号码。基于no我将为适当的请求类创建一个新对象并提供请求。现在我需要与春天融为一体。我应用了以下配置,
WEB.XML(加载弹簧上下文)
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/docspring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
在servlet中
package com.receve;
@Controller
@WebServlet("/Recever")
public class Recever extends HttpServlet {
@Autowired
private ClassOne one;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//request parameters receved and
if(req==1){
one.print();
}
}
public ClassOne getOne() {
return One;
}
public void setOne(ClassOne one) {
this.one = one;
}
}
在ClassOne中
package com.operations;
@Component
public Class ClassOne{
public void print(){
//some statement;
}
}
在spring.xml中
<context:annotation-config />
<context:component-scan base-package="com" />
但是在运行应用程序时,我在调用one.print()方法时遇到了NullPointerexception。获得这个的正确方法(配置)是什么?
感谢。