刚接触Spring,由于项目需要,我冲进去了。在将 Spring 用作 Java 应用程序时,我成功地使用了 Spring 的大部分功能,但在 Web 应用程序中没有得到想要的结果。
我正在尝试在 applicationContext.xml 中初始化我的数据源:
<context:annotation-config />
<context:component-scan base-package="learn.spring.webapp.dao" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate"
autowire="constructor" />
网页.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
我成功地在我的应用程序中初始化了 Spring,但未能在我的 dao 类中自动装配 jdbctemplate。
package learn.spring.webapp.dao;
import learn.spring.webapp.model.Manga;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class MangaDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert(Manga manga){
String sql = "insert into manga_list values (?,?)";
jdbcTemplate.update(sql,
new Object[]{manga.getName(), manga.getAuthor()}); //Null Pointer exception
}
}
我想在我的 dao 包中自动装配template
bean 。JdbcTemplate
当我NullPointerException
尝试使用JdbcTemplate
. MangaDao
正在从 servlet 调用。我想我可能没有正确使用自动装配的注释。请指教。
编辑:
调用 DAO 的 servlet 代码是:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
MangaDao md=new MangaDao();
Manga manga=new Manga();
manga.setName("Bleach");
manga.setAuthor("Kubo Tite");
md.insert(manga);
}
而stackTrace如下:
SEVERE: Servlet.service() for servlet TesterServlet threw exception
java.lang.NullPointerException
at learn.spring.webapp.dao.MangaDao.insert(MangaDao.java:16)
at learn.spring.webapp.controller.TesterServlet.doGet(TesterServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)