您的位置:首页 > 编程语言 > Java开发

初学Spring-XML文件配置Bean的一些知识点

2016-09-19 17:53 441 查看
<?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.xsd"> 
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions go here -->
</beans>
以上代码摘抄自官方文档。

1.构造注入,setter注入
(1)setter注入,个人代码示例
<bean id="card1" class="beans.Card">
<property name="id" value="1"></property>
<property name="number"><null/></property>
</bean>

(2)构造注入,个人代码示例

<!--  
    依赖构造注入可以指定index 和type类型,让属性自动匹配,他们可以混合使用   
-->
<bean id="animal" class="beans.Animal">
<constructor-arg value="TOM" index="0"></constructor-arg>
<constructor-arg value="1000" type="double"></constructor-arg>
<constructor-arg value="cat" index="2"></constructor-arg>
</bean>

(3)内部bean
<bean id="worker" class="beans.Worker">
<property name="name" value="sam"></property>
<property name="card" >
<!-- 内部bean 只能被自己引用 -->
 
<bean id="card1" class="beans.Card">
<property name="id" value="1"></property>
<property name="number"><null/></property>
</bean>
</property>
</bean>

(4)集合属性配置
<bean id="classroom" class="beansUtil.ClassRoom">
<property name="id" value="1"></property>
<property name="key" value="hello java"></property>
<property name="students">
<list>
<ref bean="student1"/>
<ref bean="student2"/>
</list>
</property>
</bean>

<bean id ="teacher" class="beansUtil.Teacher">
<property name="name" value="sam"></property>
<property name="map">
<map>
<entry key="1" value-ref="student1"></entry>
</map>
</property>
</bean>

<bean id="pro1" class="beansUtil.pro">
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123</prop>
</props>
</property>
</bean>

(5)初识scope属性
<bean id="test1" class="beans.autowire.testScope" scope="prototype">
<property name="id" value="1"></property>
</bean>

常用有:prototype singleton(单例,缺省值)

可以使用一个demo来认识scope属性
package beans.autowire;

public class testScope {
private Integer id;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

@Override
public String toString() {
return "testScope [id=" + id + "]";
}
public testScope(){
System.out.println("測試scope");
}
}

package beans.autowire;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class util {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");
System.out.println(applicationContext);
Student student = (Student)applicationContext.getBean("student");
System.out.println(student);

ClassRoom classRoom = (ClassRoom)applicationContext.getBean("classroom");
System.out.println(classRoom);
/*
* 如果scope设置为singleton的话是单例模式,那么当applicationContext对象被初始化时,则会自动初始化bean
* 如果选择prototype则是需要就new
*/
testScope scope1 = (testScope) applicationContext.getBean("test1");
testScope scope2= (testScope) applicationContext.getBean("test1");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring