importing XML config into java config based (annotation) project - spring

I'm working on Spring MVC annotation based application. I have web.xml file entry as follows (using WebConfig.java for configuration):
<servlet>
<servlet-name>sdsdispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.conf.WebConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Now when I try to integrate security related XML file I'm facing following error
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638)
I tried to import xml file as follows:
#Configuration
#EnableWebMvc
#ComponentScan("com.stk.controller")
#ImportResource({"securityContext.xml"})
public class WebConfig extends WebMvcConfigurerAdapter {
Screenshot
http://i.stack.imgur.com/BosWG.jpg

Add classpath as shown below, provided its in classpath
#ImportResource("classpath:securityContext.xml")
If you have multiple configuration files then
#ImportResource(locations={"classpath:securityContext.xml","file://c:/test-config.xml"})
You can access the file from WEB-INF directory using
#ImportResource("file:**/WEB-INF/securityContext.xml")
However I would recommend you to move configuration file to src/main/resource directory and use the file line that is loading file using classpath. These files will be copied to WEB-INF/classes directory during packing of war by maven which is classpath

Related

Spring container is looking at wrong path while loading multiple spring configuration files

We are using Spring framework in the project. I am loading applicationContext file from web.xml in the following way:
<servlet>
<servlet-name>SpringDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/CustomerService/applicationContext.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcherServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I am trying to load one configuration file JobDesigner-springintegration.xml inside the applicationContext file in the following way:
<import resource="JobDesigner-springintegration.xml"/>
Now, I am trying to load few more configuration files inside the
`JobDesigner-springintegration.xml` file in the following manner:
<import resource="*.xml"/>
But weirdly the spring container is looking in a wrong path something like below:
jndi:/localhost/cciupg/WEB-INF/CustomerService/
The log excerpts is shown below:
12:52:15,202 INFO [STDOUT] 12:52:15,202 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/CustomerService/JobDesigner-springintegration.xml]
12:52:15,453 INFO [STDOUT] 12:52:15,448 WARN [PathMatchingResourcePatternResolver] Cannot search for matching files underneath URL [jndi:/localhost/cciupg/WEB-INF/CustomerService/] because it does not correspond to a directory in the file system
java.io.FileNotFoundException: URL [jndi:/localhost/cciupg/WEB-INF/CustomerService/] cannot be resolved to absolute file path because it does not reside in the file system: jndi:/localhost/cciupg/WEB-INF/CustomerService/
at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:205)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:52)
at org.springframework.core.io.UrlResource.getFile(UrlResource.java:169)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.doFindPathMatchingFileResources(PathMatchingResourcePatternResolver.java:526)
at org.springframework.web.context.support.ServletContextResourcePatternResolver.doFindPathMatchingFileResources(ServletContextResourcePatternResolver.java:92)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.findPathMatchingResources(PathMatchingResourcePatternResolver.java:347)
at org.springframework.core.io.support.PathMatchingResourcePatternResolver.getResources(PathMatchingResourcePatternResolver.java:279)
at org.springframework.context.support.AbstractApplicationContext.getResources(AbstractApplicationContext.java:1269)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:208)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:261)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:197)
at org.springframework.beans.factory.xml.DefaultBeanDe
The question here is why the spring container is looking at wrong path jndi:/localhost/cciupg/WEB-INF/CustomerService/ instead of /WEB-INF/CustomerService/?
And the weird part is why it is appending jndi:/localhost/cciupg to the path?
WEB-INF is not part of the classpath I believe. Therefore it cannot find the your *.xml files. I believe jndi is likely the last location spring has checked when throwing the error after looking down the classpath.
In other words, try putting your files in the classpath. In java projects I usually see resources in the main -> resources path.

(Spring) class path resource exception java.io.FileNotFoundException

