jpa 2.1 spring boot with web sphere 8.5.5.8(full version) jdk 7 - spring-boot

i followed the approach suggested by jeff
https://gist.github.com/jeffsheets/aec3e94870ef903ce7efe33e00563d3c
I was able to overcome the jpa 2.1 java.lang.ClassCastException: com.ibm.websphere.persistence.PersistenceProviderImpl incompatible with javax.persistence.spi.PersistenceProvider.
But i get the following error A servlet named com.x...JerseyConfig can not be dynamically added because a servlet configuration with the same name already exists. i am using WebSphere 8.5.5.8 Full version with JDK 1.7 spring boot 1.4.0.M3.
In my JerseyConfig.java
#Component
#PropertySources(value = {
#PropertySource("classpath:application.properties"),
#PropertySource("classpath:ValidationMessages.properties")})
#DependsOn("hibernatePersistenceProviderResolver")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("com.x.package");
}
}
Thanks for any hints or pointers.

I did the following to make it work (in addition to #DependsOn mentioned previously).
1) load local class loader first and parent last.
2) Add the JVM property at the WebSphere Appserver com.ibm.websphere.jaxrs.server.DisableIBMJAXRSEngine to true in order to use only the JAX RS shipped with the application. (which fixes the servlet name already exists).
3) After deploying successfully when i ran my REST endpoint i got the following error at run time.
UOWManager transaction processing failed; nested exception is com.ibm.wsspi.uow.UOWException: java.lang.VerifyError: com/ibm/websphere/uow/UOWSynchronizationRegistry.registerInterposedSynch ronizat ion(Ljavax/transaction/Synchronization)V
To fix this add spring.jta.enabled=false to use the WebShpere JTA. (Ideally prefer to override web sphere JTA and use spring JTA, need to figure out a way).

Related

instanciating SolrCrudRepository programmatically, without Spring Boot magic

I have a Spring Boot 2.4 project that is using spring-boot-starter-data-solr. Unfortunately, spring-boot-starter-data-solr is not supported anymore (see https://spring.io/projects/spring-data-solr#overview) , so I can't easily upgrade Spring Boot as I usually do for other projects.
However, I thought I would still try to upgrade my project and keep using the latest spring-boot-starter-data-solr I could get : maybe it's not supported anymore, but I am not making an extensive use of it, so maybe I can take advantage of latest Spring Boot features for a some more time, before there's really a breaking change and I really can't use spring-boot-starter-data-solr
So, I upgraded my project to Spring Boot 2.6.4 letting Spring BOM pull all the latest versions of all dependencies managed. Of course, it failed on spring-boot-starter-data-solr, so I had to declare the version explicitly :
implementation 'org.springframework.boot:spring-boot-starter-data-solr:2.4.13'
I fixed few other things, and my project compiles. However, at startup (and in some integration tests), I have a problem :
#Configuration
public class PersistenceConfig {
#Bean
public TicketRepository ticketRepository(SolrTicketEntityRepository solrTicketEntityRepository) {
return new TicketRepositoryImpl(solrTicketEntityRepository);
}
... some other beans..
}
with SolrTicketEntityRepository defined like below :
public interface SolrTicketEntityRepository extends SolrCrudRepository<TicketEntity, String> {
#Query("?0")
Page<TicketEntity> search(String searchText, Pageable pageable);
With Spring Boot 2.6, the SolrTicketEntityRepository doesn't get instanciated anymore, so I have a missing bean at startup. I've tried adding the #EnableSolrRepositories on PersistenceConfig, but it doesn't do anything.
is there a way to mimic Spring Boot magic, and trigger programmatically SolrTicketEntityRepository / SolrCrudRepository instanciation, to be able to start my application ?
or is it way too complicated, and as recommended here , should I implement the stuff myself with the core Solr libraries without Spring Boot's help (which would be the objective at some point anyway) ?

EnableLoadTimeWeaving annotation causes application context to fail to load

I am trying to enable AspectJ load-time weaving (not Spring AOP) in a Spring Boot application. My goal is to weave advice into annotated fields and java.lang.reflect.Field.set(Object, Object) at load-time.
Per the Spring docs, I tried:
#Configuration
#EnableLoadTimeWeaving
public class Config {}
Running the Spring Boot application with this configuration resulted in the application context failing to load with this message:
Caused by: java.lang.IllegalStateException:
ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader]
does NOT provide an 'addTransformer(ClassFileTransformer)' method.
Specify a custom LoadTimeWeaver or start your Java virtual machine
with Spring's agent: -javaagent:spring-instrument-{version}.jar
The latter suggestion in that message is not a good option as I am trying to avoid necessitating launch script modifications. The aspect I need to weave actually resides in a library, so all implementing Spring Boot projects will have to make whatever changes required to get LTW to work.
I also tried this configuration:
#Configuration
#EnableLoadTimeWeaving
public class Config implements LoadTimeWeavingConfigurer {
#Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
}
Running the Spring Boot application with this configuration resulted in the application context failing to load with this message:
Caused by: java.lang.IllegalStateException:
ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader]
does NOT provide an 'addTransformer(ClassFileTransformer)' method.
It seems I need to make the JVM use a class loader that has an addTransformer(ClassFileTransformer) method. I don't know how to do that, particularly for this situation. Any suggestions?
I am not an active Spring user, but I know that Spring supports annotation- or XML-configured agent hot-attachment and has some container-specific classes for that according to its documentation. It does not seem to work reliably in all situations, though, especially when running a Spring Boot application from an IDE or so.
Anyway, the AspectJ weaver 1.8.7 and more recent can be hot-attached. I explained how to do that in a Spring setup here. If you want a simpler solution with less boilerplate but one more dependency to a tiny helper library called byte-buddy-agent, you can use this solution as a shortcut. I have not tried it, but I know the helper library and am using it myself in other contexts when hot-attaching bytecode instrumentation agents, avoiding the fuss to cater to different JVM versions and configuration situations. But in order for that to work on JVM 9+, you might need to manually activate auto-attachment for the JVM, which would be another modification for your start-up script, and you would be back to square 1.

