Error in Auto wiring component - spring

I am getting a weird issue while using #Autowired annotation
I am injecting one spring component into another spring component like below
import com.myutil.GenericUtils;
#Component
public class MyDomainImpl implements MyDomain {
#Autowired
private GenericUtils genericUtils;
}
and this works fine
When I try to inject the same component a different component ( which is implementing a generic interface ) , it throws the below error.
java.lang.IllegalArgumentException: The argument does not represent an annotation type: Autowired
This is the code which throws the error
import com.myutil.GenericUtils;
#Component
public class MyMapper implements GenericMapper<Mycomponent,MycomponentSO>{
#Autowired
private GenericUtils genericUtils;
}
I am seeing this error first time using Spring FW.
I tried to find out the root cause , but no luck.
If I remove the autowiring , the compilation error does not occur
In case if anybody has faced this error, please advise the solution
Thanks
Lives

I think the problem in auto-wiring MyMapper not auto-wiring GenericUtils, if you are auto-wiring it using interface GenericMapper try to change it to MyMapper as:
#Autowired
private GenericMapper myMapper ;
to
#Autowired
private MyMapper myMapper ;
if it is working so mostly you have a proxy wrapping your bean.
Update:
after re review the error again I found it is most probably coming from java it self, specially from RoundEnvironment it have getElementsAnnotatedWith and this method is throw exception as
IllegalArgumentException - if the argument does not represent an
annotation type
which is your case which is meaning that it is not considering Autowired as annotation type so please check if you have any other class with name Autowired may it is imported wrongly, or you have some missing jars in your build.

Related

How to use #Autowired annotation two or more different Component class for same service?

For example, have a class like as follows.
First XService service in class A is not null but second XService service in AmountValidator is null.I get NullPointerException I try to create bean new it works and then I get same exception when call AmountValidateService outsideRestService in XService.
How can I use XService everywhere that I use #Autowired annotation.
My main class:
#Service
class A extends AbstractA implements IA {
#Autowired
XService service; //first autowired definition. code go to check() method. service not null now.
public doSometing(){
validator.check();
service.methodA();
super.AbstractMethod();
}
}
Validator class used in class A :
class Validator<T> implements IValidator<T> {
public void check(){
rule.check(); // rule have a implements IValidator eg: amountValidator, dateValidator class
}
}
AmountValidator added to rule in class Validator.
#Component
class AmountValidator implements IValidator<T>{
#Autowired
XService service; // code comes here service is null. same service class mentioned above class A.
#Override
public void check(){
service.validateAmount(); // nullPointerException.
}
}
My main Service
#Component
class XService {
#Autowired
AmountValidateService outsideRestService;
public validateAmount(){
outsideRestService.validate(); // nullPointer when create XService with the `New` keyword
}
}
You have an error cause you are trying to create components/beans/services yourself. As i mentioned in comment when you create components yourself it - #Autowired doesn't work - thats you've got NPE
All classes annotated with #Component, #Service are considered special classes which are instantiated by Spring automatically via DI, instantiating them with new defeats the purpose of DI.
These special classes are named Spring Beans.
Every time the application starts, the framework instances all Spring Beans, and all #Autowired fields are injected by Spring automatically. But the Spring Beans must be defined somewhere in the class path. Else you will receive a NoSuchBeanDefinitionException
As an attempt to answer the question, since I don't have a stack trace nor all the Spring Bean definitions:
When you instantiate XService using new XService() your new instance will not actually initialize the field AmountValidateService outsideRestService, effectively leaving it as null.
You may set the field yourself but as I mentioned earlier, it defeats the purpose of DI
Your question is not complex, it is incomplete.

#Autowired bean null when inside at least two levels of components

