当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

Spring入门实例-bean初始化

发表于: 2012-07-26   作者:blackproof   来源:转载   浏览次数:
摘要: Spring入门实例 bean的初始化   实例: bean类:   public static DatabaseXMLBean createInstanceother() { DatabaseXMLBean bean = new DatabaseXMLBean(); bean.setName("emp"); bean.se

Spring入门实例 bean的初始化

 

实例:

bean类:

 

	public static DatabaseXMLBean createInstanceother()
	{
		DatabaseXMLBean bean = new DatabaseXMLBean();
		bean.setName("emp");
		bean.setLength(10);
		return bean;
	}
	private String name;
	public String getName()
	{

		System.out.println("!!!!!!!!!!!!!!!!!!!!DataBaseXML,getname");
		return name;
	}
	public void setName(String name)
	{
		System.out.println("!!!!!!!!!!!!!!!!!!!!DataBaseXML,setname");
		this.name = name;
	}
	public String getType()
	{
		System.out.println("!!!!!!!!!!!!!!!!!!!!DataBaseXML,gettype");
		return type;
	}
	public void setType(String type)
	{
		System.out.println("!!!!!!!!!!!!!!!!!!!!DataBaseXML,settype");
		this.type = type;
	}
	public int getLength()
	{
		System.out.println("!!!!!!!!!!!!!!!!!!!!DataBaseXML,getlength");
		return length;
	}
	public void setLength(int length)
	{
		System.out.println("!!!!!!!!!!!!!!!!!!!!DataBaseXML,setlength");
		this.length = length;
	}
	public String getJdbcName()
	{
		return jdbcName;
	}
	public void setJdbcName(String jdbcName)
	{
		this.jdbcName = jdbcName;
	}
	public String getJdbcType()
	{
		return jdbcType;
	}
	public void setJdbcType(String jdbcType)
	{
		this.jdbcType = jdbcType;
	}
	public boolean isPrimaryKey()
	{
		return primaryKey;
	}
	public void setPrimaryKey(boolean primaryKey)
	{
		this.primaryKey = primaryKey;
	}
	public boolean isVisible()
	{
		return visible;
	}
	public void setVisible(boolean visible)
	{
		this.visible = visible;
	}
	private String type;
	private int length;
	private String jdbcName;
	private String jdbcType;
	private boolean primaryKey;
	private boolean visible;

	@Override
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("type:").append(type).append("\n");
		sb.append("length:").append(length).append("\n");
		sb.append("jdbcName:").append(jdbcName).append("\n");
		sb.append("jdbcType:").append(jdbcType).append("\n");
		sb.append("primaryKey:").append(primaryKey).append("\n");
		sb.append("visible:").append(visible);
		return sb.toString();
	}
	
	public void init() throws Exception{
		System.out.println("DataBaseXML,init方法初始化");
	}
	
	public DatabaseXMLBean()
	{
		System.out.println("DataBaseXML构造函数");
	}

 

 配置文件:

 

<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.5.xsd">

	<bean name="column2" class="com.myspring.MyJavaBean" init-method="init">
		<property name="name" value="dirk.zhang"></property>
	</bean>
	
	<bean name="column3" class="com.myspring.DatabaseXMLBean" init-method="init">
		<property name="type" value="String"></property>
		<property name="length" value="0"></property>
	</bean>
	
	<bean name="column4" class="com.myspring.MytestSpring" init-method="init">
	</bean>

</beans>

 

 测试方法:

 

System.out.println("加载Spring配置文件");
//		Resource res = new ClassPathResource("com/myspring/demo-bean.xml");//spring url require the src as initsource
//		BeanFactory factory = new XmlBeanFactory(res);
		ApplicationContext context = new ClassPathXmlApplicationContext("com/myspring/DatabaseXMLBean.xml");
		BeanFactory xmlbf = context;//ApplicationContext可以转换为Beanfactory
		System.out.println("加载spring配置文件结束");
		MyJavaBean bean = (MyJavaBean) context.getBean("column2");
		System.out.println("获取属性name:"+bean.getName());
 

 

 

 

 

Spring入门实例-bean初始化

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
可参考文章: Spring Bean 生命周期 Spring容器-Bean的生命周期 Spring开闭原则的表现-BeanPostProc
可参考文章: Spring Bean 生命周期 Spring容器-Bean的生命周期 Spring开闭原则的表现-BeanPostProc
可参考文章: Spring Bean 生命周期 Spring容器-Bean的生命周期 Spring开闭原则的表现-BeanPostProc
1.搭建与测试Spring的开发环境 PS: Spring-beans-2.5.xsd (为使在Beans.xml操作顺畅) Perferences--
Spring对开闭原则的体现,提供了大量的回调接口,例如BeanPostProcess,该接口的作用是我们可以添加自
一、首先我们来看ClassPathXmlApplicationContext类体系结构 从该继承体系可以看出: 1. BeanFactor
作者:zuoxiaolong8810(左潇龙),转载请注明出处。 最近工作很忙,时间不多,研究spring的进度被
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客
原文出自:http://www.cnblogs.com/liunanjava/p/4401089.html 一、Aware相关接口 对于应用程序来说
一、Aware相关接口 对于应用程序来说,应该尽量减少对Sping Api的耦合程度,然而有些时候为了运用Sp
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号