Change Bean name for Bean implementations packaged in Jar - Spring Boot - spring-boot

I am using spring boot and Autowired NamedParameterJdbcTemplate as
#Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
I want to use different name for instance
private NamedParameterJdbcTemplate myTemplate;
How can this be achived in spring boot as I dont I have access to implementation class as I am using spring JDBC in POM as dependency.

Spring by default autowires the dependencies by Type reference not by name. Hence ur code will directly work without any changes required.
private NamedParameterJdbcTemplate myTemplate;
Spring will look for a bean of type NamedParameterJdbcTemplate and Autowire it, unless u have explicitly specified autowired by name. In the case of autowireby name u can use the #Qualifier to specify the bean name to autowire.
#Autowired
#Qualifier("beanName")

Related

Spring function based injection

Is there any way to inject a spring bean using #Autowired and instead of using a qualifier, using a custom annotation which resolves the bean based on a function and an argument?
For example
#Autowired
#FunctionQualifier(method=fetchCorrectBean)
private MyService myService;
The fetchCorrectBean is a function which will fetch the bean from the application context and inject it.

Bean Injection without #Autowire Annotation in spring

How to inject a class without using auto-wire annotation in spring?
There are 3 ways to do injection with Spring:
Use #Autowired (which you don't want)
Use #Inject (has most of the features as Autowired)
Use XML configuration. This doesn't require any annotations on the classes.

Objects initialization in spring framework

i am studying spring framework i have some doubts to confirm:
I see that class objects are created as beans in xml file ... but my doubt is ... only pojo class beans need to be defined in xml for instatiation or all classes
eg: my custom code class EncryptionUtil class that helps in encrypting data and so on custom logic classes also need to be instantiated using beans?This is my primary concern
what about cases like i use
JSONObject j = new JSONOBJect() (External libs);
ArrayList<String> a = new ArrayList<String>();
( java default object and collections)
do these classes also need to have bean in xml?
i exactly dont know if spring ioc will instantiate each and every object or we need to instantiate only some objects
also, in spring app does "new " keyword work for creating objects
What should we use to instantiate bean in spring mvc ?
ie : like i used ApplicationContext in my spring app , should i get bean everywhere i need
will there be any problem if i use multiple annotations ie : of spring as well as hibernate on same class at same time?
eg: something like this
#Id #GeneratedValue
#Column(name = "id")
private int id;
but if i want id to be autowired too ...
#Autowired
#Id #GeneratedValue
#Column(name = "id")
private int id;
will this work?
Declare only classes as Spring Beans which you don't want to create with new. Further Spring Beans are normally singletons. This means there is only one instance of this class. In most cases Spring Beans are not POJOs. E.g mostly i declare DAOs, Service, Controller classes as Spring Beans.
No there is no need to declare these as Spring Beans.
Yes, new works. But these instances are not managed by the spring container. And you should only instantiate non spring beans with new. Spring Beans itself are instantiated by the Spring IOC container.
Inject beans to other beans with xml config or by annotation config (#Autowired).
You can mix spring annotations with annotations of other frameworks. But your example with the id doesn't work and makes no sense, since entity id's wont' be injected. Also your Hibernate entity must be also a spring bean (in this case declared as prototype). You can inject values and beans only to spring beans.

Use Spring annotation to inject dependency

In my project, i see a spring dependency inujection syntax like this in my integration layer:
applicationContext.getBean("beanName");
where applicationContext instance of ClasspathXMLApplicationContext and "beanName" is defined in the spring xml.
If I want to inject it with Annotation, which one should I use? #Inject,#Autowired,#Resource. Seems like I cna use any one of these and I cannot seem to be able to decide which one.
This is SPring integration layer , not MVC layer but i dont think that makes any difference.
#Inject and #Autowired do the same thing, it autowires by type. #Inject is preferred because it is a java annotation and does not couple you to Spring
#Resource autowires by name. This is useful when you have many beans of the same type. You can also use #Named along with #Inject for the same behavior.
#Inject is synonymous to #Autowired.
#Autowired moreover offers optional injection #Autowired(reqired=false) #Inject doesn't have this option.
#Inject and #Resource is standardized in JSR-299 so it should be preffered if possible.
in short, order in which mentioned annotations match dependency to be injected:
#Autowired and #Inject
Matches by Type
Restricts by Qualifiers (#Qualifier annotation)
Matches by Name
#Resource
Matches by Name
Matches by Type
Restricts by Qualifiers (#Qualifier, ignored if match is found by name)
You can find more about these annotations here:
http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/
#Inject and #Autowired are similar. The native Spring annotation is #Autowired, even Spring also support the Java #Inject annotation, that makes the same.
#Resource is Java annotation, Spring also support that annotation. It means JNDI resource.
See
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-annotation-config
http://docs.oracle.com/javase/7/docs/api/javax/annotation/Resource.html
http://docs.oracle.com/javaee/6/api/javax/inject/Inject.html

Autowiring spring bean by name using annotation

In Springs latest version, we can autowire a bean using annotation as #Autowired. This will autowire the bean using its type(or constructor, if applied on it).
Is there any way I can use the #Autowired annotation based on the bean name which we were doing without annotation in Spring's XML file as autowire="byName"?
You can use:
#Autowired
#Qualifier("beanname")
According to the #Qualifier javadoc
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring
You can use JSR-250 #Resource for by-name bean autowiring, unless you need constructor injection or multi-parameter method injection.
From the docs:
If you intend to express annotation-driven injection by name, do not primarily use #Autowired, even if is technically capable of referring to a bean name through #Qualifier values. Instead, use the JSR-250 #Resource annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.
If you want to define name of the bean with which they will be registered in DI container, you can pass the name in annotation itself e.g. #Service (“employeeManager”).
Then using below code you can enable autowire by Name
#Autowired
#Qualifier("employeeManager")
private EmployeeManagerService employeeManagerService;
I was using bean name proxy which was messing up autowiring by name. #Resource didn't have that issue since it doesn't care about type. So now I know one reason for this recommendation by Spring developers :-) Just FYI
Use #Component("beanname") in the java class definition of your bean
Then while autowiring use JSR 330
#Inject
#Named(Value="beanname")

Resources