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

<?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"); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值