I am autowiring a property(its a component) in an abstract class, This is not working , always coming a null; The same property has been autowired in other abstract class which is working absolutely fine. can some one please tell me , what would be the problem?
public abstract class BaseRegistry {
#Autowired
PropertyConfig propertyConfig;
}
#Repository
public class OptionsRegistry extends BaseRegistry{
}
#Component
public class PropertyConfig {
}
Related
I have a Service class that has a generic type and a setController method that is based on the same generic type. the generic type of the servic object is only known at the time of declaration.
The problem is now when i define a ControllerImpl where the generic type is defined the #Autowired method of setController does not use that component.
Has somebody an idea how to fix it and keep the ServiceImpl generic. (it would work when i define the typ in ServiceImpl as well).
The following example show the problem i'm facing with:
#SpringBootTest
#ActiveProfiles("local")
public class AccessTest {
#Autowired
private ServiceA<BeanA> service;
#Test
void test(){
Assertions.assertNotNull(service.controller);
}
interface ValueGetter{
}
static class BeanA implements ValueGetter{
}
static class AbstractService<B extends ValueGetter>{
Controller<B> controller;
#Autowired
void setController(#Nullable Controller<B> controller){
this.controller = controller;
}
}
interface Controller<B extends ValueGetter>{
void doSomething(B value);
}
//not inner class
#Service
public class ServiceA<B extends AccessTest.ValueGetter> extends AccessTest.AbstractService<B> {
}
//not inner class
#Component
public class ControllerImpl implements AccessTest.Controller<AccessTest.BeanA> {
#Override
public void doSomething(final AccessTest.BeanA value) {
}
}
}
I am having following code below.
#Builder(toBuilder = true)
#AllArgsConstructor(access = AccessLevel.PRIVATE)
#NoArgsConstructor(access = AccessLevel.PRIVATE)
#ToString
#EqualsAndHashCode
#Configurable
public class Employee {
#Autowired
#Qualifier("findEmpByDepartment")
private Function<Long, Long> empByDepartment;
private void save() {
this.empByDepartment.getList();
}
}
and FindEmpByDepartment class below.
#Component("findEmpByDepartment")
public class FindEmpByDepartment implements Function<Long, Long> {
public void getList() {
}
....
}
My problem is I am always getting null when invoke
this.empByDepartment.getList();
line. Here this.empByDepartment is coming as null. Any idea why it is like this?
Thanks
May be you would have missed annotating any class in the flow hierarchy .
#Service, #Repository and #Controller are all specializations of #Component, so any class you want to auto-wire needs to be annotated with one of them.
IoC is like the cool kid on the block and if you are using Spring then you need to be using it all the time .
So make sure you do not have any object created with new operator in the entire flow .
#Controller
public class Controller {
#GetMapping("/example")
public String example() {
MyService my = new MyService();
my.doStuff();
}
}
#Service
public class MyService() {
#Autowired
MyRepository repo;
public void doStuff() {
repo.findByName( "steve" );
}
}
#Repository
public interface MyRepository extends CrudRepository<My, Long> {
List<My> findByName( String name );
}
This will throw a NullPointerException in the service class when it tries to access the MyRepository auto-wired Repository, not because there is anything wrong with the wiring of the Repository but because you instantiated MyService() manually with MyService my = new MyService().
For more details , you can check
https://www.moreofless.co.uk/spring-mvc-java-autowired-component-null-repository-service/
I have the following:
public abstract class ReportGenerationStrategy<T extends ReportParameter> {
public abstract void generate(T reportParameter) throws IOException;
}
The subclasses:
#Component
#AllArgsConstructor
public class DeferredRevenueReportGenerationStrategy extends ReportGenerationStrategy<DeferredRevenueReportParameter> {
}
#Component
#AllArgsConstructor
public class OffBalanceExposureReportGenerationStrategy extends ReportGenerationStrategy<OffBalanceExposureReportParameter> {
}
I am creating a map with the subclasses:
#Bean
public Map<ReportType, ReportGenerationStrategy<? extends ReportParameter>> generationStrategies(
#Qualifier("deferredRevenueReportGenerationStrategy") final ReportGenerationStrategy<? extends ReportParameter> deferredRevenue,
#Qualifier("offBalanceExposureReportGenerationStrategy") final ReportGenerationStrategy<? extends ReportParameter> offBalanceExposure
)
{
return ImmutableMap.of(
ReportType.DEFERRED_REVENUE, deferredRevenue,
ReportType.OFF_BALANCE_EXPOSURE, offBalanceExposure
);
}
And trying to inject the map as following:
#AllArgsConstructor
#Slf4j
public class ReportApplicationServices {
private final Map<ReportType, ReportGenerationStrategy<ReportParameter>> generationStrategies; //Empty map injected
}
But I receive an empty map on the field...
Why is this happening?
I believe you missed to use #Autowire annotation on the private final Map that you have declared.
Hope this helps and solves the issue.
This super class DAO:
public class CrudDAO{
}
This child class:
#Repository
public class JnsTimeDao extends CrudDAO {
}
#Repository
public class BatchDAO extends CrudDAO {
}
this super service class
#Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDAO> {
#Autowired
protected D dao;
}
startup error:
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [com.gp.dao.CrudDAO] is defined: expected
single matching bean but found 2: batchDAO,jnsTimeDao
There are 2 beans of type CrudDAO. So, Spring won't be able to understand which bean to inject. Can be solved as follows
#Repository("jnsTimeDao")
public class JnsTimeDao extends CrudDAO {
}
#Repository("batchDao")
public class BatchDAO extends CrudDAO {
}
While injecting use #Qualifier
#Transactional(readOnly = true)
public abstract class CrudService<D extends CrudDAO> {
#Autowired
#Qualifier("batchDao")
protected D dao;
}
I have a scenario wherein my ServiceImpl and Business Class implements the same interface. But im unable to autowire both of them.
#RestController
#RequestMapping("/myservice")
public interface myInterface{
#RequestMapping(value="/getSomething/{input}", method=RequestMethod.GET)
doSomething(String input);
}
Now i have two classes which implements same interface
#Component
#Qualifier("doSomethingImpl")
public class DoSomethingImpl implements myInterface{
#Autowired
#Qualifier("businessLayer")
myInterface businessLayer;
doSomething(#PathVariable String input){
//my logic here
}
}
#Component
#Qualifier("businessLayer")
public class BusinessLayer implements myInterface{
doSomething(#PathVariable String input){
//my logic here
}
}
Now when i run it on server i'm getting following error
Cannot map handler 'DoSomethingImpl' to URL path
[/myservice/getSomething/{input}]: There is already handler of type
[class com.mypackage.business.BusinessLayer] mapped.
Could someone please help me to resolve this error
The problem is that both controllers are being mapped to the same path. I suggest you change your code to something like:
public interface myInterface{
#RequestMapping(value="/getSomething/{input}", method=RequestMethod.GET)
public Whatever doSomething(String input) {
//whatever
}
}
#RequestMapping("something")
#RestController
public class DoSomethingImpl implements myInterface{
}
#RequestMapping("somethingElse")
#RestController
public class BusinessLayer implements myInterface{
}