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 {
@Test
public 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 {
@Test
public 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測試:@Test
public 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();
}
@Test
public 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測試:、
@Test
public 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測試:
@Test
public 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測試:
@Test
public 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的基礎上