Specifying which validation group to use for a bean - java

Specs : hibernate-validator[5.2.4.Final], spring-context[4.2.2.RELEASE]
I am trying to make the solution described here work as below. But there are no constraint violations encountered & things just pass by fine. Why?
I have two beans, one parent , other child. The child definition is as below
package code;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
#Service("SampleBean")
#Validated
public class SampleBean {
#NotNull(message= "value can not be null" , groups = Group1.class)
// #NotNull(message= "value can not be null")
private Integer value;
#NotNull(message= "value1 can not be null" , groups = Group2.class)
// #NotNull(message= "value can not be null" )
private Integer value1;
public Integer getValue() {
return value;
}
public void setValue(#NotNull
Integer value) {
this.value = value;
}
public Integer getValue1() {
return value1;
}
public void setValue1(Integer value1) {
this.value1 = value1;
}
}
The Parent bean definition is as below :
package code;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
#Service("SampleBeanParent")
#Validated
public class SampleBeanParent {
public void acceptChildBean(#NotNull(message = "child cannot be null")
// #Valid
#Validated(Group1.class)
SampleBean childBean) throws NoSuchMethodException, SecurityException{
System.out.println("successfully finished");
}
}
The test class is as
package code;
import java.util.ArrayList;
import java.util.List;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) throws NoSuchMethodException, SecurityException{
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
SampleBean sampleBean = (SampleBean) context.getBean("SampleBean");
try{
SampleBeanParent parent = (SampleBeanParent) context.getBean("SampleBeanParent");
parent.acceptChildBean(sampleBean);
}
catch(ConstraintViolationException e){
System.out.println("there were validation errors");
}
}
}
By The way, i have setup appropriate spring level beans as below & it works fine without groups(i have commented the validation lines in above code without groups for the working case). So this is not the problem :)
package code;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
#Configuration
#ComponentScan(basePackageClasses = {SampleBean.class})
public class SpringConfiguration {
#Bean(name = "validator")
public LocalValidatorFactoryBean initValidatorFactory(){
return new LocalValidatorFactoryBean();
}
#Bean
public MethodValidationPostProcessor initValidationPostProcessor(){
return new MethodValidationPostProcessor();
}
}

