Jump on "Spring"......

How to initialize the Spring Application Context



Configuration in Web.xml


<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/daoContext.xml  /WEB-INF/mainApp-context.xml  /WEB-INF/applicationContext.xml
   </param-value>
</context-param>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

multiple context file can be given separated by space or single context file which could import other context files in it.
One more option is there to load context with help of context loader servlet in-spite of listener as below.

<servlet> <servlet-name>context</servlet-name> 
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class> 
<load-on-startup>1</load-on-startup></servlet>


The listener inspects the 'contextConfigLocation' parameter. If the parameter does not exist, the listener will use /WEB-INF/applicationContext.xml as a default. When it does exist, it will separate the String using predefined delimiters (comma, semi-colon and whitespace) and use the values as locations where application contexts will be searched for. Ant-style path patterns are supported as well: e.g. /WEB-INF/*Context.xml (for all files whose name ends with "Context.xml", residing in the "WEB-INF" directory) or /WEB-INF/**/*Context.xml (for all such files in any subdirectory of "WEB-INF")



In Main App Context multiple context files can be imported as below. Property place holder is used to load 
properties present in application properties like server, port,url,base

daoContext.xml

<beanid="contextSource"class="org.springframework.ldap.core.support.LdapContextSource">
  <property name="url">
  <value>${ldap.url}</value>
  </property>
  <property name="base">
  <value>${ldap.base}</value>
  </property>
  </bean>


mainApp-context.xml:

<import resource="Db-context.xml" />
<import resource="Beans-context.xml" />
<import resource="Mq-context.xml" />

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:ApplicationResource.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
</bean>




ApplicationResource.properties

# LDAP
ldap.url=ldap://uvwxyz.abcdf.com:678
ldap.base=ou=people,o=intra,dc=abcdef,dc=com




Connect Spring context with Struts


In struts config.xml specify pulgin  with  ccontext file have action class bean;


<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/springappContext-servlet.xml" />
</plug-in>



define action mapping in struts config as 



<action path="/Login" name="LoginActionForm" scope="request" 
type="org.springframework.web.struts.DelegatingActionProxy" parameter="methodName">
 <forward name="success" path="/MenuSelection.do?methodName=displayScreen" />
 <forward name="error" path="/jsp/Login.jsp" />
  </action>








Comments

Popular posts from this blog

Conversion from Clob To String in java and Vice versa

Portable Java 8/ JDK8 setup without admin rights

Multi Threading , Producer consumer problem in Java