spring beans Context配置

我是春豆的新手.我指的是书籍和博客.在某些情况下,上下文配置以< 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>

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

上一篇:CakePHP到CodeIgniter的转换


下一篇:php – 来自其他实体的注册表单中的FOSUserBundle属性