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>

  

spring IOC 装配一个bean的更多相关文章

  1. Spring IOC容器创建bean过程浅析

    1. 背景 Spring框架本身非常庞大,源码阅读可以从Spring IOC容器的实现开始一点点了解.然而即便是IOC容器,代码仍然是非常多,短时间内全部精读完并不现实 本文分析比较浅,而完整的IOC ...

  2. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  3. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  4. Spring IoC 容器和 bean 对象

    程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...

  5. Spring IoC介绍与Bean的使用

    1. 介绍 IoC   IoC-Inversion of Control,即"控制反转",它不是什么技术,而是一种设计思想.在 Java 开发中, IoC意味着将设计好的对象交给容 ...

  6. Spring IOC容器中Bean的生命周期

    1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...

  7. spring IOC 容器中 Bean 的生命周期

    IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...

  8. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  9. Spring Ioc介绍和Bean的实例化

    一.IoC:Inverse of Control 控制反转   //  依赖注入  Dependency Injection 控制:某一接口具体实现类的选择权 反转:从调用者中移除控制权,转交第三方 ...

随机推荐

  1. 温故之--Linux 初始化 init 系统

    参选URL: http://www.ibm.com/developerworks/cn/linux/1407_liuming_init1/index.html 本系列一共三篇,看完记住,那水平就不一样 ...

  2. Spring MVC的@RequestMapping多个URL映射到同一个方法

    @RequestMapping可以是一个URL对应一个方法,也可以多个URL对应同一个方法,写法如下: @RequestMapping(value={"url","res ...

  3. PHP array_key_exists()

    定义和用法 array_key_exists() 函数判断某个数组中是否存在指定的 key,如果该 key 存在,则返回 true,否则返回 false. 语法 array_key_exists(ke ...

  4. PHP array_intersect_assoc()

    定义和用法 array_intersect_assoc() 函数返回两个或多个数组的交集数组. 与 array_intersect() 函数 不同的是,本函数除了比较键值,还比较键名.返回的数组中元素 ...

  5. [Angular] Remove divs to Preserve Style and Layout with ng-container in Angular

    The Angular <ng-container> is a grouping element that doesn't interfere with styles or layout ...

  6. C语言之文件操作07——读取文件数据并计算均值方差标准差

    //文件 /* =============================================================== 题目:从文本文件"high.txt" ...

  7. Swift基本常识点

    import Foundation // 单行注释 // 多行注释(支持嵌套,OC是不支持的) // 常量let,初始化之后就不可改变. // 常量的具体类型可以自动识别,等号后面是什么类型,它就是什 ...

  8. vijos - P1279Leave-绿光(数学归纳法 + python)

    P1279Leave-绿光 Accepted 标签:[显示标签] 背景 期待这一份幸运,和一份冲劲,多么奇异的际遇--. 燕姿在演唱完绿光这首歌后,出给了姿迷一个考题. 北欧有一个传说! 人一生中能看 ...

  9. python 002 文件输入输出

    python 文件对象不仅可以访问普通磁盘文件,也可以访问抽象层面上的文件对象(例如URL地址) 打开文件open() file()功能一致可以任意替代 fp = open('/etc/test.tx ...

  10. AFNetworking 3.0携带參数上传文件Demo

    一.服务端代码: 服务端是java用国产nutz搞的,实际mvc框架都大同小异.就是提交文件的同一时候还带了个表单參数 @AdaptBy(type=UploadAdaptor.class, args= ...