victory的博客

长安一片月,万户捣衣声

0%

spring | 注入方式

依赖注入的两种方式

  1. 通过bean的setXxx()方法赋值
  2. 通过bean的构造器赋值

    示例

  3. 项目目录

2.创建Student.java

package com.atguigu.spring.di;

public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String sex;
    private Double score;
    public Double getScore() {
        return score;
    }
    public void setScore(Double score) {
        this.score = score;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score + "]";
    }
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(Integer id, String name, Integer age, String sex) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    
    public Student(Integer id, String name, Double score, String sex){
        this.id = id;
        this.name = name;
        this.score = score;
        this.sex = sex;
    }
}

3.创建beans-di.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="s1" class="com.atguigu.spring.di.Student">
        <!-- 通过set方法注入 -->
        <property name="id" value="10010"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
    </bean>
        
    <bean id="s2" class="com.atguigu.spring.di.Student">
        <!-- 通过构造方法注入 -->
        <constructor-arg value="10086"></constructor-arg>
        <constructor-arg value="李四"></constructor-arg>
        <constructor-arg value="24"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
    </bean>
    
    <bean id="s3" class="com.atguigu.spring.di.Student">
        <!-- 通过构造方法注入 -->
        <constructor-arg value="10022"></constructor-arg>
        <constructor-arg value="王五"></constructor-arg>
        <constructor-arg value="90" index="2" type="java.lang.Double"></constructor-arg>
        <constructor-arg value="女"></constructor-arg>
    </bean>

    <!-- Namespaces勾选p(beans标签会增加内容xmlns:p="http://www.springframework.org/schema/p") -->
    <bean id="s4" class="com.atguigu.spring.di.Student" p:id="10033" p:name="赵六" p:age="26" p:sex="男"></bean>
</beans>

4.编写测试类

package com.atguigu.spring.di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans-di.xml");
        
        Student s1 = ac.getBean("s1", Student.class);
        Student s2 = ac.getBean("s2", Student.class);
        Student s3 = ac.getBean("s3", Student.class);
        Student s4 = ac.getBean("s4", Student.class);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }

}