Three ways of automatic assembly spring

springIOC container can be assembled automatically Bean, needs to do is autowire attribute <bean> in the manner specified automatic assembly.

ByType: according to the type of automatic assembly. If there are multiple target vessel IOC Bean bean same type, in this case, spring will be unable to determine the most appropriate for the bean property, it is not automated assembly.

ByName: The name of the automatic assembly, the target must be Bean names and attribute names identical settings.

contructor: automatically assembled by the constructor, when there are a plurality of constructors Bean in this manner will be very complex automatic assembly, not recommended,

Car.java

package com.gong.spring.beans.autowire;

public class Car {
    
    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
}

Address.java

package com.gong.spring.beans.autowire;

public class Address {
    private String street;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address [street=" + street + "]";
    }
    
}

Student.java

package com.gong.spring.beans.autowire;
import java.util.List;
import java.util.Map;

public class Student {
    
    private String name;
    private int age;
    private double score;
    private Car car;
    private Address address;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    public Car getCar() {
        return car;
    }
    public void setCar(Car car) {
        this.car = car;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", car=" + car + ", address=" + address
                + "]";
    }
    
    
}

beans-autowire.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="com.gong.spring.beans.autowire.Car" p:name="baoma"></bean>
    <bean id="address" class="com.gong.spring.beans.autowire.Address" p:street="络南街道"></bean>
    <bean id="address2" class="com.gong.spring.beans.autowire.Address" p:street="珞狮街道"></bean>
    <!-- 
    <the bean ID = "Student" class = "com.gong.spring.beans.autowire.Student" P: name = "Tom" 
    P: Age = "12 is" P: Score = "99.00" P: = REF-address " address "P: CAR-REF =" CAR "> </ bean> 
     -> 
    <-! use byName, bean name to be mounted to and setter methods in a class file names consistent if there is no match , the assembly is not 
     <the bean ID = "Student" class = "com.gong.spring.beans.autowire.Student" P: name = "Tom" 
    P: Age = "12 is" P: Score = "99.00" = the autowire "byName"> </ bean> 
    -> 
    <-! the byType according to the type of bean and current attribute automatic assembly bean, if more than one bean match, an exception is thrown vessel IOC -> 
    <bean id = "student"class="com.gong.spring.beans.autowire.Student" p:name="tom"
    p:age="12" p:score="99.00" autowire="byName"></bean>
</beans>

Main.java

package com.gong.spring.beans.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //1.创建spring的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-autowire.xml");
        //2.从容器中获取Bean实例
        Student student = (Student) ctx.getBean("student"); 
        System.out.println(student.toString());
    }
}

XML configuration in the automatic assembly of disadvantages:

(1) Automatic assembly will assemble all the attributes Bean, but only when the individual properties of the assembly, automatic assembly it is not flexible enough;

(2) either according to the type, either by name automatic assembly, can not do both;

(3) In general, rarely used automatic assembly in the actual project, because the benefits of automatic assembly function brings compared to clear more convincing clear configuration;

Guess you like

Origin www.cnblogs.com/xiximayou/p/12152357.html