bean标签的属性和spring属性注入

来源:互联网 发布:蓝鸟中文编程 编辑:程序博客网 时间:2024/06/05 00:20


bean标签的属性:

id:给bean标签起的一个名字,命名没有要求(不能含有特殊字符),根据id值得到配置对



class:创建对象所在的类的全路径

name:功能和id属性相同,id属性值中不能包含特殊符号,但是name可以(现在name属性一

般不用了)

scope:设置的类的对象范围


   singleton:默认,单例

<bean id="user" class="lcn.spring.ioc.User" scope="singleton"></bean>
  public class TestIoc {@Testpublic void testUser(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象User user = (User) context.getBean("user");User user1 = (User) context.getBean("user");System.out.println("user="+user);System.out.println("user1="+user1);}

  输出:
user=lcn.spring.ioc.User@66922804
user1=lcn.spring.ioc.User@66922804
  
   prototype:多例
  
<bean id="user" class="lcn.spring.ioc.User" scope="prototype"></bean>
 public class TestIoc {@Testpublic void testUser(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象User user = (User) context.getBean("user");User user1 = (User) context.getBean("user");System.out.println("user="+user);System.out.println("user1="+user1);}
  输出:
user=lcn.spring.ioc.User@19c5466b
user1=lcn.spring.ioc.User@66922804


   session:创建对象把创建的对象放到session域里面去

   request:创建对象把创建的对象放到request域里面去

   globalsession:创建对象把创建的对象放到globalsession域里面去

属性注入:

创建对象的时候给类的属性设置值的过程。

java中属性注入的三种方式:
1-set方法
package lcn.spring.property;


public class Book {
public String bookname;


public void setBookname(String bookname) {
this.bookname = bookname;
}


public void test(){
System.out.println("bookname*/************"+bookname);
}


}


2-有参构造方法
 
 package lcn.spring.property;public class PropertyDemo1 {private String username;public PropertyDemo1(String username){this.username=username;}public void test(){System.out.println("demo1************"+username);}}
xml配置: <!-- spring有参属性注入 -->  <bean id="propertyDemo1" class="lcn.spring.property.PropertyDemo1">  <!-- 注入属性值 -->     <constructor-arg name="username" value="小明"></constructor-arg>  </bean>   <!-- set属性注入 -->    <bean id="book1" class="lcn.spring.property.Book">      <!-- 设置属性值      property:name对应实体类中的属性名 -->      <property name="bookname" value="三国演义"></property>          </bean>

junit测试:
@Testpublic void testPropertyDemo1(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("propertyDemo1");/*propertyDemo1对应的xmlbean标签里面的id属性*/System.out.println("demo1="+demo1);demo1.test();}@Testpublic void testBook1(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象Book book = (Book) context.getBean("book1");/*propertyDemo1对应的xmlbean标签里面的id属性*/System.out.println("book="+book);book.test();}

3-使用接口注入(接口-实现接口-实现接口方法(有参数))

注入参数对象(重点):

例:一个A类,一个B类,里面都有add方法,现在的需求是用B
类的对象调用A类中的add方法。

1-原始方法:在B类中创建A的对象,然后用A的对象调用add方法


2-spring注入方式:

package lcn.spring.ser;public class Dao {public void add1(){System.out.println("dao***************");}}package lcn.spring.ser;public class Service {private Dao dao;public void setDao(Dao setdao) {this.dao = setdao;}public void add2(){System.out.println("service..........");dao.add1();}}

xml配置:
<!--  重点:对象注入 -->   <bean id="dao" class="lcn.spring.ser.Dao"></bean>   <bean id="service" class="lcn.spring.ser.Service">    <!-- 注入对象      name:B类中需要注入的属性值名称     ref:引用,需要注入的引用    -->      <property name="dao" ref="dao"></property>   </bean>

junit测试:、
@Testpublic void testService(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象//A a= (A) context.getBean("a");Service ser= (Service) context.getBean("service");/*propertyDemo1对应的xmlbean标签里面的id属性*///.out.println("a="+a);System.out.println("ser="+ser);ser.add2();}
输出:
ser=lcn.spring.ser.Service@730efd7c
service..........
dao***************
3-P名称空间注入:
package lcn.spring.ser;public class Student {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public void show(){System.out.println(name);}}

xml配置文件:
<bean id="stu" class="lcn.spring.ser.Student" p:name="xiaoming"></bean>
junit测试:
junit测试:@Testpublic void testStudent(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象//A a= (A) context.getBean("a");Student student= (Student) context.getBean("stu");/*propertyDemo1对应的xmlbean标签里面的id属性*///.out.println("a="+a);System.out.println("student="+student);student.show();}

注入复杂类型的属性:

1-数组:
2-list集合:
3-map集合:
4-properties集合:


案例:
package lcn.spring.ser;import java.util.List;import java.util.Map;import java.util.Properties;public class Person {private String[] arr;private List<String> list;private Map<String,String> map;private Properties properties;public void setArr(String[] arr) {this.arr = arr;}public void setList(List<String> list) {this.list = list;}public void setMap(Map<String, String> map) {this.map = map;}public void setProperties(Properties properties) {this.properties = properties;}public void test(){System.out.println("arr:"+arr);System.out.println("list:"+list);System.out.println("map:"+map);System.out.println("properties:"+properties);}}

xml配置:

<!-- 注入复杂类型的属性 -->   <bean id="person" class="lcn.spring.ser.Person">        <property name="arr">           <list>               <value>小明</value>               <value>小花</value>               <value>小草</value>               <value>小王</value>           </list>        </property>                <property name="list">            <list>               <value>春</value>               <value>夏</value>               <value>秋</value>               <value>冬</value>           </list>        </property>                <property name="map">           <map>               <entry key="aa" value="上海"></entry>               <entry key="bb" value="北京"></entry>               <entry key="cc" value="广州"></entry>           </map>        </property>                <property name="properties">        <props>           <prop key="this">hello</prop>           <prop key="username">beijing</prop>        </props>          </property>   </bean>

junit测试:
@Testpublic void testPerson(){//加载spring配置文件,根据配置创建对象ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");//得到配置的创建的对象//A a= (A) context.getBean("a");Person person= (Person) context.getBean("person");/*propertyDemo1对应的xmlbean标签里面的id属性*///.out.println("a="+a);System.out.println("Person="+person);person.test();}

输出:
Person=lcn.spring.ser.Person@198e261d
arr:[Ljava.lang.String;@43684706
list:[春, 夏, 秋, 冬]
map:{aa=上海, bb=北京, cc=广州}
properties:{this=hello, username=beijing}


IOC和DI的区别:
IOC控制反转,把创建对象交给spring来完成
Di给属性注入值的过程
联系:DI不能单独存在,需要建立在IOC的基础上

原创粉丝点击