Issue with a clientApplicationContext xml file - java

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

Related

java.lang.ClassNotFoundException When Filepath is Correct

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?

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/

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.

How to use JNDI DataSource provided by WebLogic 12.2.1 in Spring?

I created a JNDI connection with the following Values:
i selected Generic Data Source option
Name: jdbc/sampleDataSource
JNDI Name: jdbc/sampleDataSource
Spring Config File:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/sampleDataSource" />
I'm getting below error.
Error An error occurred during activation of changes, please see the log for details.
Error javax.naming.NameNotFoundException: While trying to lookup 'jdbc.sampleDataSource' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/sampleDataSource'
Error While trying to lookup 'jdbc.sampleDataSource' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/sampleDataSource'
I was unable to resolve it.
How do i configure in Spring 4.
Any addition jar file is required. Please help on this.
Sometimes this happens when you forget to Target your defined datasource to a specific server. You can find it in Weblogic's administration server :
then in the Targets tab:
you should select the target.
If that wasn't the problem, you may try the way of getting your datasource in the applicationContext.xml:
<bean id="dataSource" name="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/mcdsDS"/>
<property name="resourceRef" value="true"/>
</bean>
And use the dataSource reference wherever you need a datasource.
Hope this would be helpful.

Unable to load database driver via Spring, but can manually find it on the classpath

I'm getting a ClassNotFoundException on a database driver class when attempting to create a data source using Spring running under Tomcat. I am, however, able to load the driver class directly from the same data access component (both via Class.forName and getClass().getClassLoader().loadClass()). The driver jar is only installed in one place ($CATALINA_HOME/lib).
The data source definition in the Spring config file:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
The DAO definition in the Spring config file:
<bean id="countryDao" class="com.mycompany.pmo.dao.CountryDao">
<constructor-arg ref="dataSource"/>
</bean>
The DAO itself:
public class CountryDao {
private NamedParameterJdbcTemplate jdbcTemplate;
public CountryDao(DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public List<Country> getCountries() {
//I can manually load the driver class here
String sql = "select * from ref.country";
Map<String, Object> namedParameters = new HashMap<String, Object>();
//next line is line 34, where the stack trace starts
return jdbcTemplate.query(sql, namedParameters,new CountryMapper());
}
}
The stack trace:
Feb 20, 2013 2:18:35 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/PMO] threw exception [Request processing failed; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '"com.microsoft.sqlserver.jdbc.SQLServerDriver"'] with root cause
java.lang.ClassNotFoundException: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:573)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:637)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:662)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:702)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:166)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.query(NamedParameterJdbcTemplate.java:172)
at com.mycompany.pmo.dao.CountryDao.getCountries(CountryDao.java:34)
The jar should not be in $CATALINA_HOME/lib. It should be in WEB-INF/lib of your web app. I believe $CATALINA_HOME/lib will be used to load and run Tomcat, but your app classpath dependencies are loaded from WEB-INF/lib.
If you are developing in an IDE and running your program in it, you might also have to add the jar to your project build path.
As #Sotirios Delimanolis mentioned (+1) try to put the jar under WEB-INF/lib of your application.
The fact that the class is found using your manual code and cannot be found via Spring signals that the problem is in class loader. When you call Class.forName() you are definitely using the system class loader, so the class is loaded from catalina lib directory. I cannot explain why it works when you are calling getClass().getClassLoader().loadClass() from you DAO: it in this case the same application class loader should be used. But probably I do not know deeply enough how does Spring itself play with class loaders.

Categories

Resources