spring 常用属性注入和自动装配

bean1的简单属性:

  

<span style="font-family:SimHei;font-size:18px;">package com.sxt.domain;
import java.util.*;
public class Bean1 {
private String name;
private int age;
private String[]attr;
private List<String>lists;
private Set<String>sets;
private Map<String,String>maps;
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String[] getAttr() {
	return attr;
}
public void setAttr(String[] attr) {
	this.attr = attr;
}
public List<String> getLists() {
	return lists;
}
public void setLists(List<String> lists) {
	this.lists = lists;
}
public Set<String> getSets() {
	return sets;
}
public void setSets(Set<String> sets) {
	this.sets = sets;
}
public Map<String, String> getMaps() {
	return maps;
}
public void setMaps(Map<String, String> maps) {
	this.maps = maps;
}
}
</span>


配置文件:


<span style="font-family:SimHei;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  
    <bean id="bean1" class="com.sxt.domain.Bean1">
          <property name="name" value="wangli"></property>
          <property name="age" value="23"></property>
          <property name="attr">
            <list>
                     <value>attr1</value>
                     <value>attr2</value>
            </list>
          </property>
          
           <property name="lists">
            <list>
                     <value>list1</value>
                     <value>list2</value>
            </list>
          </property>
          
           <property name="sets">
            <set>
                     <value>set1</value>
                     <value>set2</value>
            </set>
          </property>
          
           <property name="maps">
             <map>
                   <entry key="key1" value="map1"></entry>
                   <entry key="key2" value="map1"></entry>
             </map>
           
           </property>
          
    </bean>
   
</beans>
</span>


2  spring的singleton和prototype的作用域

    

 <bean id="bean1" class="com.sxt.domain.Bean1" scope="prototype">
......
</bean>

Bean测试结果:

                bean1=(Bean1)factory.getBean("bean1");
Bean1 bean11=(Bean1)factory.getBean("bean1");
System.out.println(bean1==bean11); //false

 <bean id="bean1" class="com.sxt.domain.Bean1" scope="singleton">
......
</bean>
                bean1=(Bean1)factory.getBean("bean1");
Bean1 bean11=(Bean1)factory.getBean("bean1");
System.out.println(bean1==bean11); //true

默认是singleton范围。



3 spring的自动装配:

    default-autowire="byType" 会按照bean中的类型来装配对象

    default-autowire="byName" 会按照bean中的名字来装配对象

    






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值