I could do that the following way. The changed classes are as follows (I removed everything from the code that seemed to be redundant/unnecessary from the particular problem point of view)
#Service("SampleBeanParent")
#Validated(Group1.class)
public class SampleBeanParent {
public void acceptChildBean(
#Valid SampleBean childBean) throws NoSuchMethodException, SecurityException {
System.out.println("successfully finished");
}
}
#Service("SampleBean")
public class SampleBean {
#NotNull(message = "value can not be null", groups = Group1.class)
private Integer value;
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
Please also refer to this thread: Spring #Validated in service layer, it contains several useful points, among others quoted from one of the answers:
"The #Validated annotation is only used to specify a validation group, it doesn't itself force any validation. You need to use one of the javax.validation annotations, like #Null or #Valid." - indicating that #Validated(Group1.class) SampleBean childBean in your example does not seem to be correct
The last answer is discussing the specific case when there is another annotation on the method parameter besides #Valid like in your case there was also #NotNull(message = "child cannot be null")

Related

How to handle GraphQL query validation error in Spring Boot

I have a simple Spring Boot project using spring-boot-starter-graphql. This project has one controller that accepts one argument.
#Controller
public class HelloNameController {
#QueryMapping
public String hello(#Argument String name) {
return "Hello " + name;
}
}
This argument is required.
Graphql schema
type Query {
hello (name : String!) : String
}
When I call this API in the Postman and do not pass this argument the app returns an error. I want to override the message of this error message, but I can't find a way to do it.
In the official documentation, it says to implement DataFetcherExceptionResolverAdapter and I've implemented it as a bean
#Configuration
public class GraphQLConfig {
#Bean
public DataFetcherExceptionResolver exceptionResolver() {
return DataFetcherExceptionResolverAdapter.from((ex, env) -> {
if (ex instanceof CoercingParseValueException) {
return GraphqlErrorBuilder.newError(env).message("CoercingParseValueException")
.errorType(ErrorType.ExecutionAborted).build();
}
if (ex instanceof CoercingSerializeException) {
return GraphqlErrorBuilder.newError(env).message("CoercingSerializeException")
.errorType(ErrorType.ExecutionAborted).build();
} else {
return null;
}
});
}
}
The problem is that the error never gets to this point. How do I catch this type of error and override the message?
I've asked a similar question on a GitHub. Responses from graphql-java project (#2866) and spring-graphql project (#415) were similar. To summarise at the time of writing it is not possible.
Then I've created a "workaround":
First, create a custom exception class that implements GraphQLError.
import graphql.GraphQLError;
import graphql.language.SourceLocation;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.http.HttpStatus;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
#Getter
#NoArgsConstructor
public class BadRequestException extends RuntimeException implements GraphQLError {
private HttpStatus status = HttpStatus.BAD_REQUEST;
private String message = "Resource not found";
// Below code used for GraphQL only
private List<SourceLocation> locations;
public BadRequestException(String message, List<SourceLocation> locations) {
this.message = message;
this.locations = locations;
}
#Override
public Map<String, Object> getExtensions() {
Map<String, Object> customAttributes = new LinkedHashMap<>();
customAttributes.put("errorCode", this.status.value());
return customAttributes;
}
#Override
public List<SourceLocation> getLocations() {
return locations;
}
#Override
public ErrorType getErrorType() {
return ErrorType.BAD_REQUEST;
}
#Override
public Map<String, Object> toSpecification() {
return GraphQLError.super.toSpecification();
}
}
Second, create an interceptor class that implements WebGraphQlInterceptor and annotate it as #Component, so Spring can create it as a bean. Inside this class implement logic to catch the needed error and convert it to the exception class created before
import graphql.ErrorClassification;
import graphql.ErrorType;
import graphql.GraphQLError;
import graphql.validation.ValidationErrorType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.graphql.ResponseError;
import org.springframework.graphql.server.WebGraphQlInterceptor;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.WebGraphQlResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import java.util.List;
import java.util.stream.Collectors;
#Slf4j
#Component
public class ErrorInterceptor implements WebGraphQlInterceptor {
#Override
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
return chain.next(request)
.map(response -> {
log.info("[ErrorInterceptor] Intercepting response... ");
List<GraphQLError> graphQLErrors = response.getErrors().stream()
.filter(error -> ErrorType.ValidationError.equals(error.getErrorType()))
.map(this::resolveException)
.collect(Collectors.toList());
if (!graphQLErrors.isEmpty()) {
log.info("[ErrorInterceptor] Found invalid syntax error! Overriding the message.");
return response.transform(builder -> builder.errors(graphQLErrors));
}
return response;
});
}
private GraphQLError resolveException(ResponseError responseError) {
ErrorClassification errorType = responseError.getErrorType();
if (ErrorType.ValidationError.equals(errorType)) {
String message = responseError.getMessage();
log.info("[ErrorInterceptor] Returning invalid field error ");
if (ValidationErrorType.NullValueForNonNullArgument.equals(
extractValidationErrorFromErrorMessage(responseError.getMessage()))) {
String errorMessage =
"Field " + StringUtils.substringBetween(message, "argument ", " #") + " cannot be null";
return new BadRequestException(errorMessage, responseError.getLocations());
}
}
log.info("[ErrorInterceptor] Returning unknown query validation error ");
return new BadRequestException("Unknown error", responseError.getLocations());
}
private ValidationErrorType extractValidationErrorFromErrorMessage(String message) {
return ValidationErrorType.valueOf(StringUtils.substringBetween(message, "type ", ":"));
}
}
The only problem with this approach is that all needed information like a type of an error, the field that causes the error, etc. is embedded in the native error message. So to extract the needed parameters we have to parse the string message.

Making attribute in json request object not null

