java - 奇怪的 Spring 上下文组件扫描

标签 java spring

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

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;
    }

}

我使用搜索管理器并测试 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">

    <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>

Now File Dao is always null, I think its not scanning the components though base package is correct. Any help or suggestion would be highly regarded and appreciated. And I know this type of question is being asked many times so apologizes for asking it again, those answer didn't work for me.

最佳答案

我相信您必须在 applicationContext 中为您想要 Autowiring 的 bean 显式声明这一点。

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

另一种方法是 Autowiring byName ,这也应该适用于您的情况(因为成员变量 fileDao 与 bean ID fileDao 具有相同的名称)。

关于java - 奇怪的 Spring 上下文组件扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086050/

相关文章:

java - 为什么 Java FTP 客户端的传输速率差异如此之大

java - 将内部可运行类导出到 jar (Clips)

java - 无法从 mybatis 执行的 oracle 过程中接收参数

JavaPOET - 只有类有父类(super class),而不是 INTERFACE

java - 比较两个不同数组列表中的字符串

java - Voldemort安装: could not find tools. jar

spring - Tomcat 中的 LTPA token (Spring security)

Java Servlet 检索 Spring RequestMapping Url

java - 用于 Hibernate 和 Oracle 的 ID 生成器 (Spring MVC)

java - 如何为整个spring mvc应用程序定义静态Map