【spring教程之二】spring注入xml中带参数的构造函数
2015-03-16 20:35
330 查看
1、续上文,如果想在注入bean文件的时候,传入参数到构造函数中。主要需要修改的就是spring.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="do" class="com.test.pro.Student">
<constructor-arg value="19"></constructor-arg>
</bean>
</beans>
2、我们把类修改成了学生类,Student.java,其中没有默认的构造函数,而是一个新的带参数的构造。
package com.test.pro;
public class Student {
private int age;
public Student(int age)
{
this.age=age;
}
public void speaking()
{
System.out.println("我的年龄是:"+age);
}
}
3、输出结果
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="do" class="com.test.pro.Student">
<constructor-arg value="19"></constructor-arg>
</bean>
</beans>
2、我们把类修改成了学生类,Student.java,其中没有默认的构造函数,而是一个新的带参数的构造。
package com.test.pro;
public class Student {
private int age;
public Student(int age)
{
this.age=age;
}
public void speaking()
{
System.out.println("我的年龄是:"+age);
}
}
3、输出结果
相关文章推荐
- 【spring教程之二】spring注射剂xml构造方法参数
- spring笔记之二:控制反转注入参数详解
- Spring基础之二:xml配置注入
- Spring-依赖注入-多参数的构造函数注入方式
- Spring一个类包含多个构造函数且带的参数相同所存在的注入类型问题
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
- spring 依赖性注入 (spring 依赖性注入(xml配置文件,填写的时候要写全,不是包,而是到类)
- Spring.net 使用构造函数注入时必须要有默认构造函数
- 传智播客Spring2.5视频教程_编码剖析Spring依赖注入的原理 2
- 传智播客Spring2.5视频教程_编码剖析Spring依赖注入的原理 3
- Spring源码学习-容器初始化之FileSystemXmlApplicationContext(一)构造函数 推荐
- Spring 构造函数注入
- Spring Ioc 之二 -依赖注入的几种方式
- XML简易教程之二
- Spring+Hibernate:在applicationContext.xml中配置C3P0参数说明
- Spring注入与无参构造函数自定义构造函数
- Spring内核研究-set方法注入和构造函数注入
- 传智播客Spring2.5视频教程_编码剖析Spring依赖注入的原理 1
- spring注入class类型参数
- 传智播客 Spring学习 xml配置依赖注入