I'm building a Spring application which has #RestController, like:
#RestController
#RequestMapping(value = "/master")
public class MyController {
#Autowired
private MyService service;
#PostMapping("/call")
public ResponseEntity<Boolean> apiCall(#RequestBody MyDTO myDto) { ;
return new ResponseEntity<Boolean>(service.apiCall(myDto), OK);
}
}
And a request object:
public class MyDTO {
#JsonProperty("emp_number")
private long empNumber;
#JsonProperty("office_id")
private long officeId;
// ....constructors, etc.
}
In request json I want officeId to be not null.
So far I've tried marking the officeId field as:
#com.fasterxml.jackson.databind.annotation.JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
#JsonProperty(required = true)
#javax.validation.constraints.NotNull
But in the request json, even if I miss office_id, it is not throwing any error.
What am I missing?
It could be that in your case you are deserializing request, and missing primitive properties which are referenced by constructor are assigned a default value
java defaults
You could try to use corresponding Long wrapper object for MyDTO instead of primitives or maybe deserialization feature FAIL_ON_NULL_FOR_PRIMITIVES
You have to change the type from primitive to wrapper otherwise the default value of 0 will be considered and validation will pass.
Annotate MyDTO with #Valid annotation.
When Spring Boot finds an argument annotated with #Valid, it automatically bootstraps the default JSR 380 implementation — Hibernate Validator — and validates the argument.
from here
Please add following dependency to pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
PS: i have made minor adjustments to response structure to see the error in response
Entire code is as follows:
package com.example.spring.java.springjavasamples;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;
#SpringBootApplication
public class SpringJavaSamplesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringJavaSamplesApplication.class, args);
}
}
#RestController
#RequestMapping(value = "/master")
class MyController {
private final MyService service;
public MyController(MyService service) {
this.service = service;
}
#PostMapping("/call")
public ResponseEntity<Map<String, Object>> apiCall(#Valid #RequestBody MyDTO myDto, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
Map<String, String> errors = new HashMap<>();
bindingResult.getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return prepareResponse("error", errors, HttpStatus.BAD_REQUEST);
} else {
return prepareResponse("data", service.apiCall(myDto), HttpStatus.OK);
}
}
private ResponseEntity<Map<String, Object>> prepareResponse(String key, Object data, HttpStatus status) {
Map<String, Object> map = new HashMap<>();
map.put(key, data);
return new ResponseEntity<>(map, status);
}
}
class MyDTO {
#NotNull(message = "Employee number cannot be null")
#JsonProperty("emp_number")
private Long empNumber;
#NotNull(message = "Office Id cannot be null")
#JsonProperty("office_id")
private Long officeId;
#Override
public String toString() {
return "MyDTO{" +
"empNumber=" + empNumber +
", officeId=" + officeId +
'}';
}
}
#Service
class MyService {
public String apiCall(MyDTO myDto) {
System.out.println("all valid: " + myDto);
return myDto.toString();
}
}

How Spring's Cacheable Annotation can work for class initailized through new Keyword. (In a Class Constructor, initialized through Bean)