I have the following structure:
#Component
#Scope("request")
public class ListCompaniesBean {
#Autowired
private JsfUtil jsfUtil;
...
#Component
public class JsfUtil {
#Autowired
private ComponentLookup componentLookup;
...
#Component
public class ComponentLookup {
...
Spring creates the ListCompaniesBean and autowires JsfUtil without any problem, but for some reason ComponentLookup comes null when I use it inside ListCompaniesBean. If I create a new field on ListCompaniesBean and autowire ComponentLookup it is created normaly, seems that I can't create beans inside beans until some level, does anyone had this problem?
I'm not creating new instances of any of them, JstUtil and ComponentLookup are in a separate jar, and everything is on an EAR with a shared context, if you need more information just tell me.
Thanks in advance.

How to avoid the serialization exception (NotSerializableException) with Spring Data

I have a problem with Spring Data, I receive some exception of NotSerializableException type.
Below an example to explain how I have this problem :
#Component
#Scope("session")
public class Bean implements Serializable {
#Autowired
private FooRepository repository;
}
public interface FooRepository extends JpaRepository<Foo, Long>
After I look into in the code, I have seen that the interface org.springframework.data.repository.Reposotiry is not serializable.
And the last version of Spring Data hasn't changed.
I could override the serilization but I don't know if it's the best solution.
Could you tell me if you have found an other solution for this problem or workaroud.
Thank in advance.
You will "get the exception" when an instance must implement Serializable. The exception is thrown by either the serialization run-time or by the instance of the class.
I think the simplest fix is to make FooRepository "serializable", but eventually you cannot since it's a third-party library. So, in your case you must "mark" it as transient; once you do that it will be ignored by the serializable run-time.
#Component
#Scope("session")
public class Bean implements Serializable {
#Autowired
private transient FooRepository repository;
}
NOTE: That's in theory, I've never done that with an injected bean, but the result should be the same. Anyway, that applies to your issue, maybe not to your specific solution.

Superclass has no null constructors but no arguments were given. Spring Integration

I'm developing web application supported by Spring Integration. I'm using 1.0.4.RELEASE. I use CGLib proxies. I have a transactional message endpoint. Everything worked correctly, but I experimented a little bit with annotations. I use annotation-config, which works fine. I started from switching my service-activator configuration from xml to annotations, but it failed.
The following configuration worked correctly:
spring-integration.xml
<channel id="inChannel" />
<channel id="outChannel" />
<service-activator method="myMethod" input-channel="inChannel" ref="myService" output-channel="outChannel" />
MyService.java
#MessageEndpoint
#Transactional
public class MyService {
#Autowired
private MyDao myDao;
public MyObject myMethod(String message) throws Exception {
...
}
}
Trying to achieve exactly the same functionality using annotations (having in mind that I use CGLIB, so I do not need interface, but default constructor) I
removed service-activator from my .xml
changed MyService:
Changed MyService.java
#MessageEndpoint
#Transactional
public class MyService {
#Autowired
private MyDao myDao;
public MyService () {
}
#ServiceActivator(inputChannel="inChannel", outputChannel="outChannel")
public MyObject myMethod(String message) throws Exception {
...
}
}
I get the following error:
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
I've seen many threads describing the following error article, but the problem was about custom classes. My problem regards Spring class.
Error creating bean with name 'myService'
nested exception is org.springframework.aop.framework.AopConfigException
Could not generate CGLIB subclass of class [class org.springframework.integration.handler.ServiceActivatingHandler]
java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
What's wrong? Why Spring tries to create proxy for spring class but not only for MyService? Is my class somehow wrapped? I don't understand what's happening. Help greatly appreciated.
Try taking off the #Autowired Tag. That looks for a constructor or setter method in order to populate that field. Considering you have neither of those, that might be the problem. Just a guess though.
OR you can make myDao package-protected or public (so that Spring can actually autowire it)
for instance:
#Autowired
myDao

Spring annation for service

I am trying to use Spring annotation to define controller, service and dao, but failed.
the error message is
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.abs.absbase.ABSService] is defined: Unsatisfied dependency of type [interface com.abs.absbase.ABSService]: expected at least 1 matching bean
but I do define the service.
Another question is, how to define a sessionfactory to overwrite the HibernateDaoSupport in the ABSDaoImpl ?
Thanks
Source code is
#Controller
#RequestMapping("/abs.do")
public class ABSController {
#Autowired
#Qualifier("ABSService")
ABSService service;
...
}
#Service(value="ABSService")
public class ABSServiceImpl implements ABSService {
#Autowired
#Qualifier("ABSDao")
ABSDao dao;
}
#Repository(value="ABSDao")
public class ABSDaoImpl extends HibernateDaoSupport implements ABSDao {
...
}
According to me you just need to remove the #Qualifier annotation you have defined above the declaration of ABSService object in the controller. And also remove the (value="ABSService") from the #Service annotation on the service.
Hope this helps you.
Cheers.

Resources