1

我有以下配置 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 ,并且该文件应该做一些魔术以设置数据库连接,但事实并非如此。我将非常感谢任何帮助。

4

2 回答 2

1

您还需要将上下文加载器添加到您的 web.xml,以启动独立于 Dispatcher Servlet 的 ApplicationContext。

<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>
</listener>
于 2013-01-21T23:05:25.703 回答
0

你初始化bean吗?您应该有类似应用程序上下文的东西,该上下文被调用并从 .xml 中获取信息并关闭所有 setter 方法。就像是:

ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
userDAO = (UserDAO) ac.getBean("myDataSource");

另外,我不确定您是否希望 UserDAO 作为您的属性名称,如果那也是您的类名称。

于 2013-01-21T23:01:06.127 回答