In our service, we are initializing a bean (say "A") and that internally constructing a CacheableService Object by using - new CacheableService(). And as I know spring's #Cacheable annotations won't work on class method if the class is initialized using "new" Keyword.
Then what is an alternative or a way to cache method response?
Scenario :
<bean class="com.package.src.A"/>
public class A {
Map<String, CacheableService> map;
public CacheableService2() {
map = new HashedMap();
map.put("a", new CacheableService());
}
}
import org.springframework.cache.annotation.Cacheable;
public class CacheableService {
#Cacheable(value = "entityCount", key = "#criteria.toString()")
public int someEntityCount(final String criteria) {
System.out.println("Inside function : " + criteria);
return 5;
}
}
Here is a minimum example which demonstrates caching using Spring Boot. The code for the examples below can be found here.
Go to https://start.spring.io/ and create a new Spring Boot project. Make sure to include "Spring cache abstraction" which results in this entry being added to your pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
Add the #EnableCaching annotation to your application:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
#EnableCaching
#SpringBootApplication
public class CacheableApplication {
public static void main(String[] args) {
SpringApplication.run(CacheableApplication.class, args);
}
}
Your service:
package com.example;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
#Service
public class CacheableService {
#Cacheable(value = "entityCount")
public int someEntityCount(final String criteria) {
System.out.print(String.format("Inside function: %s", criteria));
return 5;
}
}
Class A:
package com.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
#Component
public class A {
private CacheableService cacheableService;
public A(#Autowired CacheableService cacheableService) {
this.cacheableService = cacheableService;
}
public int getEntityCount(String criteria) {
return cacheableService.someEntityCount(criteria);
}
}
And then here is a test that demonstrates that the caching is working. As you can see in the test a.getEntityCount("foo") is being called twice, but in standard out we only see "Inside function: foo" being printed once. Therefore we have verified that the second call resulted in the cache being used to produce the result.
package com.example;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
#SpringBootTest
class CacheableTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
#Autowired
private A a;
#BeforeEach
public void init() {
System.setOut(new PrintStream(outContent));
}
#Test
public void testCaching() {
a.getEntityCount("foo");
a.getEntityCount("foo");
assertEquals("Inside function: foo", outContent.toString());
}
}
EDIT:
If you want to move the cache outside of the Spring lifecycle and manually manage it then I would recommend using Caffeine. Here is the same example but now without any Spring involved.
Your service:
package com.example.withoutspring;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import java.util.concurrent.TimeUnit;
public class CaffeineCachingService {
private LoadingCache<String, Integer> entityCountCache = Caffeine.newBuilder()
.expireAfterAccess(5, TimeUnit.MINUTES)
.build(key -> someEntityCount(key));
public int cachedEntityCount(final String criteria) {
return entityCountCache.get(criteria);
}
private int someEntityCount(final String criteria) {
System.out.print(String.format("Inside function: %s", criteria));
return 5;
}
}
Class B:
package com.example.withoutspring;
public class B {
private CaffeineCachingService cacheableService;
public B() {
cacheableService = new CaffeineCachingService();
}
public int getEntityCount(String criteria) {
return cacheableService.cachedEntityCount(criteria);
}
}
And the same test but without Spring:
package com.example.withoutspring;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CaffeineCacheableTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private B b = new B();
#BeforeEach
public void init() {
System.setOut(new PrintStream(outContent));
}
#Test
public void testCaching() {
b.getEntityCount("foo");
b.getEntityCount("foo");
assertEquals("Inside function: foo", outContent.toString());
}
}
Obviously you need to tune the cache to perform how you want it so probably evicting the cached values after 5 minutes is not what you want but if you visit the Caffeine Github page you will see a lot of detailed examples how to configure the cache to meet your use-case.
Hope this helps!

trying to test spring caching . i need to test my #cacheble annatation on my repository

i am actually trying to test my caching mechanism . i am using caffine cache.
Test: i am calling caching method twice and expecting the same result for multiple method invocation. i.e When i cal method second time with same signature it shouldn't cal the method it should get the data from cache.
Problem: My code is actually invoking the method twice . i am mocking my repository. Please guide me, if anyone has solved this kind of problem.
my repo :
public class TemplateRepositoryOracle implements TemplateRepository
#Cacheable("Templates")
#Override
public Optional<NotificationTemplate> getNotificationTemplate(String eventTypeId, String destinationType, String destinationSubType) {}
Test:
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Ticker;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration
public class TemplateRepositoyOracleTest {
interface TemplateRepository {
#Cacheable("Templates")
Optional<Template> getNotificationTemplate(String eventTypeId, String destinationType, String destinationSubType);
}
#Configuration
#ConfigurationProperties(prefix = "caching")
#Data
#Slf4j
static class CacheConfiguration {
#Data
public static class CacheSpec {
private Integer expireAfterWrite;
}
private Map<String, CacheSpec> specs;
#Bean
public CacheManager cacheManager(Ticker ticker) {
SimpleCacheManager manager = new SimpleCacheManager();
if (specs != null) {
List<CaffeineCache> caches =
specs.entrySet().stream()
.map(entry -> buildCache(entry.getKey(),
entry.getValue(),
ticker))
.collect(Collectors.toList());
manager.setCaches(caches);
}
return manager;
}
private CaffeineCache buildCache(String name, CacheSpec cacheSpec, Ticker ticker) {
log.info("Cache {} specified timeout of {} min", name, cacheSpec.getExpireAfterWrite());
final Caffeine<Object, Object> caffeineBuilder
= Caffeine.newBuilder()
.expireAfterWrite(cacheSpec.getExpireAfterWrite(), TimeUnit.MINUTES)
.ticker(ticker);
return new CaffeineCache(name, caffeineBuilder.build());
}
#Bean
public Ticker ticker() {
return Ticker.systemTicker();
}
#Bean
TemplateRepository myRepo() {
return Mockito.mock(TemplateRepository.class);
}
}
#Autowired
CacheManager manager;
#Autowired
TemplateRepository repo;
#Test
public void methodInvocationShouldBeCached() {
Optional<Template> third = Optional.of(new NotificationTemplate(UUID.randomUUID(),"Test",DestinationType.SMS,"test","test",Optional.empty(),Optional.empty()));
Optional<Template> fourth = Optional.of(new NotificationTemplate(UUID.randomUUID(),"Test2",DestinationType.SMS,"test2","test2",Optional.empty(),Optional.empty()));
// the mock to return *different* objects for the first and second call
Mockito.when(repo.getNotificationTemplate(Mockito.any(String.class),Mockito.any(String.class),Mockito.any(String.class))).thenReturn(third);
// First invocation returns object returned by the method
Object result = repo.getNotificationTemplate("1","1","1");
assertThat(result, is(third));
// Second invocation should return cached value, *not* second (as set up above)
result = repo.getNotificationTemplate("1","1","1");
assertThat(result, is(third));
// Verify repository method was invoked once
Mockito.verify(repo, Mockito.times(1)).getNotificationTemplate("1","1","1");
assertThat(manager.getCache("notificationTemplates").get(""), is(notNullValue()));
// Third invocation with different key is triggers the second invocation of the repo method
result = repo.getNotificationTemplate("2","2","2");
assertThat(result, is(fourth));
}
}
Property file:
caching:
specs:
Templates:
expireAfterWrite: 1440