I have aded following code to my web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:com/neelamhotel/mavenwebproject5/configs/dao-context.xml
</param-value>
But I am getting error for the same as
IOException parsing XML document from class path resource [com/neelamhotel/mavenwebproject5/configs/dao-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [com/neelamhotel/mavenwebproject5/configs/dao-context.xml] cannot be opened because it does not exist
Project Structure
Explorer View
I know dao-context.xml file is there. But then why it is giving an error ?
Updated Explorer view
Created WAR file view
In a Maven project, src/main/java is for Java source files. The other files are ignored by Maven.
Resources (i.e. non-Java files) that must be copied and be available in the classpath must go in src/main/resources.
Gradle uses the same conventions.

Idea tomcat run configuration cannot find xml files of spring

I am using local tomcat server run configuration in idea. My code works just fine if I deploy it to the server through manager. However if I run it on idea it gives the following error :
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
I have this setting in web.xml :
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
I have my mvc-dispatcher-servlet.xml in src/main/webapp/WEB-INF folder. What could be causing this?
Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.
See this link
The file name must be mvc-dispatcher-servlet.xml and not mvc-dispather-servlet.xml.

Servlets in Maven

In the Maven Webapp project, I created the servlet DiaServlet in the root directory of the project, i.e., the directory where pom.xml is.
Its description in web.xml is as follows:
<servlet>
<servlet-name>DiaServlet</servlet-name>
<servlet-class>DiaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DiaServlet</servlet-name>
<url-pattern>/DiaServlet</url-pattern>
</servlet-mapping>
To this, I am getting the error:
java.lang.ClassNotFoundException: DiaServlet
One thing I tried is changing <servlet-class> tag to
<servlet-class>Prj.base.DiaServlet</servlet-class>
Here, Prj.base is the groupId of the project in the pom.xml file-- if this would be any concern(?)
<groupId>Dia.base</groupId>
The same servlet web.xml definition worked as is in a "non-Maven" web application.
When I created the servlet (I'm using NetScape), the servlet description wasn't added to the web.xml file although I'd checked to add it automatically.
Is this a problem of the build environment, or my servlet definition in web.xml (if so, how come the same definition worked in the other web application?) or something else?
new to Maven
TIA
You are a maven user, at the time of initialization of your project, you are giving package name as Dia.base, so your classes are resides in the Dia.base package only
So,
<servlet>
<servlet-name>DiaServlet</servlet-name>
<servlet-class>Dia.base.DiaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DiaServlet</servlet-name>
<url-pattern>/DiaServlet</url-pattern>
</servlet-mapping>
you need to give the fully qualified class name in the <servlet-class>

Spring-ws on Weblogic: MessageDispatcherServlet doesn't have a default constructor

I'm trying to deploy a war containing spring webservices. But I am getting a weird error like this:
following error weblogic.application.ModuleException: [HTTP:101216]Servlet: "spring-ws" failed to preload on startup in Web application: "myApp-1.0.0-SNAPSHOT.war".
javax.servlet.ServletException: Servlet class: 'org.springframework.ws.transport.http.MessageDispatcherServlet' doesn't have a default constructor
at weblogic.servlet.internal.StubSecurityHelper$ServletInitAction.run(StubSecurityHelper.java:261)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.StubSecurityHelper.createServlet(StubSecurityHelper.java:64)
at weblogic.servlet.internal.StubLifecycleHelper.createOneInstance(StubLifecycleHelper.java:58)
at weblogic.servlet.internal.StubLifecycleHelper.<init>(StubLifecycleHelper.java:48)
at weblogic.servlet.internal.ServletStubImpl.prepareServlet(ServletStubImpl.java:539)
at weblogic.servlet.internal.WebAppServletContext.preloadServlet(WebAppServletContext.java:1976)
at weblogic.servlet.internal.WebAppServletContext.loadServletsOnStartup(WebAppServletContext.java:1950)
I've tried giving a default constructor in the endpoint class.
Below are the steps which I followed:
web.xml has this entry:
<servlet>
<servlet-name>spring-ws</servlet-name>
<servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
<init-param>
<param-name>transformWsdlLocations</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-ws</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
web-inf folder has spring-ws-servlet.xml file which has the entries for component scan, enabling the annotations and the wsdl entry like this:
<sws:static-wsdl id="MyService"
location="classpath:/services/myService/1.0/MyService.wsdl" />
Please let me know if I'm missing anything here....
Thank you.
It is likely that the jar files Weblogic is using to initialize the Spring classes and those expected to be used are in conflict.
To force the Weblogic container to use the jar files bundled with the war file you have to specify a WEB-INF/weblogic.xml file with the following content
<weblogic-web-app>
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>
see this thread

Resources