为什么这个dao类不会自动装配?

时间:2011-06-21 05:17:51

标签: java spring

请查看下面的文件并告诉我为什么Dao不会自动装配。当同一个setter放入控制器时,它会正确自动装配。我把它放在这个课程中,它不起作用。

商务舱

@Component
public class AuthenticateUser {

    @Autowired
    private SecurityDAO securityDAO;

    public void setSecurityDAO(SecurityDAO securityDAO) {
        this.securityDAO = securityDAO;
    }

    protected void execute() {          
        User authenticatedUser = securityDAO.login(get_userName(),
                                                   get_password());     
    }
}

应用context.xml中

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <bean id="myDataSource" 
    class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
      <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
      </property>
      <property name="url">
        <value>jdbc:mysql://localhost/dbname</value>
      </property>
      <property name="username">
        <value>un</value>
      </property>
      <property name="password">
        <value>pw</value>
      </property>
      <!-- Disable the second-level cache  -->
        <!-- Echo all executed SQL to stdout -->
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" /> 
        <property name="annotatedClasses">
            <list>
            <value>com.projectname.model.SecurityInfo</value>
            <value>com.projectname.model.User</value>
            <value>com.projectname.model.Post</value>
            <value>com.projectname.model.Article</value>
            <value>com.projectname.model.Address</value>
            </list>
        </property> 
        <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
        </property>
    </bean>


    <bean id="mySecurityInfoDAO" class="com.projectname.dao.SecurityDAOImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    </beans>

弹簧servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan
    base-package="com.projectname" />
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <value>/WEB-INF/messages/messages</value>
    </property>
    <property name="cacheSeconds" value="60" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>
</beans>

1 个答案:

答案 0 :(得分:8)

我猜:AuthenticatedUser是域实体吗?

Spring只能处理Spring Beans,只能注入Spring Bean。

如果你有一个由new以Pojo方式实例化的类(或者由Hibernate / JPA从数据库加载,......),它就不会成为一个Spring Bean。

但你甚至可以让这个Pojos成为Spring Beans。你需要3件事:

  • @Configurable添加到实体
  • 启用Spring Configurable支持:
  • 启用AspectJ(AspectJ而不是Spring AOP) - 编译时或加载时编织。 - 如果使用编译时编织,则需要使用AspectJ编译器

@see: