【问题标题】:Why won't this dao class autowire?为什么这个 dao 类不会自动装配?
【发布时间】:2011-06-21 05:17:51
【问题描述】:

请查看下面的文件并告诉我为什么 Dao 不会自动装配。当将相同的设置器放入控制器时,它会正确自动连接。我把它放在这个类中,它不起作用。

商务舱

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

应用程序上下文.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>

【问题讨论】:

  • 你能显示AuthenticateUser所在的包名吗?
  • 或者更好的是,你能确认AuthenticateUser这个类实际上是由spring实例化的吗?
  • “它不起作用”没有帮助。告诉我们会发生什么。
  • 提示:如果你用 (@Autowired, @Ressoucce, @Inject) 注释了一个字段,注入会直接进入这个字段,而不是通过 setter。
  • 要通过设置器获取值,请将注释放在设置器上而不是字段上。

标签: java spring


【解决方案1】:

我猜:AuthenticatedUser 是域实体?

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

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

但是你甚至可以让这个 Pojos 变成 Spring Beans。你需要三样东西:

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

@见:

【讨论】:

  • 他正在使用组件扫描来扫描带有@Component 注释的类。如果配置正确,应该没问题。
  • 如果需要精细注入,@Configuration注解的类可以提供额外的bean;只需用@Bean 注释相关的工厂方法即可。由于该配置类本身也是一个组件,因此您可以将其自己的配置注入其中;这可以很好地解决棘手的注入问题。
  • @Donal Fellows:有没有什么办法可以让 Hibernate 成为 bean factory?
  • @M Platvoet:这就是我开始的原因:“让我猜一下:AuthenticatedUser 是一个域实体?”然后解释了为什么默认情况下它不起作用。
  • 不知道。我改为通过 JDO 处理我的(相对微不足道的)持久性问题,因为当时我可以足够详细地解决这个问题。
猜你喜欢
  • 2012-04-30
  • 1970-01-01
  • 2018-01-27
  • 1970-01-01
  • 1970-01-01
  • 2021-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多