At first ,Autowired is a way to automating dependency inject;
Both of Spring framework and Spring Boot provide AutoWired functions;
without annotations
in spring framework,spring framework need process DI after a bean has been created;DI can be finished by 2 ways;
DI with Configuration file
DI with xml configuration files; if properties are basic type like int or String even map or list , you can choose DI with properties tag in context configuration file ; if properties are objs , should register a log of beans (infact create a lot of bean objs) which are general bean needed,then tell spring by beans id : general bean need these beans to finish DI,if general bean need others beans to work,we call this process wired rather than DI ;
As follows: DI
-- if there is a class contains a property named age ,this class's path is com.lfg.User<bean id="user" class="com.lfg.User"><property name="age" value="18" /></bean>
As follows: wired
#if there is a classA contains a property which type is a classB,classB's path is com.lfg.Dog<bean id="dog" class="com.lfg.Dog"/><bean id="user" class="com.lfg.User"><proerties name="dog" ref="dog"/></bean>
In fact DI and wired almost is same thing;
DI with Configuration file with tag’s property “autowired”
SpringFramework provide a function to help our automatic finish
-- classA User;classB Dog;-- classA have a Dog type property named dog;<bean id="dog" class="com.lfg.Dog"/><bean id="user" class="com.lfg.User" autowired="byName"></bean>
Please note that tag’s property autowired ;value “byName” means that SpringFramework will check Object type properties;
then SpringFramework find there is a Dog type is not initialized,but bean “user” have “byName” autowired configuration;
then SpringFramework will check if context contains a bean named “dog”;
Of course ,SpingFramework can find it;
then property “dog” is initialized by bean”dog”;
theere are 2 options of autowired’s value:byName and byType
if choose byType,Spring will check beans which contained by Context by Type;for example, in byType model,
Spring & annotations
SpringFramework provide multiple annotations to process autowired;
- @Autowired,do autowired by byType ,if byType cant work, use byName,if byName still dont work, thorw error;
- @Resource,do autowired by byName ,if byName cant finish work,will try again with byType,if byType still dont work, throw error;
For example:
@Componentpublic class User{@Value(18)private int age;@Autowiredprivate Dog dog;@Resourceprivate Cat cat;}
Note that Dog obj and Cat obj are still need be registered in xmlConfiguration file;
<beans><bean id="dog" class="com.lfg.Dog"/><bean id="cat" class="com.lfg.Cat"/>--have to enable annotation in this file<context:annotation-config/></beans>
Spring Boot & annotation
The quintessence of Spring Boot is Less configurations ;so SpringBoot dont recommend use configuration file to register javaBeans;
SpringBoot provide a new way to do DI
@Component@ConfigurationProperties(perfix="person")public class Person{private int age;private String name;private Dog dog;}
in SpringBoot ,you can use yaml file finish DI
person:age: 18name: lfgdog: {name: dog,age: 3}
you can use yaml finished DI without any bean;even though class Person contain a Dog obj,the Dog obj is still not be registered; Dog obj just exist in yaml file;
