How to get the app version into a bean? - java

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/

Related

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 cumulate property placeholder?

We have a classic Maven, Spring (3.1.1) application where we created an applicationContext.xml.
In this file, we have declared a property placeholder with an external file and a file in classpath.
Here an example found in another question here :
<context:property-placeholder location="file:${ADMIN_HOME}/db.properties,classpath:configuration.properties"
ignore-unresolvable="false" ignore-resource-not-found="false" />
It is working.
But now, we have a specific config file for JUnit tests.
In this config file, we have imported the first one and added a property placeholder for tests with a classic declaration.
<import resource="applicationContext.xml" />
<context:property-placeholder location="classpath:configuration-test.properties"
ignore-unresolvable="false" ignore-resource-not-found="false" />
We have injected a value from configuration-test.properties in JUnit test.
#Value("${junit.user.login}")
private String login;
But when we launch the JUnit, an error is raised.
The key "junit.user.login" is not resolved.
We don't know why.
Any idea ?
Thank you.
Is your junit launching the correct spring context?
You added the correct xml paths to the test case like so?
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/my-test-context.xml" })
public class TestCase{}

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.

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

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.

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