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


Related articles

Spring series: Spring series


Constructor injection
  • Constructor injection was discussed in more detail in the last article, so I won’t go into detail here. Let’s just take the simplest example.

① Entity class:

public class Person {
    private String name;
    private int age;
    private String like;
    private String high;
    
    public Person(String name, int age, String like, String high) 	{
        this.name = name;
        this.age = age;
        this.like = like;
        this.high = high;
    }
    // The set, get, and tostring methods are omitted because of space.
}
Copy the code

(2) XML:


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean name="person1,person2 person3; person4" class="entity.Person">
        <constructor-arg index="0" value="Ding Da 1"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="Fishing 1"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
</beans>
Copy the code

③ Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person person = Context.getBean("person4", Person.class); System.out.println(person); }}Copy the code

④ Execution result:(5) conclusion:

  • No-parameter constructors, needless to say, cannot inject values
  • Values can be injected through a constructor with a parameter
2. Set Injection (key !!!!!)
  • Premise: Entity class:
public class Pojo {
    private String name;
    private Person person;
    private int[] intArr;
    private List<String> list;
    private Map<String,Object> map;
    private Set<String> set;
    private String like;
    private Properties info;
}
// The set, get, and tostring methods are omitted because of space.
Copy the code
2.01. Constant injection

1) beans. XML:

	<bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="name" value="Ding Da"/>
    </bean>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getName()); }}Copy the code

③ Execution result:

2.02. Bean injection

1) beans. XML:

  • Now, the thing to notice here is property
    • Value is used to assign constants
    • Ref can be used to assign beans
	<! -- Inject the value of the entity bean into the constructor -->
	<bean name="testPerson" class="entity.Person">
        <constructor-arg index="0" value="Ding Da 2"/>
        <constructor-arg name="age" value="23"/>
        <constructor-arg type="java.lang.String" value="Fishing 2"/>
        <constructor-arg type="java.lang.String" value="173"/>
    </bean>
	<! -- Inject entity beans into poJOs -->
    <bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="name" value="Ding Da"/>
        <property name="person" ref="testPerson"/>
    </bean>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getName()); System.out.println(testPojo.getPerson()); }}Copy the code

③ Execution result:

2.03 array injection

1) beans. XML:

  • Use array to add values
	<bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="intArr">
            <array>
                <value>7</value>
                <value>5</value>
                <value>9</value>
            </array>
        </property>
    </bean>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        System.out.println(testPojo.getName());
        System.out.println(testPojo.getPerson());
        int[] intArr = testPojo.getIntArr();
        for (int i : intArr) {
            System.out.print(i+""); }}}Copy the code

③ Execution result:

2.04. List injection

1) beans. XML:

	<bean id="Pojo" name="testPojo" class="entity.Pojo">
        <property name="list">
            <list>
                <value>fishing</value>
                <value>fishing</value>
                <value>Eat fish</value>
            </list>
        </property>
    </bean>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        List<String> list = testPojo.getList();
        for (String s : list) {
            System.out.print(s+""); }}}Copy the code

③ Execution result:

2.05. Map injection

1) beans. XML:

	<property name="map">
            <map>
                <entry key="name" value="Ding Da"/>
                <entry key="age" value="23"/>
                <entry key="like" value="Phishing"/>
            </map>
        </property>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        Map<String,Object> map = testPojo.getMap();
        for (Object value : map.values()){
            System.out.print(value+""); }}}Copy the code

③ Execution result:

2.06. Set injection

1) beans. XML:

	<property name="set">
            <set>
                <value>Hero alliance</value>
                <value>Eat chicken</value>
                <value>King glory</value>
                <value>The original god</value>
            </set>
        </property>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class);
        Set<String> set = testPojo.getSet();
        for(String s : set) { System.out.println(s); }}}Copy the code

③ Execution result:

2.07 Null injection

1) beans. XML:

		<property name="like">
            <null></null>
        </property>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getLike()); }}Copy the code

③ Execution result:

2.08. Properties injection

1) beans. XML:

		<property name="info">
            <props>
                <prop key="userName">773530472</prop>
                <prop key="passWord">123456</prop>
                <prop key="Verification code">Tg3O</prop>
            </props>
        </property>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Pojo testPojo = Context.getBean("testPojo", Pojo.class); System.out.println(testPojo.getInfo().toString()); }}Copy the code

③ Execution result:

2.09. P name injection

① Beans.xml: Note that you introduce external constraints in the header file

  • xmlns:p=”www.springframework.org/schema/p”
  • P(properties: properties) namespace, the properties are still set to the set method
  • There can be no constructors with parameters


      
<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="Person" class="entity.Person" p:name="Ding Da" p:age="23"/>
</beans>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person testPojo = Context.getBean("Person", Person.class); System.out.println(testPojo); }}Copy the code

③ Execution result:

2.10. C named Injection

① Beans.xml: You need to add constraint files to your header files

  • xmlns:c=”www.springframework.org/schema/c”
  • C(Constructor: Constructor) namespace, the property is still set to the set method
  • If there are several arguments in the constructor, you must write several arguments in C, otherwise you will report an error!


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="Person" class="entity.Person" c:name="Ding Da" c:age="23" c:like="Ha ha ha." c:high="123"></bean>
</beans>
Copy the code

② Test class:

public class Test {
    public static void main(String[] args) {
        ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");
        Person testPojo = Context.getBean("Person", Person.class); System.out.println(testPojo); }}Copy the code

③ Execution result:


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