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

  1. -- if there is a class contains a property named age ,this class's path is com.lfg.User
  2. <bean id="user" class="com.lfg.User">
  3. <property name="age" value="18" />
  4. </bean>

As follows: wired

  1. #if there is a classA contains a property which type is a classB,classB's path is com.lfg.Dog
  2. <bean id="dog" class="com.lfg.Dog"/>
  3. <bean id="user" class="com.lfg.User">
  4. <proerties name="dog" ref="dog"/>
  5. </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

  1. -- classA User;classB Dog;
  2. -- classA have a Dog type property named dog;
  3. <bean id="dog" class="com.lfg.Dog"/>
  4. <bean id="user" class="com.lfg.User" autowired="byName">
  5. </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, can be or whatever the bean id is, keep class=”com.lfg.Dog”,SpringFramework will find it;

Spring & annotations

SpringFramework provide multiple annotations to process autowired;

  1. @Autowired,do autowired by byType ,if byType cant work, use byName,if byName still dont work, thorw error;
  2. @Resource,do autowired by byName ,if byName cant finish work,will try again with byType,if byType still dont work, throw error;

For example:

  1. @Component
  2. public class User
  3. {
  4. @Value(18)
  5. private int age;
  6. @Autowired
  7. private Dog dog;
  8. @Resource
  9. private Cat cat;
  10. }

Note that Dog obj and Cat obj are still need be registered in xmlConfiguration file;

  1. <beans>
  2. <bean id="dog" class="com.lfg.Dog"/>
  3. <bean id="cat" class="com.lfg.Cat"/>
  4. --have to enable annotation in this file
  5. <context:annotation-config/>
  6. </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

  1. @Component
  2. @ConfigurationProperties(perfix="person")
  3. public class Person
  4. {
  5. private int age;
  6. private String name;
  7. private Dog dog;
  8. }

in SpringBoot ,you can use yaml file finish DI

  1. person:
  2. age: 18
  3. name: lfg
  4. dog: {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;