通過(guò)配置applicationContext.xml文件來(lái)獲取類對(duì)象以及參數(shù)
實(shí)例:
步驟1:在com.hello.dao包下新建一個(gè)接口UserDao以及它的實(shí)現(xiàn)類UserDaoImpl
UserDao接口:
package com.hello.dao;
public interface UserDao {
????????public void login(String name,String pass);
}
UserDaoImpl實(shí)現(xiàn)類:
package com.hello.dao;
public class UserDaoImpl implements UserDao {
????????@Override
????????public void login(String name, String pass) {
????????????????System.out.println("用戶名:"+name+"? 密碼:"+pass);
????????}
}
步驟2:在com.hello.service包下新建一個(gè)接口UserService以及它的實(shí)現(xiàn)類UserServiceImpl
UserService接口:
package com.hello.service;
public interface UserService {
????????public void login(String name,String pass);
}
UserServiceImpl實(shí)現(xiàn)類:
package com.hello.service;
import com.hello.dao.UserDao;
public class UserServiceImpl implements UserService {
????????// 1. 添加字段(字段名要和applicationContext.xml中的一樣)
????????private UserDao userDao;
????????// 2. 添加字段的 setter() 方法(一定要有,要不然userDao為null)
????????public void setUserDao(UserDao userDao) {
????????????????this.userDao = userDao;
????????}
????????//3.在xml文件中需要把初始化的對(duì)象闰蛔,ref 到指定的類中
????????@Override
????????public void login(String name, String pass) {
????????????????userDao.login(name,pass);
????????}
}
步驟3:在src下新建一個(gè)xml文件applicationContext.xml
applicationContext.xml配置如下:
<?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="UserService" class="com.hello.service.UserServiceImpl">
????????<property name="userDao" ref="UserDao"></property>
</bean>
<bean id="UserDao" class="com.hello.dao.UserDaoImpl">
</bean>
</beans>
上面的property為參數(shù)注入彪标,參數(shù)注入需要set方法來(lái)初始化鞍匾,還有一個(gè)構(gòu)造器注入constructor斑匪,構(gòu)造器注入和參數(shù)注入差不多喊递,區(qū)別就是構(gòu)造器注入不需要set方法袭艺,而是需要有一個(gè)有參構(gòu)造方法來(lái)初始化庸诱。
步驟4:最后一個(gè)步驟就是測(cè)試了,新建一個(gè)測(cè)試類TestClass
package com.hello.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hello.service.UserService;
public class TestClass {
@Test
public void test(){
// 獲取 Spring 容器 --- BeanFactory
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
//獲取bean
UserService us = (UserService) factory.getBean("UserService");
us.login("陳贊", "1234");
}
}
就兩個(gè)步驟粗卜,獲取Spring容器+獲取需要用的bean就可以拿到需要用的一個(gè)對(duì)象了
*然后還有一個(gè)獲取Spring 容器的方式屋确,也就是讀取applicationContext.xml的方法:
方式②BeanFactory factory = new xmlBeanFactory(new ClassPathResoure("applicationContext.xml"))
這個(gè)方式跟上面那個(gè)方式①BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml") 的區(qū)別是什么呢?续扔?
方式①:在自身被實(shí)例化時(shí)一次性完成所有Bean的創(chuàng)建攻臀,所以在服務(wù)啟動(dòng)的時(shí)候就會(huì)校驗(yàn)xml文件的正確性
方式②:采用的是延遲加載,知道第一次使用getBean()方法的獲取Bean實(shí)例時(shí)才會(huì)創(chuàng)建相對(duì)應(yīng)的Bean纱昧,在服務(wù)器啟動(dòng)時(shí)不會(huì)校驗(yàn)xml文件的正確性刨啸,知道獲取bean時(shí),如果裝配錯(cuò)誤馬上就會(huì)產(chǎn)生異常(不受其它bean的影響)
簡(jiǎn)單的說(shuō)就是如果采用方式①识脆,執(zhí)行xml文件時(shí)只要隨便一個(gè)bean裝配有錯(cuò)誤都會(huì)報(bào)錯(cuò)设联;而采用方式②時(shí),執(zhí)行xml文件不會(huì)去校驗(yàn)正確性灼捂,只有g(shù)etBean的時(shí)候?qū)?yīng)的bean裝配有錯(cuò)誤時(shí)才會(huì)報(bào)錯(cuò)仑荐。