I have a usecase where i want a particular class or a spring bean to be injected in a set of classes that have a particular annotation. Basically i want a few set of methods to be added to the annotated classes.
Suppose i have a class annotated with annotation #Finders. Then i want the class to be able to use some predefined methods like
findById(), findByName() etc to be available to the class and we should be able to call these functions in a static context like
User.findById() and so on.
Is there a way to inject these methods to the class using Java or Spring. Can i use reflection to do this.
Is there any other way i can get this done in java.
Related
I have a set of classes I would like to require a certain method implemented with Spring's #Scheduled annotation. I don't want a default defined, I just want to require that the annotation is used on a certain method.
Is this possible either through Interfaces or abstract classes?
For one of my requirment i have created a inteface with multiple methods and each method annotated with my own java custom annotation.
I have a spring aop aspect for my implementation class and i am not able to get my custom annotation in aop aspect.
after doing debug i understood my custom annotation is part of the interface and not in implementation class.
How can i get my custom annotation in my implementation methods which declared in interface ?
In Java, annotations are not inhereted from interfaces.With aspects you must annotate the implementation class (and/or methods within that class), not the interface (if any) that the class implements. String aop follows Java’s rule that annotations on interfaces are not inherited.
So, if you want to work with your annotation , create an abstract super class to be able to do this.
Inside aspect you work with proxy object and methods are wrapped in proxy calls. But if you know a real class/interface , you can get annotation by reflection api from source class/interface
There is a class whose constructor is annotated with #Autowired. I want to create an object of this class without actually passing the constructor parameters and using the default parameters. Can someone tell me how can i create an object of this class?
One of the reasons of using Spring framework is you don't create objects and manage them manually spring does it for you.If you can give some more details example code or something it will help to understand your situation better.
Spring is a dependency injection framework (it does hosts of other stuff as well). So the whole point is not to "create" your own instances and re-use the instances that Spring has created for you.
If you want create object of class whose constructor is marked as #Autowired then still you can create object of that class using normal new java operator.
You can create an object of that class by just marking your field with #Autowired.Spring handles object creation for you.
I am new to Spring and I want to start using dependency injection.
I have many classes, and they hold dependencies as interfaces references, which I populate with objects I send as parameters to the constructor.
I know I can configure each class separately to inject each dependency with a specific implementation, but that means if I want to change the implementation of a specific dependency to all classes then I need to change every class, I want to be able to do that in one place for all classes.
What is the best way to do that?
In your class you will inject the interface:
public class Foo {
#Autowried
private Boo boo;
}
Then you will control which implantation gets added to the spring container :
#Service
public class BooImpl implements Boo {
}
Now all you need to do is change the implementation.
If possible I would use Spring Boot, you can download the examples and run the straight away https://spring.io/guides/gs/spring-boot/
I am testing a set of classes that will, ultimately, be used to create beans within Spring contexts. I am using JUnit for testing. I want to ensure that when instances of these classes are instantiated programmatically within a test that the life-cycle methods and the #Required setters are correctly called. I know I can do this via a plain-old-java-proxy but am wondering if there exists specific tools within Spring (or elsewhere) that will help me.
I am looking to write something like
X x = ???.newBeanInstance(X.class);
x.setFoo(foo);
x.setBar(bar);
x.$postConstruct();
...
x.$preDestroy();
x = null;
The other, less desirable, option is to just create the context's XML and use it to initialize the instances.
If you bring-up the whole context, you can use the spring-test package and its #ContextConfiguration.
If you want to handle things manually, you are on your own (almost). If you have field injection, you can use ReflectionTestUtils to set dependencies. But apart from that you:
instantiate the object with new X()
call setters
call the initializer method (you should know which one it is)
call the destruction method in the end