java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory - java

I have very simple NetBean Project. It include this controlller line,
#RequestMapping(value = "/MyDoc.htm", method = RequestMethod.POST)
public String FormUpload(#RequestParam("file") MultipartFile file) {
return "MyDoc";
}
and in dispatcher servelet I have,
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="50000000"/>
But I am getting this error java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
The project include just a single controller with two methods. GET and POST. GET version works very fine.

It looks like one of the packages you are using has a dependency on one of the apache commons software packages. You can either add it to your classpath manually, or use a build tool like maven, ant, ivy etc.

Related

How to get the app version into a bean?

I have a maven spring application and want to get the application version into the spring context.xml file.
My pom.xml file is setup to build war files with the implementation version in the MANIFEST file.
I can get the implementation version at runtime like so:
String appVersion = MyApp.class.getPackage().getImplementationVersion();
But I need it when creating a bean in the applicationContext.xml file.
<bean id="myApp" class="MyApp">
<constructor-arg type="java.lang.String" name="Version" value="version_from_manifest"/>
</bean>
I thought of using factory bean and method but can't figure out how to call
SomeClassInMyPackage.class.getPackage().getImplementationVersion()
In you application.properties add a placeholder application.version.number that will be passed in from Maven.
version.number=#application.version.number#
Added the placeholder as new property in you pom.xml
<application.version.number>${version}</application.version.number>
Inject the new config into the Spring bean you need
#Value("${version.number}")
private String versionNumber;
https://serradev.wordpress.com/2016/02/02/app-version-number-in-java-code-with-maven-and-spring-boot/

How to get hibernate mapping files from another project using xml based config?

I'm working with spring and hibernate. Currently I have the context config file like this
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- other properties -->
<property name="mappingDirectoryLocations" value="classpath:mappings/" />
</bean>
the *.hbm.xml mappings are in the same project.
Now I plan to pull some entities together with the mappings out, so they can be shared with other projects. The question is, how should I configure the sessionFactory bean to get *.hbm.xml files from the newly created project?
I tried mappingJarLocations but got error saying that the class path is not valid.
Instead of classpath: use classpath*:.
Check What is the difference between "classpath:" and "classpath:/" in Spring XML? for a extended answer on the differences between the 2.
AFASIK, Hibernate looks for mentioned hbm files in all the jars in classpath. You need to mention only the files.

Autowiring a Spring 3.1 standalone application returning "No unique bean of type [x.x.x.x] is defined" exception

I am writing a standalone application (not web based) which will be run from the command prompt using the command:
java -jar myapplication.jar
I developed the application on eclipse as a Maven project so Eclipse retrieves all the dependant libraries. If i right click the main class and select "Run As ">"Java Application" it works fine.
Now the problem I have is I need to deploy the application as a single jar file. To do this, I used Eclipse to export the application as a "Runnable Jar" (i.e via the "export command"). This generated the jar file. I looked into the jar file and all the classes and the dependent jar files are in the jar file.
The Spring application context file is also in the jar file in the top level folder. The "inside" of the jar file looks like this:
- com
- myapp
- service
- MyAppService.class
- dao
- MyAppDataDao.class
- MyMainClass.class
- META-INF
- org
- application-context-components.xml
- log4j.properties
- [several jar files for spring, log4j, velocity etc)
I tried running the jar file using the following command and it gave me this error:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.myapp.MyMainClass] is defined: expected single bean but found 0:
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:257)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1012)
at com.myapp.MyMainClass.init(MyMainClass.java:44)
at com.myapp.MyMainClass.main(MyMainClass.java:65)
... 5 more
The file com.myapp.MyMainClass is in the jar file with the correct name. The classes in the package are autowired. I think that i must have missed something in the annotations or maybe something in the application context file. The structure of the classes and the annotations used is shown below:
MyMainClass
#Component
public class MyMainClass{
#Autowired
MyAppService myAppService;
public static void main(String args[]){
try{
context = new ClassPathXmlApplicationContext(properties.get("app.context"));
MyMainClass mymainClass = context.getBean(MyMainClass.class);
mymainClass.myAppService.getData()....
....
}catch(Exception e){
throw new CWAException(fname + ":" + e);
}
}
}
The app.context property returns the name of the application context file.
MyAppService
#Service
public class MyAppService{
#Autowired
MyAppDataDao myAppDataDao;
---
---
---
}
MyAppDataDao
#Repository("myAppDataDao;")
public class MyAppDataDao {
getData(){
}
---
---
---
}
The application context file looks like this
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Auto scan the components -->
<context:component-scan base-package="com.myapp" />
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"
p:resourceLoaderPath="file://C:\template"
p:preferFileSystemAccess="true"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
<property name="url"><value>jdbc:oracle:thin:#x.x.x.x:x</value></property>
<property name="username"><value>xxx</value></property>
<property name="password"><value>xxx</value></property>
</bean>
</beans>
Looking at the error I would guess that the Autowiring is not kicking in but I cant figure out where in the configuration I got it wrong. The application is in the packaged jar file and I am loading the file using ClassPathXmlApplicationContext so it should find it. I also don't understand why it works on eclipse but not after it has been exported.
Any ideas?
The class PathMatchingResourcePatternResolver in Spring3.0 atleast does not search for the classes marked with autowiring annotation inside jars.
Add the directory in which the jar is present to the classpath.
It will detect the classes and load as beans.

