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 控制:某一接口具体实现类的选择权 反转:从调用者中移除控制权,转交第三方 ...
随机推荐
- DSP、Media、AdExchanger之间的关系及交互流程
广告商,如以下的樱花日语,淘宝卖家.其须要推广自己的产品. Zampdsp(晶赞) 是DSP平台.其与非常多广告商合作,广告商在平台上公布广告创意,并托付平台代为投放. tanx.com 是adExc ...
- 【Android】ListView 优化
重用 ListView Item ListView创建时其会创建屏幕可容纳数量的 Item.ListView 滚动时,刚消失的 item 会被保存到回收池中.新出现的 item 从回收池中获取避免反复 ...
- 2014腾讯实习生笔试题——define与typedef
2014腾讯实习生笔试(广州站)第26题填空题: #define MAX_NUM 1000+1 int Temp = Max_NUM*10; 则Temp的值为( ) 答案是:1010, 由于宏定义仅仅 ...
- C++第11周(春)项目3 - 点类派生直线类
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 [项目3 - 点类派生直线类]定义点类Poin ...
- hdu4738Caocao's Bridges
什么?有人要炸我的桥?!D飞他(心疼周瑜大都督) 这个就是求割边/桥了. #include<cstdio> #include<iostream> #include<cst ...
- Codeforces--598A--Tricky Sum(数学)
Tricky Sum Tricky SumCrawling in process... Crawling failed Time Limit:1000MS Memory Limit:26 ...
- Codeforces--615B--Longtail Hedgehog(贪心模拟)
B. Longtail Hedgehog time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- bzoj 1191 [ HNOI 2006 ] 超级英雄Hero —— 二分图匹配
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1191 就是二分图匹配的裸题: 注意题目要求是第一次匹配失败就退出!没仔细看题差点丢失1A. ...
- Iframe 用法的详细讲解
1转自:https://blog.csdn.net/judyge/article/details/51786064 zIframe 用法的详细讲解 把iframe解释成“浏览器中的浏览器“很是恰当 & ...
- z-index 、层叠上下文、层叠级别、z-index失效
一.z-index z-index默认处于非激活状态,只有定位元素(即position:relative/absolute/fixed时)才会被激活. z-index与层叠上下文关联. 当z-inde ...