spring bean 中构造函数
来源:互联网 发布:淘宝空包代发哪个好 编辑:程序博客网 时间:2024/06/08 01:32
可以在配制文件中进行实例化,但有时更希望可以在对象实例化时通过构造方法实例化。
Bean中增加一个构造方法。那么就要在配置文件中增加一个参数constrator-org,
例如:
//此方法中有一个构造函数,
package spring02;
public class SimpleBean {
private String name ;
private String password ;
public SimpleBean(String name,String password)
{
this.setName(name) ;
this.setPassword(password) ;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
==========================================
//上面用到了构造函数,这里就要用<constructor-arg index="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-2.0.xsd">
<bean id="simple" class="spring02.SimpleBean">
<constructor-arg index="0">
<value>朱庆良</value>
</constructor-arg>
<constructor-arg index="1">
<value>123456</value>
</constructor-arg>
</bean>
</beans>
===============================================
//测试
package spring02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = null ;
context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
SimpleBean simple = (SimpleBean)context.getBean("simple") ;
System.out.println("姓名:"+simple.getName()) ;
System.out.println("密码:"+simple.getPassword()) ;
}
}
- spring bean 中构造函数
- spring bean 的配置-不同构造函数
- spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
- Spring-bean构造方法
- spring中构造函数注入
- InvocationTargetException(Bean没有注入) 和 spring初始化构造函数
- Spring Bean xml显式装配之构造函数
- spring中构造bean实例的三种方法
- spring在构造函数中注入对象
- spring bean中引用bean
- Spring-Bean的构造函数和Setter执行顺序以及InitializingBean
- FAQ(8):spring:遗漏编写bean的无参构造函数
- Spring中装配bean
- spring中bean生命周期
- Spring中bean定义
- Spring中bean生命周期
- spring 中 bean 生命周期
- Spring bean属性注入(构造器)
- Linux JDK 安装 环境变量配置
- springIoC
- 关于条码自动更的解决办法
- spring对象数组的注入
- Win XP中通过安全策略关闭端口
- spring bean 中构造函数
- 我的漫漫程序人生路(下)
- spring
- SmartUpload上传
- Solaris最基本的命令
- MASM32编程演示程序在窗口模式和全屏模式间切换
- 用一个HelloWorld例子手把手教会你使用FLEX BlazeDS
- session对象
- servlet