Spring beans Context Configuration

I am new to spring beans. I mean books and blogs. In some configuration of the context it is indicated as <beans:bean> and in some simply <beans> . What is the difference? Should we provide an XML namespace in the context file? Will it link to the acutal site during application deployment?

+4
source share
2 answers

It doesn't matter with Spring - XML โ€‹โ€‹must be valid so that Spring can understand it what it is. It is up to you which format you choose. Usually you use the default namespace to not print too much (all examples are based on Appendix C. Configuration based on XML schema ):

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

xmlns="..." defines the default namespace (the one used if you donโ€™t specify any namespace at all, for example <beans/> . This is fine if you use only one beans namespace and sometimes several declarations from other Namespaces:

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

But sometimes you will notice that you are using more nodes from different namespaces than the default beans namespace. A good example is Spring Configuration 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> 

You see how inconvenient this is because the beans namespace is so rarely used by default, but the whole file must be cluttered with the security: prefix? What about this (note how xmlns declarations have changed):

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

These two configuration files are semantically equivalent (this is just another way of encoding the same information). But the latter is much readable. Just use the default namespace, which namespace is used the most.

+4
source

It depends on the configuration of the XML namespace and is not a Spring function, it does not matter for the code, it has some meaning for the encoder, but in fact, it does affect the definition of the xmlns attribute on the root xml element. Read more about the <beans:bean> in this Spring Security chapter (which doesn't really match the Spring Framework, but it is very common and uses its own namespace in XML files). You can write:

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

which is equivalent to:

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

Personally, I always use beans as the main namespace, but this has to do with the habit.

+1
source

All Articles