4

私は春豆の初心者です。本やブログを参考にしています。コンテキスト設定が として与えられる場合もあれば、<beans:bean>だけで与えられる場合もあり<beans>ます。違いはなんですか?コンテキスト ファイルに XML 名前空間を指定する必要がありますか? アプリ導入時に実際のサイトを参照するのですか?

4

2 に答える 2

5

Springに関する限り、それは問題ではありません。Springが理解できるように、XMLは有効である必要があります。それだけです。どのフォーマットを選択するかはあなた次第です。通常、入力しすぎないようにデフォルトの名前空間を使用します(すべての例は付録Cに基づいています。XMLスキーマベースの構成)。

<?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/>。これは、単一beansの名前空間のみを使用し、他の名前空間からの宣言をほとんど使用しない限り、問題ありません。

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

ただし、デフォルトの名前空間と比較して、さまざまな名前空間からはるかに多くのノードを使用していることに気付く場合がありbeansます。良い例は、SpringSecurity構成ファイルです。

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

beansデフォルトの名前空間はめったに使用されないので、それがいかに不器用であるかわかりますが、ファイル全体がsecurity:プレフィックスで乱雑になっている必要がありますか?これはどうですか(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>

これらの2つの構成ファイルは意味的に同等です(同じ情報をエンコードするための異なる方法です)。しかし、後者の方がはるかに読みやすくなっています。最も使用されている名前空間のデフォルトの名前空間を使用するだけです。

于 2012-07-16T19:24:00.673 に答える
1

これはXML名前空間の構成に依存し、Spring機能ではなく、コードに違いはなく、コーダーにいくらかの違いをもたらしますが、実際にはxmlns、ルートxml要素に属性を定義する効果があります。詳細について<beans:bean>は、Spring Securityのこの章を参照してください(実際にはSpring Frameworkと同じではありませんが、非常に一般的であり、XMLファイルで独自の名前空間を使用します)。あなたは書ける:

<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.1.xsd">

  <global-method-security pre-post-annotations="enabled">
    <expression-handler ref="expressionHandler"/>
  </global-method-security>

  <beans:bean id="expressionHandler" class=
        "org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <beans:property name="permissionEvaluator" ref="myPermissionEvaluator"/>
  </beans:bean>
</beans:beans>

これは次と同等です:

<beans xmlns="http://www.springframework.org/schema/beans" <!-- see the difference here? -->
       xmlns:security="http://www.springframework.org/schema/security"
       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.1.xsd">
  <security:global-method-security pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler"/>
  </security:global-method-security>

  <bean id="expressionHandler" class=
        "org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler">
    <property name="permissionEvaluator" ref="myPermissionEvaluator"/>
  </bean>
</beans>

個人的にはいつも豆をメインの名前空間として使っていますが、それは習慣の問題です。

于 2012-07-16T19:23:38.480 に答える