0

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

弹簧上下文

 <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 对象的代码是否为空

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

现在 File Dao 总是为空,我认为它没有扫描组件,虽然基本包是正确的。任何帮助或建议都将受到高度重视和赞赏。而且我知道这种类型的问题被问了很多次,所以很抱歉再次问这个问题,这些答案对我不起作用。

4

4 回答 4

2

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

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

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

于 2013-08-06T16:59:28.320 回答
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中有描述

于 2013-08-06T17:02:32.603 回答
0

您在这里有一些选择,您可以使用以下方式注释您的课程SearchManager

  • @Component

  • @Configurable- @Configurable 用于标记符合 Spring 依赖注入的类。当您不能或无意将您的类用作 Spring Bean 时,应该使用它(自 Spring 2.0 起可用)

还有其他方法可以连接您的课程,但您可能会失去一些好处。

您还应该删除 xml 声明:

<bean id="searchManager" class="com.bakaenterprise.bl.SearchManager" />
于 2013-08-06T17:18:03.953 回答
0

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

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

编辑:

FileUploadDao定义了两次,一个带有@Component注释的 bean,第二个带有 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

EDIT-3

您需要自动装配SearchManager

  public SearchServlet extends HttpServlet {

    @Autowired
    private SearchManager searchManager;

    public void init(ServletConfig config) {
      super.init(config);
      SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
        config.getServletContext());
    }
  }
于 2013-08-06T17:03:16.067 回答