奇怪的Spring Context组件扫描

时间:2013-08-06 16:54:26

标签: java spring

我已经从stackoverflow找到了许多我的查询的解决方案,这是我第一次在这里问一个问题而且我真的不知道这有什么问题。实际上我正在尝试按类型自动连接我的一个类,但无法做到这一点。以下是顺序源代码。

  

Spring Context

 <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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="/WEB-INF/mysqlConfig.properties" />

<bean id="sessionManager" class="com.bakaenterprise.dal.SessionManager">
    <property name="sessionFactory" ref="sessionFactory" />

</bean>

<context:annotation-config />
<context:component-scan base-package="com.bakaenterprise" />

<bean id="searchManager" class="com.bakaenterprise.bl.SearchManager" />

<bean id="fileDao" class="com.bakaenterprise.dal.impl.FileUploadDao" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">

        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialSize" value="10" />
        <property name="maxActive" value="5" />
        <property name="maxWait" value="5000" />
    </bean>

    <!-- Hibernate Configuration -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:dataSource-ref="dataSource" p:packagesToScan="com.bakaenterprise.beans">

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.MySQLDialect
                </prop>
                <prop key="hibernate.show_sql">
                    true
                </prop>
                <prop key="hibernate.generate_statistics">
                    true
                </prop>
            </props>
        </property>
    </bean>

</beans>

接口

package com.bakaenterprise.dal;

import com.bakaenterprise.beans.FileUploadBean;
import com.bakaenterprise.core.base.GenericDao;

public interface IFileUploadDao extends GenericDao<FileUploadBean> {

}

实施

package com.bakaenterprise.dal.impl;

    import com.bakaenterprise.beans.FileUploadBean;
    import com.bakaenterprise.core.base.HibernateDaoSupport;
    import com.bakaenterprise.dal.IFileUploadDao;
    import java.io.Serializable;
    import java.util.List;
    import org.springframework.stereotype.Component;

    /**
     * @author ali
     */
    @Component
    public class FileUploadDao extends HibernateDaoSupport<FileUploadBean> implements IFileUploadDao {

      @Override
      public boolean save(FileUploadBean obj) {
        super.save(obj);
          return true;
      }

      @Override
      public FileUploadBean getRecordById(Serializable id) {
        return super.getRecordById(id);
      }

      public boolean deleteRecordById(int id){
        return super.deleteById(id);
      }


      @Override
      public List<FileUploadBean> listAll() {
        return super.listAll();
      }

    }

经理类

package com.bakaenterprise.bl;

package com.bakaenterprise.bl;

import com.bakaenterprise.dal.IFileUploadDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author ali
 */
@Component
public class TestManagerImpl implements ITestManager {
 @Autowired
    private IFileUploadDao fileDao;

    @Override
    public void test() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
@Override
    public IFileUploadDao getFileDao() {
        return fileDao;
    }
@Override
    public void setFileDao(IFileUploadDao fileDao) {
        this.fileDao = fileDao;
    }

}

我正在使用Search Manager并测试FileUploadDao对象的代码为null或不是

@Autowired
private ITestManager testManager;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {

            IFileUploadDao fileUploadDao = testManager.getFileDao();
            // now testManager is null

}

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd“&GT;

    <servlet>
        <servlet-name>SearchServlet</servlet-name>
        <servlet-class>com.bakaenterprise.server.SearchServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SearchServlet</servlet-name>
        <url-pattern>/servlets/SearchServlet</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <jsp-config>
        <taglib>
            <taglib-uri>/jstl/core_rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/c.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/jstl/xml_rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/x_rt.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/jstl/fn_rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fn.tld</taglib-location>
        </taglib>
        <taglib>
            <taglib-uri>/jstl/fmt_rt</taglib-uri>
            <taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
        </taglib>
    </jsp-config>


    <filter>
        <filter-name>performance</filter-name>
        <filter-class>com.bakaenterprise.util.PerformanceLog</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>performance</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/springContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
   </listener-class>
    </listener>
</web-app>
  

现在File Dao总是为null,我认为虽然基础包是正确的,但它不会扫描组件。任何帮助或建议都将受到高度重视和赞赏。而且我知道这种类型的问题被多次询问,因此再次提出问题道歉,这些答案对我不起作用。

4 个答案:

答案 0 :(得分:2)

我相信您必须在applicationContext中为要自动装配的bean明确声明这一点。

<bean id="searchManager" class="com.bakaenterprise.bl.SearchManager" autowire="byType"/>

另一种方法是自动装配byName,它也适用于您的情况(因为成员变量fileDao与bean ID fileDao具有相同的名称)。

答案 1 :(得分:2)

SearchManager也应注释为在组件扫描期间注入的IFileUploadDao实现bean

@Component
public class SearchManager {

此外,您手动初始化SearchManager将导致其依赖关系未被注入 - 该bean需要由Spring管理。由于没有直接的方法将Spring Beans注入Java Servlet,因此可以使用Spring框架HttpRequestHandler

public class AnnotatedHttpServletRequestHandler implements HttpRequestHandler {

   @Autowired
   private SearchManager searchManager;
   ...
}

具体细节在Injecting Spring Beans into Java Servlets

中描述

答案 2 :(得分:0)

尝试在spring上下文配置中添加依赖属性:

<bean id="searchManager" class="com.bakaenterprise.bl.SearchManager" 
      depends-on="fileDao" />

修改

FileUploadDao定义了两次,一个bean有@Component注释,第二个有XML:

<bean id="fileDao" class="com.bakaenterprise.dal.impl.FileUploadDao" />

所以,你只需要创建一个bean的两个副本!我认为这是一个问题。

修改-2

尝试删除此XML定义:

<bean id="searchManager" class="com.bakaenterprise.bl.SearchManager" />

<bean id="fileDao" class="com.bakaenterprise.dal.impl.FileUploadDao" />

并将@Component注释添加到SearchManager

修改-3

您需要自动装配SearchManager

  public SearchServlet extends HttpServlet {

    @Autowired
    private SearchManager searchManager;

    public void init(ServletConfig config) {
      super.init(config);
      SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
        config.getServletContext());
    }
  }

答案 3 :(得分:0)

您可以在此处选择一些选项,您可以使用以下代码注释班级SearchManager

  • @Component

  • @Configurable - @Configurable用于标记符合Spring依赖注入条件的类。如果您不能或不打算将您的类用作Spring Bean (从Spring 2.0开始提供)

  • ,则应该使用它。

还有其他方法可以为您的课程提供帮助,但您可能会失去一些好处。

您还应该删除xml声明:

<bean id="searchManager" class="com.bakaenterprise.bl.SearchManager" />