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>
spring IOC 装配一个bean的更多相关文章
- Spring IOC容器创建bean过程浅析
1. 背景 Spring框架本身非常庞大,源码阅读可以从Spring IOC容器的实现开始一点点了解.然而即便是IOC容器,代码仍然是非常多,短时间内全部精读完并不现实 本文分析比较浅,而完整的IOC ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean
5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...
- Spring IoC 容器和 bean 对象
程序的耦合性: 耦合性(Coupling),又叫耦合度,是对模块间关联程度的度量.耦合的强弱取决于模块间接口的复杂性.调用模块的方式以及通过界面传送数据的多少.模块间的耦合度是指模块之间的依赖关系,包 ...
- Spring IoC介绍与Bean的使用
1. 介绍 IoC IoC-Inversion of Control,即"控制反转",它不是什么技术,而是一种设计思想.在 Java 开发中, IoC意味着将设计好的对象交给容 ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- Spring Ioc介绍和Bean的实例化
一.IoC:Inverse of Control 控制反转 // 依赖注入 Dependency Injection 控制:某一接口具体实现类的选择权 反转:从调用者中移除控制权,转交第三方 ...
随机推荐
- [JZOJ4687]奇袭
[JZOJ4687]奇袭 题目 由于各种原因,桐人现在被困在Under World(以下简称UW)中,而UW马上要迎来最终的压力测试——魔界入侵. 唯一一个神一般存在的Administrator被消灭 ...
- 利用rsync如何同步单个文件
rsync -vzrtopg --progress --include-from=/home/test/include.list --exclude=/* root@192.168.30.179::b ...
- nyoj_49_开心的小明_201403161133
开心的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天 ...
- 树 (p155, 从中序和后续回复二叉树)
递归求解, You are to determine the value of the leaf node in a given binary tree that is the terminal no ...
- js递归解决汉诺塔问题
汉诺塔是一个印度的古老传说.有三个圆柱,其中一个圆柱上放着若干圆盘,这些圆盘从上到下,直径递增,利用一个辅助圆柱,将原来柱子上的圆盘放到另一个柱子上,依旧是从上到下直径递增. 汉诺塔是一个经典的递归案 ...
- Maven: java.lang.ClassNotFoundException: org.eclipse.aether.spi.connector.Transfer$State
在mac中使用maven compile时发生以下错误: Maven: java.lang.ClassNotFoundException: org.eclipse.aether.spi.connect ...
- Android颜色透明度数值一览
100% — FF 95% — F2 90% — E6 85% — D9 80% — CC 75% — BF 70% — B3 65% — A6 60% — 99 55% — 8C 50% — 80 ...
- Java 两个整数相除保留两位小数,将小数转化为百分数
Java 两个整数相除保留两位小数,将小数转化为百分数 源于:http://blog.sina.com.cn/s/blog_624d755d0101cvuq.html 后来学习了:http://blo ...
- [Cypress] Test XHR Failure Conditions with Cypress
Testing your application’s behavior when an XHR call results in an error can be difficult. The use o ...
- XMPP基本内容简单介绍
即时通讯技术简单介绍 即时通讯技术(IM)支持用户在线实时交谈.假设要发送一条信息,用户须要打开一个小窗体,以便让用户及其朋友在当中输入信息并让交谈两方都看到交谈的内容.有很多的IM系统,如AOL I ...