春天不叫召唤者

时间:2013-01-21 22:56:01

标签: java spring configuration javabeans setter

我有以下配置 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>

的applicationContext.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>

RegistrationController的setter

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的setter:

private DataSource dataSource;

    public void setDataSource(DataSource source){
        System.out.println("data source setter called");
        this.dataSource=source;

    }

并且不会调用RegistrationController设置器和UserDAOImpl设置器。 我有一种感觉,我可能错配了配置,但由于Spring文档的复杂性,我无法得到我做错了。 据我所知,contextConfigLocation应该指向springContext.xml,并且该文件应该做一些魔术以便设置数据库连接,但事实并非如此。 我会非常感谢任何帮助。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

你初始化bean了吗?你应该有一个类似于被调用的应用程序上下文的东西,并从.xml中获取信息,并取消所有的setter方法。类似的东西:

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

此外,我不确定您是否希望UserDAO作为您的媒体名称,如果这也是您的班级名称。