Can't Import properties after integrating spring-batch-admin into existed spring boot - spring

I have worked on a project using spring-batch and spring-boot.
I followed the exact rules how to integrate it by:
1. removing all #EnableBatchProcessing
2. adding ServletConfiguration and WebappConfiguration (and also import them using
#Import({ ServletConfiguration.class, WebappConfiguration.class })
add props:
batch-mysql.properties
business-schema-mysql
and modified application.properties with:
server.servletPath=/*
spring.freemarker.checkTemplateLocation=false
ENVIRONMENT=mysql
Now here is the side effect. My app is using an applicationContext .xml in addition to it's java config.
that applicationContext has some place holders:
<context:property-placeholder
location="file:///etc/location/services/myapp.properties"/>
<bean name="configuration" class="com.mycompany.commons.configuration.factory.BeanAwareConfigurationFactory">
<property name="serviceId" value="${serviceId}"/>
...
</bean>
As soon as I integrated spring-batch-admin I got this error:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'serviceId' in string value "${serviceId}"
at
...
I tried #PropertySource to import it, but it didn't work:
#PropertySource("file:///etc/location/services/myapp.properties")
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
System.out.printf("Started processor service app");
}
As soon as I removed spring-batch-admin from my spring-boot project I manage to attach those props.
Any idea how to overcome this?

You can override spring-batch-admindefault context loading configuration. In src/main/resources/META-INF/spring/batch/override/manager/ you can place env-context.xml file with configuration of resources which need to be loaded.
Here is spring batch admin one which can be used as starting point so you can do something like:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Use this to set additional properties on beans at run time -->
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/org/springframework/batch/admin/bootstrap/batch.properties</value>
<value>classpath:batch-default.properties</value>
<value>classpath:batch-${ENVIRONMENT:hsql}.properties</value>
<!-- this line you can add-->
<value>file:///etc/location/services/myapp.properties</value>
</list>
</property>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="order" value="1" />
</bean>
</beans>

I forked the github project and I added the fix to prevent the placeholder error. You can get the new code here: https://github.com/vesperaba/spring-batch-admin-spring-boot.
The issue was SpringBatch has it's own PropertyPlaceholder and you have to overwrite it but to do that you have to manually import some files in order to avoid the one define it.
Here you can find the new conf: https://github.com/vesperaba/spring-batch-admin-spring-boot/blob/master/src/main/java/de/codecentric/batch/config/MainV2Configuration.java

Related

How to get Spring/Gradle to recognize JdbcTemplate initialization

I have created a small SpringBoot/Gradle REST controller application, but I am
having problems initializing a connection to a Postgresql database. My application
includes the following class that's used to access the database:
#Component
public class UserSettingDAO {
#Autowired
private JdbcTemplate jdbcTemplate;
#Autowired
private DataSource dataSource;
...
}
In the first version of the program, I included the following in /src/main/resources/application.properties:
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://...
spring.datasource.username=...
spring.datasource.password=...
In this version, my REST service successfully connected with the database. However,
for consistency with other projects in my group, I changed the initialization of
dataSource and jdbcTemplate as follows:
First, I removed those property
definitions above from application.properties. Next, I created an XML initialization file resources/resources_deployment1/spring-resource1.xml that included the
following:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:pdao_connection.properties"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
where the referenced file resources/resources_deployment1/pdao_connection.properties
contains the following:
driverClassName=org.postgresql.Driver
db.username=...
db.password=...
url=jdbc:postgresql://...
After this, when I then deployed my application (to Websphere Liberty) and hit my REST
service from Chrome, it failed with the following message:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
It seems like there is a break in the long chain of references so that Spring does
not have access to the required connection parameters. How can I fix this, while
keeping the deployment-specific parameters in a subfolder of resources? In
particular, how do I convince the build and runtime operations to look in the
resources/resources_deployment1 folder?
it required artifacts spring-jdbc and postgresql java connector inside your web.xml or pom.xml
I stumbled upon an answer: Adding the following piece of code in the same package where I'm using jdbcTemplate
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
#Configuration
#ImportResource("classpath:spring-resource1.xml")
public class DAOConfiguration {
}
appears to let Spring know that it should look in the referenced XML file that is, in fact, under the project's resources/ directory.

Liferay 7 MessageListener osgi module

When I worked with the liferay 6.1 I created an application that received the message.
Java class:
public class MailMessageBus implements MessageListener
\src\main\webapp\WEB-INF\src\META-INF\messaging-spring.xml file:
<beans
default-destroy-method="destroy"
default-init-method="afterPropertiesSet"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="messagingConfigurator" class="com.liferay.portal.kernel.messaging.config.PluginMessagingConfigurator">
<property name="messageListeners">
<map key-type="java.lang.String" value-type="java.util.List">
<entry key="mail-send-message">
<list value-type="com.liferay.portal.kernel.messaging.MessageListener">
<ref bean="messageListener.mail_listener" />
</list>
</entry>
</map>
</property>
<property name="destinations">
<list>
<ref bean="destination.mail"/>
</list>
</property>
</bean>
<!-- Destination class -->
<bean id="destination.mail" class="com.liferay.portal.kernel.messaging.ParallelDestination">
<property name="name" value="mail-send-message" />
</bean>
<!-- Listener class -->
<bean id="messageListener.mail_listener" class="customportlet.AMessageBusListener.MailMessageBus" />
and \src\main\webapp\WEB-INF\web.xml file:
...
<context-param>
<param-name>portalContextConfigLocation</param-name>
<param-value>/WEB-INF/src/META-INF/messaging-spring.xml</param-value>
</context-param>
...
How can I do the same for liferay 7 in the OSGi module? OSGi module does not have a web.xml file.
Thank you very much!
I'm trying to accomplish the same thing, and based on what I've found so far the web.xml is unnecessary. OSGi modules seem to detect spring configuration automatically, according to this resource:
https://docs.spring.io/spring-osgi/docs/current/reference/html/app-deploy.html
Just put your configuration in xml files located at META-INF/spring

Could not resolve placeholder in string value with external files

I have configured a config.xml file choosing the appropriate properties file depending on environment. I am running this as a Spring Boot app with Apache Camel.
The config looks like this.
<bean id="properties"
class="org.apache.camel.component.properties.PropertiesComponent">
<property name="locations" ref="locations" />
</bean>
<bean id="bridgePropertyPlaceholder"
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="locations" ref="locations" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<beans profile="dev">
<util:list id="locations">
<value>classpath:config/users.properties</value>
<value>classpath:config/application.properties</value>
<value>classpath:config/application-dev.properties</value>
</util:list>
<beans profile="test">
<util:list id="locations">
<value>file:${project.dir}/config/users.properties</value>
<value>file:${project.dir}/config/application.properties</value>
</util:list>
When using the test profile I want to use the external files defined in the config (because I dont want to commit username/password to repo). That seems to work okay.
However, my users.properties file contains:
username=myusername
password=mypassword
and my application.properties contains:
loginParameters=username=${username}&password=${password}
when running java -jar myjar.jar --spring.profiles.active=test I encounter:
java.lang.IllegalArgumentException: Could not resolve placeholder 'username' in string value '${username}&password=${password}'
It clearly loads the properties files because it states:
Loading properties file from URL: file:...../users.properties
Loading properties file from URL: file:...../application.properties
Bridging Camel and Spring property placeholder configurer with id: bridgePropertyPlaceholder
...
And then the exception occurs. How can I resolve the issue where the application.properties file doesn't recognize my properties defined in users.properties? Everything works okay when running the dev-profile.
in your application properties, use $simple{username} and $simple{password} to tell Camel to put the values there.

Spring Entitymanager java.lang.ClassFormatError

This is 2nd day that I am struggling with this issue.
We have a web app that uses spring and hibernate. Now We are trying to expose some functionality to command line. So We should be able to run a class from command line(without web app) so it should have access to spring and hibernate.
<context:annotation-config/>
<context:component-scan base-package="com.x.y.z"/>
<jpa:repositories base-package="com.x.y" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="jpaProperties">
<props>
<prop key="javax.persistence.validation.mode">NONE</prop>
<prop key="hibernate.validator.apply_to_ddl">false</prop>
<prop key="hibernate.validator.autoregister_listeners">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="SQL_SERVER" />
<property name="showSql" value="false" />
</bean>
</property>
</bean>
Here I am using LocalEntityManagerFactoryBean but it seems there is no way to set datasource to it. It gets from persistance.xml
If I use LocalContainerEntityManagerFactoryBean I can set datasource(it has datasource property)
persistance.xml:
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="oms-jpa">
<class>com.domain.User</class>
</persistence-unit>
In persistance.xml I am not defining any database informaton for datasource, so LocalEntityManagerFactoryBean does not work for this.
what I am getting:
Error creating bean with name 'entityManagerFactory' defined in file [C:\Users\ekamoliddinov\IdeaProjects\web\conf\spring-config.xml]: Invocation of init method failed; nested exception is java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/validation/Validation
googled then some says:
Jar versions messed up
jee-api.jar is missing
But I have jee-api.jar.
My questions:
What is causing the above exception?
Can I use LocalContainerEntityManagerFactoryBean without having web
container? Spec does not say anything about this.
If i can not use LocalContainerEntityManagerFactoryBean then how can I set up
LocalEntityManagerFactoryBean so it should use my datasource not from
persistance.xml(I can not define anything in persistance.xml).
I am using spring version 3.1.3 with hibernate 4.1.8.Final and i am in command line(I do no have jee environment)
Sorry about more than one questions, but all questions are arising from the same root I think.
Any help will be appreciated.
Thanks.
After trying 3 days I fixed the issue. The jars where messed up :)
It was because I have jee-api.jar in my classpath and I was running the code from command line, I had not web env. So there was not any impl of jee-api.jar in my env. That is why it was giving the above exception. I removed jee-api.jar and disappeared.
Another thing That I can tell: we can use LocalContainerEntityManagerFactoryBean from command line. I am using it.
The last thing: We can not set datasource to LocalEntityManagerFactoryBean
This my help to someone.

Inject JAXBContext into spring

I am trying to inject a JAXBContext into spring application context, by:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.Class" value="com.package.MyClassName"/>
</bean>
It throws an exception:
No matching factory method found: factory method 'newInstance'
And I also try :
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg type="java.lang.String" value="com.package"/>
</bean>
And It throws an an exception:
javax.xml.bind.JAXBException: "com.package" doesnt contain ObjectFactory.class or jaxb.index
I did put a jaxb.index file inside the package "com.package" and has a single line "MyClassName" in the file.
#Tomasz's answer is the solution I'd recommend, but if you want to stick with JAXBContext, then the reason your first example failed is that the static getInstance() method on JAXBContext doesn't take a single Class argument, it takes a vararg list of them. So you need to inject a list, not a single class:
<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg value-type="java.lang.Class">
<list>
<value>com.package.MyClassName</value>
</list>
</constructor-arg>
</bean>
Have you tried Spring OXM? The last line is important, namespaces are for reference only:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.package"/>
</beans>
See 8.4. XML Schema-based Configuration. Yu'll also need spring-oxm on your classpath.
this will resolve the problem for jaxb.index file or ObjectFactory problem in spring env. provide the value of the package where the classes are their which generate the xml
enter code here`
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan" >
<value>com.adarsh.spring.integration.entities</value>
</property>
</bean>`

Resources