πŸ“œ ⬆️ ⬇️

Spring IoC Annotation-based configuration, part 2

In the previous article, I talked about the main annotations of Spring IoC, but there are still a few interesting things that I would like to tell.
For those who do not know what the Spring Framework is, I suggest reading this article .



A few more words about @Autowired


The @Autowired annotation can be applied not only to bin fields to inject dependencies in them, but also to setters, constructors, and methods. Consider what it means to @Autowired in each of these cases.
')
1. @Autowired applied to the setter
Let our experimental bin, which we will inject, look like this:
package ru.mypackage;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service( "testBean" )
@Scope( "singleton" )
public class TestBean {
private String data = "I am a singleton!!" ;

public String getData() {
return data;
}

public void setData( String data) {
this .data = data;
}
}


* This source code was highlighted with Source Code Highlighter .

And bin in which we will inject such:
package ru.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service( "lab1Bean" )
@Scope( "session" )
public class SimpleBean {

private TestBean bean;

@PostConstruct
public void init() {
System. out .println(bean.getData());
}

public void setBean(TestBean bean) {
this .bean = bean;
}
}


* This source code was highlighted with Source Code Highlighter .

Actually in this case, if we put @Autowired before the setBean setter:
@Autowired
public void setBean(TestBean bean) {
this .bean = bean;
}


* This source code was highlighted with Source Code Highlighter .

he will put a testbean singleton in the bin field and in the console we will see the cherished β€œI am a singleton !!”.

2. @Autowired applied to the constructor
The situation is similar - if we add the following constructor:
@Autowired
public SimpleBean (TestBean bean) {
this .bean = bean;
}


* This source code was highlighted with Source Code Highlighter .

then in the console we will see the long-awaited "I am a singleton !!".

3. @Autowired applied to the method
The most interesting situation, here we add this method
@Autowired
public void doSomething(TestBean bean) {
this .bean = bean;
System. out .println( "I'm an autowired method and " + bean.getData());
}


* This source code was highlighted with Source Code Highlighter .

Spring will automatically call doSomething and give it an instance of TestBean. The console will appear:
I'm an autowired method and I am a singleton !!!
I am a singleton !!!
The first prints doSomething, the second prints init.
Note that you must set the bean field value; Spring will not do this for you. That is, if you do not write this.bean = bean; then in the init method there will be a NullPointerException.
@Autowired also has an optional required property. If required = false, Spring will not throw an exception if it does not find the required bean in the context. That is, this class will be created quite normally:
package ru.mypackage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

@Service( "lab1Bean" )
@Scope( "session" )
public class SimpleBean {

@Autowired(required= false )
private TestBean bean;

@PostConstruct
public void init() {
// System.out.println(bean.getData());
}

public void setBean(TestBean bean) {
this .bean = bean;
}
}


* This source code was highlighted with Source Code Highlighter .

Abstract Qualifier


This annotation allows several specification of a bin, which is necessary for @Autowired. Qualifier takes one input parameter - the name of the bean.
@Autowired
@Qualifier( "specialTestBean" )
private TestBean bean;


* This source code was highlighted with Source Code Highlighter .

This construct will search in the context of a bin named specialTestBean and in our example we will respectively receive an exception, since the TestBean is declared with the name 'testBean' (@Service ("testBean")).
On the basis of Qualifier, you can create your own attributes of beans, this is written quite well (and, importantly, with a huge number of examples) in the Spring Reference Manual.

Annotation Resource


Resource - Java EE 5 and Java 6 annotation. The action is similar to @Autowired. As a parameter, 'name' can take the name of a bean. The examples above can be rewritten like this:
// @Autowired
//
@Resource
private TestBean bean;


* This source code was highlighted with Source Code Highlighter .

and
// @Autowired
// @Qualifier("specialTestBean")
@Resource(name= "specialTestBean" )
private TestBean bean;


* This source code was highlighted with Source Code Highlighter .


This is where Spring IoC annotations can be completed. We must draw some conclusions ...
Spring has certainly become slimmer, more beautiful with the advent of the configuration style annotations, however, as practice shows, the mechanism is not yet debugged, because sometimes it happens that Spring, for the life of me, does not want to take an annotated bin, and the one described through XML easily swallows.

Source: https://habr.com/ru/post/48606/


All Articles