峰Spring4学习依赖注入的几种方式

Posted 有点懒惰的大青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了峰Spring4学习依赖注入的几种方式相关的知识,希望对你有一定的参考价值。

一、装配一个bean

二、依赖注入的几种方式

com.cy.entity   People.java:

package com.cy.entity;

public class People {
    private int id;
    private String name;
    private int age;
    
    public People() {
        super();
        // TODO Auto-generated constructor stub
    }
    public People(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = 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 + "]";
    }
    
    
}
View Code

spring管理bean的xml: 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="helloWorld" class="com.cy.test.HelloWorld"></bean>

      <!-- 装配一个bean -->
      <bean id="people" class="com.cy.entity.People"></bean>
      
      <!-- 依赖注入 -->
      <!-- 属性注入 -->
     <bean id="people2" class="com.cy.entity.People">
         <property name="id" value="1"></property>
         <property name="name" value="张三"></property>
         <property name="age" value="11"></property>
     </bean>
     
     <!-- 构造函数注入 通过类型-->
     <bean id="people3" class="com.cy.entity.People">
         <constructor-arg type="int" value="2"></constructor-arg>
         <constructor-arg type="String" value="李四"></constructor-arg>
         <constructor-arg type="int" value="22"></constructor-arg>
     </bean>
     <!-- 构造函数注入 通过索引-->
     <bean id="people4" class="com.cy.entity.People">
        <constructor-arg index="0" value="3"></constructor-arg>
        <constructor-arg index="1" value="王五"></constructor-arg>
        <constructor-arg index="2" value="55"></constructor-arg>
    </bean>
    <!-- 构造函数注入 类型 索引联合使用-->
    <bean id="people5" class="com.cy.entity.People">
        <constructor-arg index="0" type="int" value="4"></constructor-arg>
        <constructor-arg index="1" type="String" value="招六"></constructor-arg>
        <constructor-arg index="2" type="int" value="66"></constructor-arg>
    </bean>
    
    <!-- 工厂方法注入  非静态工厂 -->
    <bean id="peopleFactory" class="com.cy.factory.PeopleFactory"></bean>
    <bean id="people6" factory-bean="peopleFactory" factory-method="createPeople"></bean>
    
    <!-- 工厂方法注入  静态工厂 -->
    <bean id="people7" class="com.cy.factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>

com.cy.factory下的

静态工厂:PeopleFactory2.java:

package com.cy.factory;
import com.cy.entity.People;


public class PeopleFactory2 {
    public static People createPeople(){
        People people = new People();
        people.setId(6);
        people.setName("小八");
        people.setAge(88);
        return people;
    }
}
View Code

非静态工厂:PeopleFactory.java:

package com.cy.factory;
import com.cy.entity.People;


public class PeopleFactory {
    public People createPeople(){
        People people = new People();
        people.setId(5);
        people.setName("小七");
        people.setAge(77);
        return people;
    }
}
View Code

 

测试代码:

package com.cy.service;

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

import com.cy.entity.People;

public class Test {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext ac =new ClassPathXmlApplicationContext("beans.xml");
        People people = (People) ac.getBean("people");
        System.out.println(people);
        
        //属性注入
        People people2 = (People) ac.getBean("people2");
        System.out.println(people2);
        
        //构造函数注入  通过类型
        People people3 = (People) ac.getBean("people3");
        System.out.println(people3);
        
        //工厂方法注入 非静态工厂
        People people6 = (People) ac.getBean("people6");
        System.out.println(people6);
        
        //工厂方法注入 静态工厂
        People people7 = (People) ac.getBean("people7");
        System.out.println(people7);
        
    }
}
View Code

console打印:

以上是关于峰Spring4学习依赖注入的几种方式的主要内容,如果未能解决你的问题,请参考以下文章

依赖注入之针对不同类型变量的几种注入方式

spring笔记--依赖注入之针对不同类型变量的几种注入方式

Spring Ioc-依赖注入的几种方式

Spring的几种注入bean的方式

spring注入bean的几种方式

Castle Windsor 学习-----Installer的几种安装方式