首页 > 代码库 > Spring 依赖注入(一、注入方式)

Spring 依赖注入(一、注入方式)

首先装配一个实体类People

package com.maya.model;

public class People {
    private int id;
    private String name;
    private int age;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    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;
    }
    
    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    public People(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public People() {
        super();
    }
}

配置beans.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 属性注入 -->
    <!-- 通过属性名 -->
    <bean id="people1" class="com.maya.model.People">
        <property name="id" value="http://www.mamicode.com/1"></property>
        <property name="name" value="http://www.mamicode.com/小明"></property>
        <property name="age" value="http://www.mamicode.com/22"></property>
    </bean>
    
    <!-- 通过构造方法:可以通过属性名、索引、属性类型-->
    <!-- 通过属性名 -->
    <bean id="people2" class="com.maya.model.People">
        <constructor-arg name="id" value="http://www.mamicode.com/2"></constructor-arg>
        <constructor-arg name="name" value="http://www.mamicode.com/小明2"></constructor-arg>
        <constructor-arg name="age" value="http://www.mamicode.com/12"></constructor-arg>
    </bean>
    
    <!-- 通过索引 -->
    <bean id="people3" class="com.maya.model.People">
        <constructor-arg index="0" value="http://www.mamicode.com/2"></constructor-arg>
        <constructor-arg index="1" value="http://www.mamicode.com/小明2"></constructor-arg>
        <constructor-arg index="2" value="http://www.mamicode.com/12"></constructor-arg>
    </bean>
    
    <!-- 通过属性类型 -->
    <bean id="people4" class="com.maya.model.People">
        <constructor-arg type="int" value="http://www.mamicode.com/2"></constructor-arg>
        <constructor-arg type="String" value="http://www.mamicode.com/小明2"></constructor-arg>
        <constructor-arg type="int" value="http://www.mamicode.com/12"></constructor-arg>
    </bean>
  
  
</beans>

读取配置文件

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");//读取配置文件
        People people1=(People)ac.getBean("people1");
        System.out.println(people1);
        
        People people2=(People)ac.getBean("people2");
        System.out.println(people2);
        People people3=(People)ac.getBean("people3");
        System.out.println(people3);
        People people4=(People)ac.getBean("people4");
        System.out.println(people4);
    }
}

 注入静态类与非静态类

public class Factory {
    public People createPeople(){
        People p=new People();
        p.setId(5);
        p.setName("小5");
        p.setAge(55);
        return p;
    }
//静态
public class StaticFactory {
    public static People createPeople(){
        People p=new People();
        p.setId(6);
        p.setName("小6");
        p.setAge(66);
        return p;
    }
<!-- 通过非静态工厂 -->
      <bean id="peopleFactory" class="com.maya.factory.Factory"></bean>    
    <bean id="people5" factory-bean="peopleFactory" factory-method="createPeople"></bean>
      
      <!-- 通过静态工厂 -->
      <bean id="people6" class="com.maya.factory.StaticFactory" factory-method="createPeople"></bean>    
     People people5=(People)ac.getBean("people5");
        System.out.println(people5);
        
        People people6=(People)ac.getBean("people6");
        System.out.println(people6);

 

Spring 依赖注入(一、注入方式)