am trying to implement an OSGI service which can serve me as ConfigurationFactory the service implementation has just two properties as shown below.
#Property(value="this is service variable property value")
static final String MY_SERVICE_VARIABLE = "service.variable";
#Property(description="Label for this MyService")
private static final String MY_SERVICE_LABEL = "service.label";
and am retrieving this service configuration data from an OSGI servlet where i am trying to call this service by below code which compiles fine and retrieves data randomly from the multiple service configuration.
#Reference
MyService myservice;
But when i wanted to get each configuration data by using the service.label and am calling the service by using below code snippet in my OSGI servlet, while compiling am facing the below Error.
#Reference("(service.label=TESTCALL)")
MyService myservice;
cannot find symbol [ERROR] symbol: method value() [ERROR] location:
#interface org.apache.felix.scr.annotations.Reference.
use #Reference(target = "(service.label=TESTCALL)") for AEM 6.x versions, it should compile. I have uploaded the sample POC that i have used earlier at my gourivar github and the same POC example you can find at my aemvardhan.wordpress.com
Your service is most likely missing the Service Factory annotation. Something like:
#Service
#Component(
metatype=true,label="my service",
description="sample my service implementation",
configurationFactory=true)
public class MyServiceImpl implements MyService {
}
Note the configurationFactory=true attribute. This enables the service to have multiple configurations.
Related
I have created by own library(com.custom.mylib) which returns a string like below.
#Component
public class MyLibrary{
#Value("${str.message}")
private String message; //This val should come from app which is going to use this lib
public String readMessage() {
return message;
}
I have create a project which is going to use above library. I have included the lib as pom dependency .But when I try to call library method from my app. I get the error below.
How to resolve it?
#Autowired
private MyLibrary myLibrary;
Consider defining a bean of type 'com.custom.mylog.MyLibrary' in your
configuration.
I also have below in application.properties file so that library can pick the value up
str.message=Hello world
I got the solution it seems.I need to create META-INF file and do org.springframework.boot.autoconfigure.EnableAutoConfiguration=<fully_qualified_name_of_configuration_file>
as given here
Spring Boot: autowire beans from library project
As it has to be used as a external library, you can instantiate it throught a #Configuration file:
#Configuration
public class AppConfiguration {
#Bean
public MyLibrary createMyLibraryInstance() {
return new MyLibrary();
}
}
The rule I used is the follow (this is not an universal rule):
In your domain classes (Controller, Service) : use #Autowired in your constructor. It is the recommanded way to inject your dependencies.
You want to use external classes : implements a Java Configuration with #Configuration annotation, to instanciate your external classes as beans.
You want to create custom utilities classes : decorate it with #Component.
When you have more than on implementation, use #Qualifier and define your beans in a #Configuration class.
I want to test the repository layer and I'm using spring webflux. My test class is as follows
#RunWith(SpringRunner.class)
#DataJpaTest
public class DataTester {
#Autowired
private MyRepository repository;
#Test
.....
}
Even though this would work in spring-mvc when using spring-weblux I get the following error.
Failed to load ApplicationContext
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.context.ApplicationContextException: Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
How to resolve this? If I am to start the whole application context with #SpringBootApplication it works. Any other options without using that?
The reason for this was that in the application.properties the application type was set as reactive.
spring.main.web-application-type=reactive
This tries to auto configure a web server in this case a reactive web server. As #DataJpaTest does not provide a bean for that, this fails. This can be fixed in either two ways.
One is by Adding an application.properties file in the resources directory of the test package and setting the value as,sprig.main-web-application-type=none solves this issue.
Or we can simple pass a property value to the annotation as follows. #DataJpaTest(properties = "spring.main.web-application-type=none")
If you are using Spring Boot 2+ then only #DataJpaTest is enough on test class.
So your test class should be
#DataJpaTest
public class DataTester {
#Autowired
private MyRepository repository;
#Test
.....
}
I am trying to develop a library in Spring that need property from my application. My application get properties from a Spring Cloud Config Server.
So in my library, I have #Configuration annoted class.
#Configuration
public class ServiceConfiguration {
#Value("${idwebservice.uri}")
private String identificationWsUri;
}
I would like this property to be injected through my app after fetching the config server. My application is a Spring Boot application so I didn't implement the fetch of the config server myself.
I am sure that this property is well injected in my app but if I run the app, I've got this error:
Could not resolve placeholder 'idwebservice.uri' in value "${idwebservice.uri}"
I don't know if it is possible so I chose a different approach. My String is now an autowired String
#Autowired
private String identificationWsUri;
and in my app
#SpringBootApplication
public class Application {
#Value("${idwebservice.uri"})
String identificationWsUri;
#Bean
public String identificationWsUri() {
return identificationWsUri;
}
}
I prefered to answer to my question because I saw a similar question and maybe it will help some people struggling with the same problem.
I noticed multiple ways in which developers create an Apache Felix Service. Each of the attached snippets seem to work. Would need some help to understand, which syntax is best for which scenario
Sample 1: Service created without interface
Declaration of Service
D
#Component
#Service(ServiceViaClass.class)
public class ServiceViaClass{
}
Using service via #Reference annotation
private ServiceViaClass serviceViaClass;
Sample 2:Service implementing interface. No value attribute for #Service annotation
- Declaration of Service
#Component
#Service
public class ServiceViaInterfaceImpl implements ServiceViaInterface{
}
Using service via #Reference annotation
private ServiceViaInterface serviceViaInterface;
Sample 3: Service implementing interface with value attribute for #Service annotation
- Declaration of Service
#Component
#Service(ServiceViaInterface.class)
public class ServiceViaInterfaceImpl implements ServiceViaInterface{
}
Using service via #Reference annotation
private ServiceViaInterface serviceViaInterface;
A component implements an interface and publishes itself as a service under that interface, so that clients can find the component using only the interface.
Sample 1 — publishing a service using the concrete type of the component — is nearly always useless. The service can only be found using the concrete type, and if clients can see the concrete type then why not just instantiate it directly rather than get an instance from the service registry??
Sample 2 — publishing a service by implementing an interface and then just adding the #Service annotation — is what you should usually do. When you use #Service and the component directly implements an interface, the build tooling infers that your component wants to be published as a service under that interface.
Sample 3 has the exact same effect at runtime as sample 2, it's just a bit more explicit in the code. Some people prefer this because it's explicit, others (including me) dislike it because it is redundant.
I'm not able to run a simple test in spring boot 1.4. I followed the tutorial from the official site testing-the-spring-mvc-slice but I didn't get it to work.
every time i get the following error:
java.lang.IllegalStateException: Unable to find a #SpringBootConfiguration, you need to use #ContextConfiguration or #SpringBootTest(classes=...) with your test
any ideas, hints?
Thanks in advance
Edit:
this is the controller
#Controller
public class UserManagementController {
#GetMapping(value = "/gs/users/getUsers")
public #ResponseBody String getAllUsers() {
return "test";
}
}
this is the test
#RunWith(SpringRunner.class)
#WebMvcTest(UserManagementController.class)
public class UserManagementControllerTest {
#Autowired
private MockMvc mvc;
#Test
public void showUserView() throws Exception {
this.mvc.perform(get("/gs/users/getUsers"))
.andExpect(status().isOk())
.andDo(print());
}
}
From my point of view it's exactly the same like this post from the site.
the #WebMvcTest will do:
Auto-configure Spring MVC, Jackson, Gson, Message converters etc.
Load relevant components (#Controller, #RestController, #JsonComponent etc)
Configure MockMVC
now why i need to configure a "super" class
The search algorithm works up from the package that contains the test
until it finds a #SpringBootApplication or #SpringBootConfiguration
annotated class. As long as you’ve structure your code in a sensible
way your main configuration is usually found.
So you have annotated your test with #*Test. It run, checked for configuration in subclasses, haven't found any, thrown an exception.
You have to have a config in a package or subpackage of test class or directly pass config class to #ContextConfiguration or #SpringBootTest or have class annotated with #SpringBootApplication.
According to #SpringBootApplication. I have tested controller in way you have mentioned with #WebMvcTest: it works if application has class annotated as #SpringBootApplication and fails with exception you've mentioned if not. There is remark it the article you mentioned:
In this example, we’ve omitted classes which means that the test will
first attempt to load #Configuration from any inner-classes, and if
that fails, it will search for your primary #SpringBootApplication
class.
Github discussion about the same point.
Spring Boot Documentation