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 控制:某一接口具体实现类的选择权 反转:从调用者中移除控制权,转交第三方 ...
随机推荐
- [Poj2112][USACO2003 US OPEN] Optimal Milking [网络流,最大流][Dinic+当前弧优化]
题意:有K个挤奶机编号1~K,有C只奶牛编号(K+1)~(C+K),每个挤奶机之多能挤M头牛,现在让奶牛走到挤奶机处,求奶牛所走的最长的一条边至少是多少. 题解:从起点向挤奶机连边,容量为M,从挤奶机 ...
- 【ACM】hdu_zs3_1007_Rails_201308100802
Rails Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Total Submissi ...
- 利用Date类计算生活时间
今天学习到了Date类还有其他一些常用类! 这里就简单使用Date及其一些方法计算生活时间. import java.text.ParseException; import java.text.Sim ...
- PDF在线预览-pdfjs使用
请参考我的开源: https://github.com/wuyechun2018/itools/blob/master/src/main/webapp/WEB-INF/views/pdf/index. ...
- Linux下统计某个目录的文件个数(转)
1.统计某文件夹下文件个数,不包括子文件夹 比如:统计/home下.jpeg文件的个数 ls -l "/home" | grep ".jpeg" | wc -l ...
- RDS for MySQL Mysqldump 常见问题和处理
https://help.aliyun.com/knowledge_detail/41732.html?spm=5176.7841698.2.13.u67H3h
- [数据结构与算法]排序算法(Python)
1.直接插入排序 给定一个数组后,从第二个元素开始,如果比第一个小,就跟他交换位置,否则不动:第三个元素如果比第二个小,把第三个跟第二个交换位置,在把第二个与第一个比较:..... def inser ...
- 软件测试之怎么避免Bug漏测?
一.对需求评审阶段,对业务需求细节理解不明确,未深入挖掘隐含拓展需求 改进措施 需求评审前,我们应该先仔细阅读prd及交互文档,先形成自己对产品的思考,通过脑图的方式列出对产品设计的疑问点,从用户或者 ...
- [洛谷0925]NOIP模拟赛 个人公开赛 OI
P3395 路障 题目背景 此题约为NOIP提高组Day1T1难度. 题目描述 B君站在一个n*n的棋盘上.最开始,B君站在(1,1)这个点,他要走到(n,n)这个点. B君每秒可以向上下左右的某个 ...
- B1391 [Ceoi2008]order 最大权闭合图 最小割
啊啊啊,假的题吧!!!我用的当前弧优化T了6个点,其他人不用优化AC!!!震惊!!!当前弧优化是假的吧!!! 到现在我也没调出来...大家帮我看看为啥70.... 来讲一下这个题的思路,就是设一个源点 ...