Issue with a clientApplicationContext xml file

I'm running a tutorial I got off the web and I'm getting an error:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'orderService' defined in
class path resource [clientApplicationContext.xml]:
Invocation of init method failed;
nested exception is javax.xml.ws.WebServiceException:
Failed to access the WSDL at: http://localhost:8080/services/order?WSDL.
It failed with:
http://localhost:8080/services/order?WSDL.
It's for Spring 2.5, Tomcat 7, Eclipse Helios and java 1.6.
All I did was change this value from port 9090 to 8080:
<property name="wsdlDocumentUrl"
value="http://localhost:8080/services/order?WSDL"/>
I have the file in two places: under java resources and also under src. I used the defaults for the app code as I just pulled it into my project and the port number is the only thing I changed, other than creating a new dynamic web project in eclipse.
In the main method here is the offending code:
ApplicationContext ctx =
new ClassPathXmlApplicationContext("clientApplicationContext.xml");
There is an applicationContext.xml file under web-inf that I added my bean definition to:
<bean id="orderService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean" >
<property name="serviceInterface" value="com.javacoda.jaxws.order.client.OrderService"/>
<property name="wsdlDocumentUrl" value="http://localhost:8080/services/order?WSDL"/>
<property name="namespaceUri" value="com.javacoda.jaxws.order"/>
<property name="serviceName" value="DefaultOrderServiceService"/>
<property name="portName" value="DefaultOrderServicePort"/>
</bean>
Looks right, so what am I doing wrong here?
It tells you that it:
Failed to access the WSDL at: http://localhost:8080/services/order?WSDL
Can you access this WSDL from the browser?
Look at the setter of the WSDL:
/**
* Set the URL of the WSDL document that describes the service.
*/
public void setWsdlDocumentUrl(URL wsdlDocumentUrl) {
this.wsdlDocumentUrl = wsdlDocumentUrl;
}
There is no magic here => it expects a WSDL to be at that location.
You can publish WSDL dynamically:
<sws:dynamic-wsdl id="holiday"
portTypeName="HumanResource"
locationUri="/holidayService/"
targetNamespace="http://mycompany.com/hr/definitions">
<sws:xsd location="/WEB-INF/hr.xsd"/>
</sws:dynamic-wsdl>
or statically:
<sws:static-wsdl id="orders" location="/WEB-INF/wsdl/orders.wsdl"/>
Read more about "Publishing the WSDL" and "Automatic WSDL exposure"
I think you should use class path prefix that should solve the problem, If you use class path prefix java run time will find the context file under src/main/resources
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:clientApplicationContext.xml")

How to substitute a variable with real value in a xml file , which is in another dependency jar file by Maven

We have a project called web-app1 and has a dependency on another jar file called core-app.jar which is provided by another team as a shared library , yet there is a hibernate.cfg.xml in this core-app.jar (inside of the jar), with content as below.
<hibernate-configuration>
<session-factory>
<property name="dialect">${hibernate.dialect}</property>
<property name="query.substitutions"><![CDATA[false 'N', true 'Y']]></property>
<property name="show_sql">false</property>
<property name="format_sql">false</property>
<property name="use_sql_comments">false</property>
<property name="generate_statistics">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<!-- Search Configurations -->
<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</property>
<property name="hibernate.search.default.indexBase">${lucene.index.home}</property>
<property name="hibernate.search.default.batch.merge_factor">10</property>
<property name="hibernate.search.default.batch.max_buffered_docs">10</property>
</session-factory>
</hibernate-configuration>
As we see in the Search Configurations section, there is a variable ${lucene.index.home} that should be replaced by other projects on different OS platform,
so the question, does maven provide a way to filter a dependency jar file and filter the content? any plugins ? war:war , unzip ? dependencies ? I couldn't figure a fast way to do that. it looks to me , no matter what plugin would be adopted, the plugin needs to do 4 things basically.
1 unpack the jar in
process-resources phase.
2 substitute the ${var} with
value defined in profile.
3 pack it again back into a jar.
4 need to copy it back from the
packing/unpacking workspace back to
the maven process path ??
did anyone run into this similar requirement before.
thanks
I would assume that those values are meant to be set at runtime, likely as VM arguments. It doesn't make sense to provide a jar file that has to be modified to be able to be used.
If you really really REALLY have to do filtering at build time for configuration purposes, those configuration files should be filtered, NOT your dependencies. Then, you should either bundle said file into multiple artifacts (assuming of course you are targeting multiple environments), or be provided outside the built artifact as an externalized resource.

Categories

Resources