Spring入门实例-bean初始化
程序员文章站
2022-05-21 22:14:27
...
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());