为什么这个dao类不会自动装配? [英] Why won't this dao class autowire?

查看:145
本文介绍了为什么这个dao类不会自动装配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



商务舱





< pre class =lang-java prettyprint-override> @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());
}
}

application-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>联合国< / value>
< / property>
< property name =password>
< value> pw< / value>
< / property>
<! - 禁用二级缓存 - >
<! - 回显所有已执行的SQL到stdout - >
< / bean>
< bean id =sessionFactoryclass =org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
< property name =dataSourceref =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 =mySecurityInfoDAOclass =com.projectname.dao.SecurityDAOImpl>
< property name =sessionFactoryref =sessionFactory/>
< / bean>

< / beans>

spring-servlet.xml

 <?xml version =1.0encoding =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 =prefixvalue =/ WEB-INF / views //>
< property name =suffixvalue =。jsp/>
< / bean>

< bean id =messageSourceclass =org.springframework.context.support.ReloadableResourceBundleMessageSource>
< property name =basenames>
< value> / WEB-INF / messages / messages< / value>
< / property>
< property name =cacheSecondsvalue =60/>
< property name =defaultEncodingvalue =UTF-8/>
< / bean>
< / beans>


解决方案

让我猜: AuthenticatedUser 是域实体吗?



Spring只能处理Spring Beans,只能注入Spring Beans。 / p>

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



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




  • add @Configurable 到实体

  • 打开Spring Configurable支持:

  • 启用AspectJ(AspectJ不是Spring AOP) - 编译时或加载时编织。 - 如果你使用编译时编织你需要使用AspectJ编译器



@See:




Please look at the files below and tell me why the Dao will not autowire. It autowires correctly when the same setter is put in a controller. I put it in this class and it does not work.

Business Class

@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());     
    }
}

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

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

解决方案

Let my guess: AuthenticatedUser is a Domain Entity?

Spring can only handle Spring Beans, and can only inject in Spring Beans.

If you have a class that is instantiated in a Pojo way by new (or loaded form the database by Hibernate/JPA,...) it does not become a Spring Bean.

But you can make even this Pojos become Spring Beans. You need 3 things:

  • add @Configurable to the Entity
  • turn on Spring Configurable support:
  • enable AspectJ (AspectJ not Spring AOP) - compiletime or loadtime weaving. -- If you use compiletime weaving you need to use an AspectJ Compiler

@See:

这篇关于为什么这个dao类不会自动装配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