Just a general question, when you define a Java based configuration web app. Ie.e have a class for : ApplicationContext and a WebApplicationInitializer class.
How does Spring know it has to load the beans, as no xml config files exists.. how does tomcat know anything about the webapp without a web.xml
Its a newbie question.. i appreciate that. :)
See this blog post from SpringSource blog, important part about web.xml has an example, basically you point to JavaConfigWebApplicationContext instead of default XmlWebApplicationContext in DispatcherServlet's <init-param>:
<web-app>
<!-- Configure ContextLoaderListener to use JavaConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified #Configuration classes -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>example.RootApplicationConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.config.java.context.JavaConfigWebApplicationContext</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified #Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>example.web.WebBeansConfig</param-value>
</init-param>
</servlet>
</web-app>
I have a VERY GOOD WAY to help you learn Spring MVC if you have Maven up and running.
IF SO: go to your command line (Cygwin) I use...
mvn archetype:generate
It will ask for an 'archetype number'. For you... type 16
Enter the group ID which is just the main package.
Enter Artifact ID which is your project name.
SNAP-SHOT --- just press enter and same with version.
Package - is the same as your group ID name. EX: com.spring
Confirm it by entering the letter 'y' and press enter.
DO all of the above after your are in your workspace directory. That way it is created there.
You can do "mvn eclipse:eclipse" to load it in Eclipse OR you can just import it. I prefer the old fashioned importing an existing project.
Everything will be 'already' set up for you in terms of ALL configuration (Java-Based) which is good for you. It will have all the Maven dependencies you need as well already in your pom.xml. You can add or take from it if you want.
The point here is that you will have a running project already and you can play with it from there. I create all my projects like this at first and erase what I don't need and add what I do and then go from there.
Good luck!!!
Anywho... add this to your web.xml. This will help you in your answer. Research this below:
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Related
I have an ear file built which has a war file inside.The war has web.xml which has servlet defined:
<servlet>
<servlet-name>ExcelDownload</servlet-name>
<servlet-class>web.ExcelDownload</servlet-class>
<multipart-config>
**<!--<max-file-size>1048576</max-file-size> -->**
</multipart-config>
</servlet>
The problem here is my in ear file max-file-size is commented that means there are no attributes defined for the multipart-config. But when I deploy my ear on WebSphere 8.5.5.9, it changes my web.xml to:
<servlet>
<servlet-name>ExcelDownload</servlet-name>
<servlet-class>com.ofss.infra.web.ExcelDownload</servlet-class>
<multipart-config>
<max-file-size>0</max-file-size>
**<max-request-size>0</max-request-size>**
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
Since the max-file-size attribute is defined as zero in web.xml I am not not able to use HTTP request to upload the file to server.
I need help on to understand why the attribute is being added by WAS though its not available in ear. I did my digging I tried during deployment not to use metadata-complete attribute to false then instead of changing web.xml WAS has created a new file web_merged.xml (this has max-file-size set to zero) and am still facing the issue.
I'm trying to implement olap4j-xmlaserver to access Mondrian via HTTP/XMLA. I compiled the project, created a war file (including a web.xml file) and deployed it in Tomcat 7. Problem is that I'm getting an exception when the pivot client application sends an XMLA request:
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mondrian:Jdbc=jdbc:mysql://localhost:3306/foodmart?user=root&password=xxx;Catalog=/WEB-INF/FoodMart.xml'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at mondrian.xmla.impl.Olap4jXmlaServlet$Olap4jPoolingConnectionFactory.getConnection(Olap4jXmlaServlet.java:300)
at mondrian.xmla.XmlaHandler.getConnection(XmlaHandler.java:2917)
... 33 more
Caused by: java.sql.SQLException: No suitable driver at java.sql.DriverManager.getDriver (DriverManager.java:315)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 37 more
The web.xml I put together (I couldn't find a sample in the olap4j-xmlaserver project). I'm sure this web.xml file needs to be fixed to solve the problem, especially the connection string:
<web-app ....
<display-name>olap4j-xmlaserver</display-name>
<description>
Olap4J XML/A server
</description>
<servlet>
<servlet-name>xmla</servlet-name>
<servlet-class>mondrian.xmla.impl.Olap4jXmlaServlet</servlet-class>
<init-param>
<param-name>OlapDriverClassName</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</init-param>
<init-param>
<param-name>OlapDriverConnectionString</param-name>
<param-value>
jdbc:mondrian:Jdbc=jdbc:mysql://localhost:3306/foodmart?user=root&password=xxx;Catalog=/WEB-INF/FoodMart.xml
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>xmla</servlet-name>
<url-pattern>/xmla</url-pattern>
</servlet-mapping>
</web-app>
Any ideas how to fix the web.xml configuration or pointers to relevant documentation will be greatly appreciated.
You are missing the Java driver for MySQL in your classpath. Add it to your webapp in the folder named 'WEB-INF/lib'.
I'm trying to create a servlet which shares liferay session contents with my application.So I need to use PortalDelegateServlet.But I can not find in how to import this library to my project.I can not find any .jar files or something.
How can I import liferay java library to my project?
PortalDelegateServlet is in portal-service.jar which is a required part of the Liferay container. If you grabbed a bundle (which in comment you mention the Tomcat bundle), then it is provided for you. All you should need to do is configure your web.xml:
<servlet>
<!-- http://issues.liferay.com/browse/LEP-2297 -->
<servlet-name>service</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalDelegateServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>com.example.MyServlet</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
This Liferay issue (which is linked to in the source for PortalDelegateServlet) makes it sound as if this has been available since version 4.3.0
I would like to load inside my Spring MVC Web Application (packaged as WAR) some Spring framework beans annotated with #Service from an external jar, which is in charge of accessing a database and located in the classpath under /WEB-INF/lib. If possible, it would be desirable to load them automatically using the #Autowired annotation.
I have followed successfully the solution in this link1:
this.ctx = new ClassPathXmlApplicationContext("services-context.xml");
this.myAService = ctx.getBean("myAService");
However, this solution uses Spring API function getBean which is considered a bad practice (see link2).
I also tried, without luck two more things to load the external jar´s applicationContext:
WAR´s appContext.xml:
<import resource="classpath*:/WEB-INF/lib/pathToExternalJar/applicationContext.xml">
WAR´s web xml -> load the jar´s appContext as described here (link3). (e.g. *applicationContext.xml):
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:localSpringContext.xml
classpath:*applicationContext.xml
</param-value>
</context-param>
What is the best approach to load those beans properly and how should it be done?
WAR´s appContext.xml and WAR´s web xml are both feasible. If you need to run integration tests based on both localSpringContext.xml and external jar's applicationContext.xml frequently, I recommend the WAR´s appContext.xml approach.
Updated1:
WAR´s appContext.xml:
<import resource="classpath:{classpath}/applicationContext.xml"/>
WAR´s web xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:localSpringContext.xml
classpath:{classpath}/applicationContext.xml
</param-value>
</context-param>
For example, if your applicationContext.xml is under package :com/gmail/hippoom
you can get it by classpath:com/gmail/hippoom/applicationContext.xml or classpath*:applicationContext.xml with wildcard.
Everything was working properly until i added a new java package to my project, when i run my application the stack trace shows java.lang.ClassNotFoundException, i had to reference another class library as i could not figure out how to compile fresh new classes using eclipse. I added an external folder to my build path to temporarily fix the problem but now i am faced with the same problem.
Stack Trace and web.xml are below:
My question is, how do you do compile a project to create new classes using eclipse?
Caused by: java.lang.ClassNotFoundException: org.bixin.dugsi.security.JpaRealm
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:257)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:408)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1271)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1242)
... 38 more
WEB XML:
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Dugsi_Manager Vaadin Application Servlet</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>org.bixin.dugsi.web.DugsiManagerApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Dugsi_Manager Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Did a mvn clean and a mvn build to clear the cache of all old classes and create new classes