如何跨不同的xml文件访问bean

时间:2011-03-28 13:52:20

标签: java hibernate spring

我的spring hibernate应用程序中有三个xml文件

弹簧security.xml文件

<security:authentication-manager>
            <security:authentication-provider user-service-ref="customUserDetailsService">

            </security:authentication-provider>
    </security:authentication-manager>

    <!-- Use a Md5 encoder since the user's passwords are stored as Md5 in the database -->
    <bean class="org.springframework.security.authentication.encoding.Md5PasswordEncoder" id="passwordEncoder"/>

    <!-- A custom service where Spring will retrieve users and their corresponding access levels  -->
    <bean id="customUserDetailsService" class="com.vaannila.service.CustomUserDetailsService" >

     </bean>

冬眠-context.xml中

enter code here

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close"
            p:driverClass="${app.jdbc.driverClassName}"
            p:jdbcUrl="${app.jdbc.url}"
            p:user="${app.jdbc.username}"
            p:password="${app.jdbc.password}"
            p:acquireIncrement="5"
            p:idleConnectionTestPeriod="60"
            p:maxPoolSize="100"
            p:maxStatements="50"
            p:minPoolSize="10" />

<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
            p:sessionFactory-ref="sessionFactory" />

    <bean id="registrationDAO" class="com.vaannila.dao.RegistrationDAOimpl" >
  <constructor-arg ref="sessionFactory"/>
 </bean>

现在我的春天安全,我想要像

这样的东西
<bean id="customUserDetailsService" class="com.vaannila.service.CustomUserDetailsService" >
     <constructor-arg ref="registrationDAO"/>
     </bean

但是我的registrationDAO在hibernate-config中,当我在spring Security中这样做时,它说没有bean命名为注册DAO

1 个答案:

答案 0 :(得分:1)

Spring支持跨外部jar读取应用程序上下文。只需在上下文文件名中添加“classpath:”前缀即可。 Spring将在整个项目中寻找它。

例如,如果您正在创建Web应用程序,则可以声明您的业务逻辑应用程序上下文(web.xml)

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml <!-- tell Spring to look for context defined on the classpath -->
    </param-value>
</context-param>

这样你就可以根据需要使用尽可能多的上下文。