ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring beans Context配置

2019-06-20 23:07:19  阅读:155  来源: 互联网

标签:frameworks spring


我是春豆的新手.我指的是书籍和博客.在某些情况下,上下文配置以< beans:bean>给出.在一些只是< beans>.有什么不同?我们应该在上下文文件中提供XML命名空间吗?它是否会在应用程序部署时引用该实际站点?

解决方法:

Spring而言无关紧要 – XML必须是有效的,以便Spring可以理解它,就是这样.您可以选择哪种格式.通常使用默认命名空间来避免输入太多(所有示例都基于Appendix C. XML Schema-based configuration):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="..."/>

</beans>

xmlns =“…”属性定义默认命名空间(如果你根本没有指定任何命名空间就使用的命名空间,例如< beans />.只要你只使用单个bean命名空间而偶尔使用很少的声明,这就没用了.来自其他名称空间:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <bean id="..." class="...">
      <property name="isolation">
        <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
      </property>
    </bean>

</beans>

但有时您会注意到,与默认bean命名空间相比,您使用的是来自不同命名空间的更多节点.一个很好的例子是Spring Security configuration文件:

<beans xmlns:security="http://www.springframework.org/schema/security"
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

    <security:http auto-config='true'>
        <security:intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:intercept-url pattern="/**" access="ROLE_USER" />
        <security:form-login login-page='/login.jsp'/>
        <security:session-management>
            <security:concurrency-control max-sessions="1" />
        </security:session-management>
        <security:openid-login>
            <security:attribute-exchange>
                <security:openid-attribute name="email" type="http://axschema.org/contact/email" required="true" />
                <security:openid-attribute name="name" type="http://axschema.org/namePerson" />
            </security:attribute-exchange>
        </security:openid-login>
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider user-service-ref='myUserDetailsService'/>
    </security:authentication-manager>

    <bean id="myUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>

</beans>

你看到它是多么笨拙,因为很少使用bean默认命名空间,但整个文件必须用安全性:前缀混乱?那怎么样(请注意xmlns名称空间声明如何更改):

<beans:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

    <http auto-config='true'>
        <intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/**" access="ROLE_USER" />
        <form-login login-page='/login.jsp'/>
        <session-management>
            <concurrency-control max-sessions="1" />
        </session-management>
        <openid-login>
            <attribute-exchange>
                <openid-attribute name="email" type="http://axschema.org/contact/email" required="true" />
                <openid-attribute name="name" type="http://axschema.org/namePerson" />
            </attribute-exchange>
        </openid-login>
    </http>

    <authentication-manager>
        <authentication-provider user-service-ref='myUserDetailsService'/>
    </authentication-manager>

    <beans:bean id="myUserDetailsService" class="org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl">
        <beans:property name="dataSource" ref="dataSource"/>
    </beans:bean>

</beans:beans>

这两个配置文件在语义上是等效的(它只是对相同信息进行编码的不同方式).但后者更具可读性.只需使用默认命名空间,无论使用哪个命名空间最多.

标签:frameworks,spring
来源: https://codeday.me/bug/20190620/1248798.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有