【问题标题】:How to access beans in across different xml files如何跨不同的xml文件访问bean
【发布时间】:2011-03-28 13:52:19
【问题描述】:

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

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

休眠上下文.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中这样做时,它说没有名为registration DAO的bean

【问题讨论】:

  • 如何在web.xml 中指定它们?
  • 其实我忘了在里面写hibernate-context文件。但现在它的工作

标签: java hibernate spring


【解决方案1】:

Spring 支持跨外部 jar 读取应用程序上下文。只需将“类路径:”前缀添加到上下文文件名。 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>

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

【讨论】:

    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多