Spring-依赖注入-多参数的构造函数注入方式
2014-09-26 13:46
477 查看
创建项目
项目名称:spring092601
2.引入spring jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
3.添加配置文件
在conf下添加spring的核心配置文件applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
4.创建业务bean
在src目录下创建
包名:cn.jbit.spring092601.domain
package cn.jbit.spring092601.domain;
import java.io.Serializable;
/**
* 学校
* @author Administrator
*
*/
public class School implements Serializable {
private Integer id;//学校编号
private String name;//学校名称
public School() {
super();
}
public School(String name) {
super();
this.name = name;
}
public School(Integer id,String name) {
super();
this.id = id;
this.name = name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
5.在配置文件中编写bean的配置,并注入属性值,也就是我们的DI(Dependency Injection)依赖注入
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--
构造函数注入
-->
<bean id="school" class="cn.jbit.spring092601.domain.School">
<!-- index:表示构造函数中参数的索引 -->
<constructor-arg index="0">
<value>1</value>
</constructor-arg>
<constructor-arg index="1">
<value>lisi</value>
</constructor-arg>
</bean>
</beans>
6.测试bean的属性注入
在test目录下测试
包名:cn.jbit.spring092601.domain
/**
* 构造函数注入
*/
@Test
public void testConstr(){
/*
*
*/
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
School school = (School) beanFactory.getBean("school");
System.out.println(school.getName()+school.getId());
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1558488
项目名称:spring092601
2.引入spring jar包
commons-logging.jar
junit-4.4.jar
log4j.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
3.添加配置文件
在conf下添加spring的核心配置文件applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>
4.创建业务bean
在src目录下创建
包名:cn.jbit.spring092601.domain
package cn.jbit.spring092601.domain;
import java.io.Serializable;
/**
* 学校
* @author Administrator
*
*/
public class School implements Serializable {
private Integer id;//学校编号
private String name;//学校名称
public School() {
super();
}
public School(String name) {
super();
this.name = name;
}
public School(Integer id,String name) {
super();
this.id = id;
this.name = name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
5.在配置文件中编写bean的配置,并注入属性值,也就是我们的DI(Dependency Injection)依赖注入
配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!--
构造函数注入
-->
<bean id="school" class="cn.jbit.spring092601.domain.School">
<!-- index:表示构造函数中参数的索引 -->
<constructor-arg index="0">
<value>1</value>
</constructor-arg>
<constructor-arg index="1">
<value>lisi</value>
</constructor-arg>
</bean>
</beans>
6.测试bean的属性注入
在test目录下测试
包名:cn.jbit.spring092601.domain
/**
* 构造函数注入
*/
@Test
public void testConstr(){
/*
*
*/
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
School school = (School) beanFactory.getBean("school");
System.out.println(school.getName()+school.getId());
}
本文出自 “素颜” 博客,请务必保留此出处http://suyanzhu.blog.51cto.com/8050189/1558488
相关文章推荐
- Spring-依赖注入-构造函数注入方式
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- Spring笔记之一:依赖注入的方式属性注入和构造函数注入
- spring 依赖注入之设值注入方式
- Spring Ioc 之二 -依赖注入的几种方式
- spring 依赖注入方式总结详解
- (4) 编码剖析Spring装配基本属性的原理【附加:注入依赖对象的两种方式】
- (4) 利用 Setter方式实现 【第一种方式】 依赖注入,编码剖析Spring依赖注入的原理
- Spring依赖注入的两种方式比对
- (5) 编码剖析Spring装配基本属性的原理【附加:注入依赖对象的两种方式】
- spring依赖注入的方式
- Spring Ioc-依赖注入的几种方式
- spring依赖注入方式
- spring 依赖对象的注入方式(3种) 和 公共属性的设置abstract="true" parent
- 使用Spring2.0的annotation方式进行依赖注入检查
- Spring依赖注入方式
- spring-依赖注入的方式
- Spring Ioc-依赖注入的几种方式
- (6) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- spring入门(依赖注入方式,生命周期,读取属性文件值,数据库接连)