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 控制:某一接口具体实现类的选择权 反转:从调用者中移除控制权,转交第三方 ...
随机推荐
- [转]数据库查询 sysobjects
sysobjects sysobjects是系统自建的表,里面存储了在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等),各在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占 ...
- hibernate 普通字段延迟载入无效的解决的方法
关联对象的延迟载入就不说了.大家都知道. 关于普通字段的延迟载入,尤其是lob字段,若没有延迟载入,对性能影响极大.然而简单的使用 @Basic(fetch = FetchType.LAZY) 注解并 ...
- 卡尔曼滤波(Kalman Filter) 的进一步讨论
我们在上一篇文章中通过一个简单的样例算是入门卡尔曼滤波了.本文将以此为基础讨论一些技术细节. 卡尔曼滤波(Kalman Filter) http://blog.csdn.net/baimafujinj ...
- BeanUtils使用案例
1.BeanUtils框架/工具(APACHE开源组织开发) (1)BeanUtils框架可以完毕内省的一切功能.并且优化 (2)BeanUtils框架可以对String<-> ...
- mysql20170404代码实现
CREATE DATABASE IF NOT EXISTS school; USE school; CREATE TABLE tblStudent( StuId ) NOT NULL PRIMARY ...
- oc60--Category 分类 练习
// main.m // Category练习 #import <Foundation/Foundation.h> #import "NSString+NJ.h" // ...
- vim copy termi
用vim写代码时,经常遇到这样的场景,复制多行,然后粘贴. 这样做:1. 将光标移动到要复制的文本开始的地方,按v进入可视模式.2. 将光标移动到要复制的文本的结束的地方,按y复制.此时vim会自动将 ...
- 学习笔记二十三——字符函数库cctype【转】
本文转载自: 字符函数库cctype 在头文件cctype(ctype.h)中定义了一些函数原型,可以简化输入确定字符是否为大写字母.数字.标点符号等工作. 例如: 如果ch是一个字母,则isalph ...
- [POJ 3565] Ant
[题目链接] http://poj.org/problem?id=3565 [算法] KM算法求最小匹配 [代码] #include <algorithm> #include <bi ...
- K-means algorithm----PRML读书笔记
The K-means algorithm is based on the use of squared Euclidean distance as the measure of dissimila ...