Spring两种属性注入方式:set和有参构造

IOC操作--Bean管理

Bean管理指的是两种操作:

  1、创建对象

  2、属性注入

Bean管理的操作有两种实现方式:

  1、xml配置文件方式实现

  2、注解方式实现

 --------------------------------------------------------------------------------------------------------------------------------------------------------

Spring提供的IOC容器实现方式,ApplicationContext接口

  此接口有两个实现类:

  1、ClassPathXmlApplicationContext(配置的xml文件必须在src同级目录下,否则找不到xml路径)

  2、FileSystemXmlApplicationContext(配置的xml文件必须在src目录外,否则找不到xml路径)

----------------------------------------------------------------------------------------------------------------------------------------------------------

使用xml方式注入属性

<1>DI:注入属性

1、set方法注入

2、用有参构造进行注入

-------------------------------------------------------

一、set方法注入(举例)

  1、首先建一个Book类,里面加一个成员变量,使用set来为name注入值

 1 public class Book {
 2     private String name;
 3     //set方法
 4     public void setName(String name) {
 5         this.name = name;
 6     }
 7 
 8     //测试方法
 9     public void test() {
10         System.out.println(name);
11     }
12 }

  2、配置xml,set方式注入,要用<property>标签进行赋值操作,value写需要为name赋的值

<!--    为Bean标签添加属性值-->
    <bean id="book" class="Core.Book">
    <property name="name" value="Java基础"></property>
</bean>

  3、测试

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

public class TestBook {
public static void main(String[] args) {
//1、加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("Book.xml");
//2、获取xml配置文件创建的对象
Book book = context.getBean("book", Book.class);
//3、测试是否为name进行赋值
book.test();
}
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

二、有参构造注入

package Core;
public class Person {
    private String name;
    private int age;

    //有参构造注入
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
   //测试方法
    public void add(){
        System.out.println(name+age);
    }
}

  在src同级目录下配置xml,有参构造方式注入要使用<constructor-arg>标签

    <bean id="person" class="Core.Person">
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="age" value="22"></constructor-arg>
    </bean>
 1 package Test;
 2 
 3 import Core.Person;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class TestPerson {
 8     public static void main(String[] args) {
 9         ApplicationContext context=new ClassPathXmlApplicationContext("Person.xml");
10         Person person = context.getBean("person", Person.class);
11         person.add();
12     }
13 }

 

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  null和特殊符号的注入方式

1、在属性中注入 null 值,可以在<constructor-arg>或<property>标签中加入<null/>,但int类型属性不能注入null

<bean id="person" class="Core.Person">
    <constructor-arg name="name">
        <null/>
    </constructor-arg>

2、特殊符号注入,橙黄色字体可以任意修改   下面是格式举例:  <![CDATA[《河南》]]>

<property name="address">
            <value>
                <![CDATA[《河南》]]>
            </value>
        </property>

 

posted @ 2021-03-08 20:41  by晚风  阅读(608)  评论(0)    收藏  举报