spring bean 上下文配置

标签 spring frameworks

我是 Spring Bean 的新手。我指的是书籍和博客。在某些情况下,上下文配置为 <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/> 。只要您只使用单个 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 相比,您使用了更多来自不同命名空间的节点。命名空间。一个很好的例子是 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>

你看它有多笨拙,因为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>

这两个配置文件在语义上是等价的(只是对相同信息的编码方式不同)。但后者更具可读性。只需使用最常用的命名空间即可。

关于spring bean 上下文配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11510886/

相关文章:

java - 为什么 iBATIS 会给出过时的结果,即使禁用了缓存?

java - 在 spring 中使用多对一映射时获得无限的 Json 响应

javascript - 拉斐尔 Canvas (背景)onclick事件

.net - .NET framework 2.0 在 Windows XP 中是可选的吗?

java开源从xml生成sql查询

ios - 在 iOS 中创建自定义框架

用于网络邮件服务的Python网络框架

ajax - 如何将参数传递给 Thymeleaf Ajax 片段

java - 如何扫描包中的 Hibernate 实体而不是使用 hbm.xml?

spring - 如何调试不执行任何操作且不抛出异常的 Spring webflow 提交