Jersey 2 + Spring 4 without web.xml/applicationContext.xml - spring

I have 2 Maven projects:
project1 that contains my entities/services/DAOs, with a Java Spring (4.2.4) config (no XML)
project2 that is my RESTful API using Jersey (2.22.1), that contains the resources (controllers), Java configuration (extending ResourceConfig, no web.xml)
I would like to inject my services in my Jersey resources, here is what I did:
I added the Maven dependency to project1 in project2
I added the dependency jersey-spring3 to project2
I didn't change anything in my Jersey config class.
I run jetty (Maven plugin, no public static void main or any class to run the server).
First attempt: applicationContext.xml was required.
Second attempt: after adding an empty applicationContext.xml (to project2), my services are not found.
Third attempt: after adding <context:component-scan base-package="com.xxx.project1"/> to applicationContext.xml (package where my project1 Java Spring config lives), services are injected.
My question is how to get rid of the applicationContext.xml file?
I would like my Spring config from project1 to be detected automatically...
Some code:
The Spring configuration
#Configuration
#ComponentScan("com.xxx.project1.service")
public class SpringConfig {
}
The Jersey configuration
#ApplicationPath("/v1")
public class Application extends ResourceConfig {
public Application() {
packages("com.xxx.project2.filter",
"com.xxx.project2.resource");
register(RolesAllowedDynamicFeature.class);
}
}

Use a Spring WebApplicationInitializer to bootstrap the Spring components. You can see an example in this post.

Related

Junit tests start failing after adding #ComponentScan in the Spring boot application class file

I am using Spring boot version 2.3.3.Release
Junit platform launcher 1.5.2
If I add #ComponentScan under #SpringBootApplication, the tests fail with the error :-
Caused by org.springframework.dao.InvalidDataAccessApiUsageException at RepositoryConfigurationExtensionSupport.java:367
My tests have the config like :-
#ExtendsWith(SpringExtension.class)
#WebMvcTest(value = Abcd.class)
And i use :-
#MockBean for service layer and
#Autowired for MockMvc bean
EDIT 1:
I added the ComponentScan to include an external library. The jar is in a lib folder and i use gradle compile filetree to include it.

Spring boot 2.1 while loading external properties, application properties in classpath are ignored

I am having an application.properties in my main/resources folder. Now certain values I want to over-ride at runtime using an external properties file.
To do this I have added the following code:
class ServletInitializer extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
application=application.properties('spring.config.location:/external/properties/,${catalina.base}/../config/config.properties')
application.build()
application.sources(Application)
}
}
With this change its reading the external file but completely ignoring the application.properties file (i.e any value present in application.properties only is not retained).
Spring boot version : 2.1.0.RELEASE
Groovy version : 2.5.3
JDK : 1.8
Note : This same code was working for spring boot version 1.5.10.RELEASE

Error configuring mongo and neo4j with spring boot 1.5.7

I am using MongoDb and Neo4j in my Spring Boot Application. I have recently updated by Spring Boot Gradle Plugin from 1.2.6 to 1.5.7.
I am having two config files one for mongo and other for neo4j. After updating the version of spring boot I found that #EnableMongoRepositories and #EnableNeo4jRepositories are showing following errors in their respective config files:
No constructor with 1 argument defined in class
'org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean'
No constructor with 1 argument defined in class
'org.springframework.data.neo4j.repository.support.GraphRepositoryFactoryBean'
I am having following annotations in neo4j config:
#Configuration
#EnableNeo4jRepositories("<packagename>.neo4j.repository")
#EnableTransactionManagement
public class DatabaseConfigurationNeo4j
extends Neo4jConfiguration { ... }
and following annotations in mongo config:
#Configuration #Profile("!" + Constants.SPRING_PROFILE_CLOUD)
#EnableMongoRepositories("<packagename>.repository")
#Import(value = MongoAutoConfiguration.class)
#EnableMongoAuditing(auditorAwareRef = "springSecurityAuditorAware")
public class DatabaseConfiguration extends AbstractMongoConfiguration
{ ... }
If I remove these #EnableRepositories line from the files, these errors are removed but when I run it, the repositories bean are not creating. I think these lines are necessary but dont know how to remove this error.
Thank you.
updating spring-neo4j to 4.2.8 (not 5.0.0 as neo4j 5.0.0 will work with spring-boot 2.0 M and above) and spring-mongo to 1.10.6 solved the configuration issues.

Spring-boot #ImportResource ignored in Weblogic

I'm facing a strange issue while deploying a spring-boot legacy-war in weblogic-12c. The same war/application works in both mvn spring-boot:run (embedded-tomcat) / Standalone tomcat war deployment.
The #ImportResource configured in the main application is not getting loaded which is causing few bean-injection inconsistencies. Is there any known issue to be worked-around for deploying in weblogic12c?
Note: I've already tried below:
1. extends SpringBootServletInitializer implements WebApplicationInitializer
2. Separate inner-config class
#Configuration #ImportResource({
"classpath*:**/**my-applicationContext.xml"}) #ComponentScan(basePackages =
{"com.myapp"
})
SLF4J exclude in weblogic.xml
<wls:prefer-application-packages>
<wls:package-name>org.slf4j</wls:package-name>
<wls:package-name>com.google.common.*</wls:package-name>
</wls:prefer-application-packages>
The issue was with the Antlr pattern used for loading xml-appcontexts. Strangely the same expression worked in embedded tomcat / standalone tomcat.
The fix was to expand few of the *
#Configuration
#ImportResource({
"classpath*:mypackage/**my-applicationContext.xml"})
#ComponentScan(basePackages = {"com.myapp" })
Debug info:
AbstractBeanDefinitionReader (Line 216) - spring-beans-4.2.4

NoSuchBeanDefinitionException for autowired bean when java bean configuration location is changed from src/main/java to src/test/java

I have an existing spring boot application and I am adding junit tests to it. The project has Configuration.java in src/main/java folder containing the bean configuration.
In my junit class when I refer to configuration file present in location src/main using
#SpringApplicationConfiguration(classes = Configuration.class)
then the Autowired bean intializes properly. However when I copy the same configuration file in src/test/java and refer it using Configuration
#SpringApplicationConfiguration(classes = TestConfig.class)
then the autowire bean fails to initilize giving exception NoSuchBeanDefinitionException.
Its the same configuration java file. The bean I am autowiring is present in src/main/java. Does the location of bean configuration java files affects the bean initialization ? If Yes, how to resolve it ?
Note: I am copying the same config java file to src/test/java as I need some different property while testing.
I was able to solve it be using basepackage attribute of #ComponentScan as shown below(in the config file of src/test/java have put the package name belonging to package in src/main/java).
#Configuration
#ComponentScan(excludeFilters = #ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = CommandLineRunner.class),basePackages = "com.mypackage")
#EnableAutoConfiguration(exclude={MetricRepositoryAutoConfiguration.class})
public class TestConfiguration {

Resources