java.lang.ClassNotFoundException When Filepath is Correct - java

I'm working with Spring right now and have been trying to resolve a number of Failed to load ApplicationContext errors. This latest one is because it can't find a specific class when creating a bean structured like the following:
<bean id="problematicBean"
class="com.domain.application.search.problematicBean">
<property name="missingClass">
<bean class="com.domain.application.search.MissingClass">
</bean>
</property>
</bean>
I've triple checked and the filepath used in the bean class is correct. Other beans with identical paths (at least to the point of applicaiton) work perfectly. The file exists exactly where it should be for this classpath to work. The only difference is that this is a groovy file. Does anyone know why my application can't find the class?

Related

MyBatis MapperScannerConfigurer to scan base package from classpath

I have configured MapperScannerConfigurer in spring application context as follow:
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<property name="basePackage" value="path.to.package.from.classpath.dao" />
</bean>
Here basePackage is in a jar file that is set in classpath.
One of the mapper from the package is autowired in the one of the services I am using. Looks like MyBatis is failing here to scan package from classpath as I am getting error stating no such Bean found:
No qualifying bean of type 'path.to.package.from.classpath.dao.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate.
I have searched online and found nothing which has similar scenario.
I am going to answer here my own question as it may help someone facing the similar issue.
The problem was in the way I created jar in the first place. iBatis might have a different way of scanning the packages as it was working and I thought the same could work with MyBatis. But MyBatis may have a different mechanism for scanning packages. It appears it is looking for directory entries in the jar and if it does not find it, it will throw this exception. So make sure you choose the option Add directory entries while creating the jar as shown below:

gettign proxy of a bean in different project inside java class

I am new to the world of spring so i may ask a silly question but please let me the solution of my below problem please .
My problem is that I have two projects independent project nae is project A and project B ,now in project A i have the below xml configuration of bean
<bean id="abcService" class="com.jmx.JMXServiceImpl" autowire="no">
<constructor-arg index="0">
<ref bean="jobDetailsDomainHome" />
</constructor-arg>
</bean>
now in project A this bean get initilazied easily now i need this same bean initialized in project B also , so i have added project A in project B classpath also now please advise inside java class named rty of Project Bhow can i call this same bean abcService
The bean abcService depends on bean jobDetailsDomainHome. So there's no way to use abcService without the other bean.
You can split the configuration in various xml files. So define the abcService and the needed beans in one xml file, which is imported by the configurations of project A and project B.
<import resource="classpath*:service-context.xml" />
The import of xml files can use the classpath like shown above. But you can use locations in the file system too.
It's not important which bean is defined in which file as long as every needed bean is defined.

Class autodetection in persistence.xml from src/test

I have a persistence.xml at src/main/resources/META-INF/ and another at src/test/... which differ only in the value of the database:
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/lanchecker-prod" />
and:
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/lanchecker-test" />
However I'm also using class autodetection which works fine for the main resource but fails when I use the test resource.
Is there any way to get this to work or am I obliged to use explicit class naming?
EDIT:
The linked suggestion doesn't actually work it it requires the project is built and tested as a .jar. In my case I'm attempting to do the testing under Eclipse, which can resolve the src/test/resources/META-INF/ location.
I tried adding:
<jar-file>${PROJECT_LOC}/src/main/java/biz/ianw/lanchecker/</jar-file>
but that resulted in:
Exception in thread "main" java.lang.ExceptionInInitializerError
...
Caused by: javax.persistence.PersistenceException: Unable to build entity manager factory
...
Caused by: java.lang.IllegalArgumentException: Unable to visit JAR file:${PROJECT_LOC}/src/main/java/biz/ianw/lanchecker/. Cause: Illegal character in opaque part at index 6: file:${PROJECT_LOC}/src/main/java/biz/ianw/lanchecker/
And then:
<jar-file>C:\Users\Ian\git\LANchecker\src\main\java\biz\ianw\lanchecker\</jar-file>
which passed entity manager factory startup, but still failed later with:
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: RouteCabinFares is not mapped [select rcf from RouteCabinFares rcf]
(RouteCabinFares is one of the classes I'm attempting to autolocate).
Maybe this technique can only be used to autolocate in jar files?
As far as I can tell what I'm trying to do here is not possible.
The best work around I've come up with is to remove the properties you wish to vary from the persistence.xml, in this case:
<property name="hibernate.connection.url" value="jdbc:mysql://localhost/lanchecker-test" />
and specify them at runtime using code similar to:
Properties properties = new Properties();
properties.setProperty("hibernate.connection.url", "jdbc:mysql://localhost/lanchecker-test");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("lanchecker", properties);
The actual property values can then be injected at runtime via Spring. This involves separating standard properties into persistence.xml and varying properties into spring.xml and is, to my mind, extremely hokey.
I get the impression that Spring Boot might offer a simpler solution but at the expense at another framework so, for the moment, the above is what I'm going for.

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.

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

Categories

Resources