spring IOC 装配一个bean

1.0属性注入

新建一个people类

package com.java.test3;

/**
 * @author nidegui
 * @create 2019-06-22 14:45
 */
public class People {
    private Integer id;
    private String name;
    private String age;

    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 String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "People{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}

  

装配在bean里面

<?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.java.test.Helloworld"></bean>

  	<bean id="people" class="com.java.test3.People"></bean>

	<!--属性注入-->
	<bean id="people2" class="com.java.test3.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
	</bean>
</beans>

  

测试:

package com.java.test3;

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

/**
 * @author nidegui
 * @create 2019-06-22 14:47
 */
public class Test {
    public static void main(String[] args) {
        /*属性注入*/
       ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        People people =(People) ac.getBean("people2");
        System.out.println(people);
    }
}

  

 

2.0构造函数注入

1.0通过类型注入

在实体中添加构造方法

 

    public People(Integer id, String name, String age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

bean.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="people3" class="com.java.test3.People">
		<constructor-arg type="java.lang.Integer" value="1"></constructor-arg>
		<constructor-arg type="java.lang.String" value="nideg"></constructor-arg>
		<constructor-arg type="java.lang.String" value="15"></constructor-arg>
	</bean>

	<!--属性注入-->
	<!--<bean id="people2" class="com.java.test3.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
	</bean>-->
</beans>

  

3.0按照索引注入

<?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="people3" class="com.java.test3.People">
		<constructor-arg type="java.lang.Integer" value="1"></constructor-arg>
		<constructor-arg type="java.lang.String" value="nideg"></constructor-arg>
		<constructor-arg type="java.lang.String" value="15"></constructor-arg>
	</bean>

	<!--索引注入,按照构造方法的顺序注入-->
	<bean id="people4" class="com.java.test3.People">
		<constructor-arg index="0" value="1"></constructor-arg>
		<constructor-arg index="1" value="nideg"></constructor-arg>
		<constructor-arg index="2" value="15"></constructor-arg>
	</bean>

	<!--属性注入-->
	<!--<bean id="people2" class="com.java.test3.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
	</bean>-->
</beans>

  

 

3.0工厂方法注入

 创建一个工厂

package com.java.test3;

/**
 * @author nidegui
 * @create 2019-06-22 15:15
 */
public class Factory {
    /**
     * 定义一个非静态工厂
     * @return
     */
    public People createFactoty(){
        People p=new People();
        p.setId(1);
        p.setName("ni");
        p.setAge("25");
        return p;
    }
}

  

注入bena.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="people3" class="com.java.test3.People">
		<constructor-arg type="java.lang.Integer" value="1"></constructor-arg>
		<constructor-arg type="java.lang.String" value="nideg"></constructor-arg>
		<constructor-arg type="java.lang.String" value="15"></constructor-arg>
	</bean>

	<!--索引注入,按照构造方法的顺序注入-->
	<bean id="people4" class="com.java.test3.People">
		<constructor-arg index="0" value="1"></constructor-arg>
		<constructor-arg index="1" value="nideg"></constructor-arg>
		<constructor-arg index="2" value="15"></constructor-arg>
	</bean>

	<!--属性注入-->
	<!--<bean id="people2" class="com.java.test3.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
	</bean>-->

	<!--工厂模式注入-->
	<bean id="peopeFactoty" class="com.java.test3.Factory"></bean>
	<bean id="people5" factory-bean="peopeFactoty" factory-method="createFactoty"></bean>
</beans>

  

工厂模式的静态方法注入

 public static People createFactoty2(){
        People p=new People();
        p.setId(1);
        p.setName("ni");
        p.setAge("25");
        return p;
    }

  

 

<?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="people3" class="com.java.test3.People">
		<constructor-arg type="java.lang.Integer" value="1"></constructor-arg>
		<constructor-arg type="java.lang.String" value="nideg"></constructor-arg>
		<constructor-arg type="java.lang.String" value="15"></constructor-arg>
	</bean>

	<!--索引注入,按照构造方法的顺序注入-->
	<bean id="people4" class="com.java.test3.People">
		<constructor-arg index="0" value="1"></constructor-arg>
		<constructor-arg index="1" value="nideg"></constructor-arg>
		<constructor-arg index="2" value="15"></constructor-arg>
	</bean>

	<!--属性注入-->
	<!--<bean id="people2" class="com.java.test3.People">
		<property name="name" value="nidegui"></property>
		<property name="id" value="1"></property>
		<property name="age" value="12"></property>
	</bean>-->

	<!--工厂模式注入-->
	<bean id="peopeFactoty" class="com.java.test3.Factory"></bean>
	<bean id="people5" factory-bean="peopeFactoty" factory-method="createFactoty"></bean>

	           <!--静态方法注入-->
	<bean id ="people6" class="com.java.test3.Factory" factory-method="createFactoty2"></bean>
</beans>

  

 

posted on 2019-06-22 14:55  nidegui  阅读(109)  评论(0编辑  收藏  举报