Using both WebApplicationInitializer and web.xml in spring mvc+spring security+spring session redis web application

I'm trying to implement Spring redis session in an existing Spring MVC (ver 5.1.6) application. In web.xml we have ContextLoaderListener, DispatcherServlet and contextConfigLocation are all defined.
After required dependencies are included and suggested code changes are done, i'm getting below error:
Caused by: java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader definitions in your web.xml!"}}*
As part of code changes i'm extending the class "AbstractHttpSessionApplicationInitializer",(from Spring session core library) which internally implements WebApplicationInitializer. Seems like that is trying to create another context and throwing the above error. We cannot avoid extending this class, as this does the job of registering redisHttpSession to context.
Most of the examples available are all with spring boot. So there they wouldn't have faced this issue.
Any solution, other than completely replacing web.xml and use only WebApplicationInitializer?
Just want to provide an update. Instead of extending AbstractHttpSessionApplicationInitializer abtract class, i have taken a different approach by initializing bean RedisHttpSessionConfiguration thru XML bean definition.
This approach worked.
Followed the steps mentioned in the below thread;
How to configure Spring sessions to work with Redis in xml?
Along with that we need to serialize the cookie as well;
#Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("SESSIONID");
serializer.setCookiePath("/");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}

Spring Boot JPA CrudRepository

I'm working with Spring Boot + Spring Data JPA and facing this problem when trying to inject a class that extends CrudRepository:
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'topicRepository': Could not resolve
matching constructor (hint: specify index/type/name arguments for
simple parameters to avoid type ambiguities)
Repository Class:
public interface TopicRepository extends CrudRepository<Topic, Integer> {}
Service Class:
#Service
public class TopicService {
#Autowired
private TopicRepository topicRepository;
}
Any suggestions?
I was having the same issue, and I fixed it by switching Spring Boot versions. Changing the Spring Data JPA versions did nothing (this is where I assumed the bug would be), so I think there is a bug in Spring Boot version 1.5.1. I switched back to version 1.4.3 and the error was gone. I didn't try subsequent/different versions, so you may just have to experiment with your dependencies and their versions.
For the record, you can have your service class annotated with #Repository, it shouldn't make any difference. I've been setting these apps up the same way using the service/dao pattern, and it has never been too picky with the annotations. Hopefully this may help others whose Spring Boot development flow suddenly throws an error!
Which versions of spring-data-commons and spring-data-jpa are you using. I just ran into this using spring-data-commons 1.13.x with spring-data-jpa 1.10.x. Upgrading spring-data-jpa to 1.11.x fixed the issue for me.
I too had the same issue after updating Spring Boot to 1.5.4.
I am also using spring-data-envers, which was at version 1.0.4. Upgrading to 1.4.1 solved the problem.
I hope it helps someone :)
Make sure:
1) TopicRepository is annotated with #Repository.
2) You have the scanning packages configured:
<jpa:repositories base-package="mypkg.repositories"></jpa:repositories>
Had the same issue on 1.5.2. Upgrading to 1.5.5 solved the problem.
You can use Applicationcontext to inject repository to this reference topicRepository..
You just declare applicationcontext in #rest controller class
Same like topicRepository by using annotation. Then you pass this to the service class which should take parms through constructor.
Ex-
public TopicService(Applicationcontext ctx) {this.topicRepository =context.getBean(TopicRepository.class);
}

Using #ConfigurationProperties in Spring Boot Application doesn't work

I am using Spring Boot V 1.4.1 for a new application.
My app requires two JDBC data sources and I was following the example at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources how to set it up.
My Spring beans configuration class is annotated with #EnableConfigurationProperties and my first bean is defined as
#Primary
#Bean
#ConfigurationProperties(prefix = "first.database")
DataSource qivsDB() {
return DataSourceBuilder.create().build();
}
, the second one accordingly. My application.properties file has properties defined like
first.database.url=jdbc:[redacted]
first.database.username=[redacted]
first.database.password=[redacted]
For reasons I not transparent to me during debugging this is failing to initialize: Cannot determine embedded database driver class for database type NONE - debug showed me that the builder does not have any properties set when calling build().
What did I miss here?
Before you do all the debugging part, you should have a look to the auto-configuration report. If you define your own DataSource there's no reason for Spring Boot to start looking at what it can do for your app. So, for some reasons, that definition of yours is not applied in your app and the default in Spring Boot still applies, doesn't find any JDBC url in the default namespace and attempt to start an embedded database. You should see in the auto-config report that the DataSourceAutoConfiguration still matches.
I am not sure the public keyword has anything to do with it, though you won't get custom meta-data for that key since we only scan for public methods.

Resources