我有以下配置 web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>minapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>minapp</servlet-name>
<url-pattern>hello.htm</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>register</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>register</servlet-name>
<url-pattern>/register/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
</web-app>
应用程序上下文.xml:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/minapp" />
<property name="username" value="root" />
<property name="password" value="verysecret" />
</bean>
<bean id="myUserDAO" class="no.java.UserDAOImpl">
<property name="DataSource" ref="myDataSource"/>
</bean>
<bean name="/register/register.htm" class="no.java.RegistrationController" >
<property name="UserDAO" ref="myUserDAO" />
</bean>
</beans>
注册控制器的设置器
private UserDAO userDAO;
public void setUserDAO(UserDAO userDAO) {
System.out.println("dao set");
if (userDAO == null){
System.out.println("dao null");
}
this.userDAO = userDAO;
}
UserDAOImpl 的设置器:
private DataSource dataSource;
public void setDataSource(DataSource source){
System.out.println("data source setter called");
this.dataSource=source;
}
并且没有调用 RegistrationController 设置器和 UserDAOImpl 设置器。我有一种感觉,我可能放错了配置,但由于 Spring 文档的复杂性,我无法理解我做错了什么。据我了解 contextConfigLocation 应该将 spring 指向 applicationContext.xml ,并且该文件应该做一些魔术以设置数据库连接,但事实并非如此。我将非常感谢任何帮助。