This is the 15th day of my participation in the More text Challenge. For more details, see more text Challenge


Related articles

Spring series: Spring series


Use annotations for autowiring

  • In the last article, we explained several ways to automate Spring assembly, but in actual development, we use annotations to implement assembly most often.
First, the @autowired

Do not use the beans header file automatically created by idea to import beans.② Add the introduction of file header:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
Copy the code

③ XML: enable attribute annotation support

	<! -- Enable attribute annotation support -->
    <context:annotation-config/>

    <bean id="car" class="entity.Car"/>
    <bean id="house" class="entity.House"/>
    <bean id="person" class="entity.Person"/>
Copy the code

(4) Entity class:

public class Person {
    @Autowired
    private Car car;
    @Autowired
    private House house;

    private String name;
    private int age;
    private String like;

    public Car getCar(a) {
        return car;
    }

    public House getHouse(a) {
        return house;
    }

    public String getName(a) {
        return name;
    }

    public int getAge(a) {
        return age;
    }

    public String getLike(a) {
        returnlike; }}Copy the code
public class Car {
    public void getName(a){
        System.out.println("I'm a car!); }}Copy the code
public class House {
    public void getName(a){
        System.out.println("I'm a house!); }}Copy the code

⑤ Test class:

public class TestAnonatation {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("person",Person.class); person.getCar().getName(); person.getHouse().getName(); }}Copy the code

⑥ Execution result:His conclusion:

  • With @AutoWired, you can omit the set method in the Person class, simplifying the amount of code
  • @autowired (Required = false) defaults to true, objects are not allowed to be null, false means that objects can be null
  • @autowired is actually equivalent to byType auto-assembly, it is based on type to auto-assembly
Second, @ the Qualifier
  • Qualifier is a composite annotation and cannot be used alone
  • Use @autoWired to specify byName() assembly
	@Autowired
    @Qualifier(value = "car")
    private Car car;
    @Autowired
    @Qualifier(value = "house")
    private House house;
Copy the code
  • Note this, you can directly click on the specified location of the XML!

  • Execution Result:

Third, @ the Resource

1) XML:

	@Resource(name = "car")
    private Car car;
    @Resource
    private House house;
Copy the code

② Execution result:

(3) conclusion:

  • When name is specified, byName is used, and the ID in XML cannot be empty!
  • If byName is not specified, byType is used by default. If byType is not specified, byName is used by default.
  • All failed!
4. @Autowired and @Resource

@autowired and @Resource:

  • Both @Autowired and @Resource can be used to assemble beans. You can write it on a field, you can write it on a setter method.

  • @autowired defaults to assembly by type (which is part of the Spring specification). By default, you must require that dependent objects exist. To allow null values, you can set the required attribute to false. @Autowired(Required =false), which we can use in conjunction with the @Qualifier annotation if we want to use name assembly

  • @Resource (which belongs to J2EE return), by default, is assembled by name, which can be specified through the name attribute. If the name attribute is not specified, when an annotation is written on a field, it defaults to the name of the field to be looked up by name, and when an annotation is written on a setter method, it defaults to the name of the property to be assembled. The assembly is done by type when no bean matching the name can be found. Note, however, that if the name attribute is specified, it will only be assembled by name.

  • They do the same thing: inject objects with annotations, but in a different order. @autowired first byType, @Resource first byName


I see no ending, but I will search high and low

If you think I blogger writes good! Writing is not easy, please like, follow, comment to encourage the blogger ~hahah