Qualifier not working in spring

CallingApp.java
#Service
#ComponentScan(basePackages = { "com.codegeekslab.type" })
public class CallingApp {
#Autowired
#Qualifier("BasicPhone")
private Phone phone;
public CallingApp(Phone phone) {
this.phone = phone;
}
public void makeCall(int number) {
phone.openApp(number);
}
}
Phone.java
package com.geekslab.device;
public interface Phone {
public void openApp(int number);
}
BasicPhone.java
package com.codegeekslab.type;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.geekslab.device.Phone;
#Component("BasicPhone")
public class BasicPhone implements Phone {
{
System.out.println("BasicPhone");
}
public void openApp(int number) {
System.out.println("calling via simcard... " + number);
}
}
SmartPhone.java
package com.codegeekslab.type;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.geekslab.device.Phone;
#Component("SmartPhone")
public class SmartPhone implements Phone {
{
System.out.println("SmartPhone");
}
public void openApp(int number) {
System.out.println("calling via whatsapp..." + number);
}
}
Test.java
package com.codegeekslab.test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.codegeekslab.app.CallingApp;
import com.codegeekslab.type.BasicPhone;
import com.codegeekslab.type.SmartPhone;
import com.geekslab.device.Phone;
public class Test {
public static void main(String[] args) {
//ApplicationContext context =
// new GenericXmlApplicationContext("beans.xml");
//SpringHelloWorld helloSpring = context.getBean("springHelloWorld", SpringHelloWorld.class);
//comment this for xml less spring
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.codegeekslab.app","com.codegeekslab.type");
//context.register( BasicPhone.class,SmartPhone.class,CallingApp.class);
context.refresh();
CallingApp callingApp = context.getBean("callingApp", CallingApp.class);
callingApp.makeCall(99999);
}
}
Even though i am giving qualifier as #Qualifier("BasicPhone") in CallingApp class ,I am getting Exception as follows:
No qualifying bean of type [com.geekslab.device.Phone] is defined: expected single matching bean but found 2: BasicPhone,SmartPhone
You pass phone as a constructor argument in your CallingApp service without specifying the bean.
Try either to put a qualifier at your constructor or stick to the autowire injection which is something your already do.
i removed the CallingApp class constructor and it worked.
public CallingApp(Phone phone) {
this.phone = phone;
}
As Constructor was overriding the setter method.
You need to add no argument constructor
public CallingApp(){
//do nothing
}
public CallingApp(Phone phone) {
this.phone = phone;
}

Categories

Resources