【一】實(shí)驗(yàn)?zāi)康模?/h1>
(1)掌握Bean的實(shí)例化
(2)掌握Bean的裝配方式
(3)掌握Bean的作用域和生命周期
【二】結(jié)構(gòu):
1配置(不具體介紹)
2實(shí)例化:構(gòu)造方法實(shí)例化(最常用)沿猜、靜態(tài)工廠實(shí)例化枚荣、實(shí)例工廠實(shí)例化
3作用域
4生命周期
5裝配方式
【三】實(shí)驗(yàn)代碼:
一、實(shí)例化
BeanClass.java
package instance;
//一啼肩、bean的構(gòu)造方法之 實(shí)例化(最常用)
public class BeanClass {
public String message;
public BeanClass() {
message = "構(gòu)造方法實(shí)例化Bean";
}
public BeanClass(String s) {
message = s;
}
}
BeanInstanceFactory.java
package instance;
public class BeanInstanceFactory {
public BeanClass createBeanClassInstance() {
return new BeanClass("調(diào)用實(shí)例工廠方法實(shí)例化Bean");
}
}
BeanStaticFactory.java
package instance;
//二棍弄、bean實(shí)例化之 靜態(tài)工廠實(shí)例化
public class BeanStaticFactory {
private static BeanClass beanInstance = new BeanClass("調(diào)用靜態(tài)工廠方法實(shí)例化Bean");
public static BeanClass createInstance() {
return beanInstance;
}
}
配置文件:ApplicationContext.xml
<!-- 一、構(gòu)造方法實(shí)例化Bean -->
<bean id="constructorInstance" class="instance.BeanClass" scope="prototype"/>
<!-- 二疟游、靜態(tài)工廠方法實(shí)例化Bean,createInstance為 靜態(tài)工廠類BeanStaticFactory中的靜態(tài)方法-->
<bean id="staticFactoryInstance" class="instance.BeanStaticFactory" factory-method="createInstance"/>
<!-- 三呼畸、配置工廠 -->
<bean id="myFactory" class="instance.BeanInstanceFactory"/>
<!-- 還需要使用factory-bean屬性指定配置工廠,應(yīng)為這個(gè)是對(duì)象方法而不是類方法 颁虐,使用factory-method屬性指定使用工廠中哪個(gè)方法實(shí)例化Bean-->
<bean id="instanceFactoryInstance" factory-bean="myFactory" factory-method="createBeanClassInstance"/>
測(cè)試:TestInstance.java
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import instance.BeanClass;
public class TestInstance {
public static void main(String[] args) {
//初始化spring容器蛮原,通過(guò)路徑加載配置文件
@SuppressWarnings("resource")
ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
//一、構(gòu)造方法的
BeanClass b1 = (BeanClass)appCon.getBean("constructorInstance");
System.out.println(b1 + "------" + b1.message);
//二另绩、靜態(tài)工廠的
BeanClass b2 = (BeanClass)appCon.getBean("staticFactoryInstance");
System.out.println(b2+ "------" +b2.message);
//三儒陨、實(shí)例工廠的
BeanClass b3 = (BeanClass)appCon.getBean("instanceFactoryInstance");
System.out.println(b3+ "------" +b3.message);
}
}
二、作用域(可作用的范圍)
<!-- 構(gòu)造方法實(shí)例化Bean 笋籽,并設(shè)置為 prototype蹦漠,默認(rèn)為singleleton-->
<bean id="constructorInstance" class="instance.BeanClass" scope="prototype"/>
三、生命周期
配置文件:
<!-- 3.1车海、配置bean的生命周期笛园,使用init-method屬性指定初始化方法,使用 destroy-method屬性指定銷毀方法-->
<bean id="beanLife" class="life.BeanLife" init-method="initMyself" destroy-method="destroyMyself"/>
BeanLife.java
package life;
public class BeanLife {
//相當(dāng)于回調(diào)侍芝,以后可以寫數(shù)據(jù)庫(kù)的方法
public void initMyself() {
System.out.println(this.getClass().getName() + "?執(zhí)行自定義的初始化方法");
}
public void destroyMyself() {
System.out.println(this.getClass().getName() +"執(zhí)行自定義的銷毀方法");
}
}
TestLife.java
package test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import life.BeanLife;
public class TestLife {
public static void main(String[] args) {
//初始化容器研铆,加載配置文件
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println("獲得對(duì)象前");
BeanLife blife = (BeanLife)ctx.getBean("beanLife");
System.out.println("獲得對(duì)象后" + blife);
ctx.close();//關(guān)閉容器
}
}
四、裝配方式(bean注入到容器的方式)
1.基于xml的裝配方式
<!-- 4.1.1 使用構(gòu)造方法注入方式裝配ComplexUser實(shí)例 user1-->
<bean id="user1" class="assemble.ComplexUser">
<constructor-arg index="0" value="chenheng1"/>
<!--hobbyList-->
<constructor-arg index="1">
<list>
<value>唱歌</value>
<value>跳舞</value>
<value>爬山</value>
</list>
</constructor-arg>
<!--residenceMap -->
<constructor-arg index="2">
<map>
<entry key="dalian" value="大連"/>
<entry key="beijing" value="北京"/>
<entry key="shanghai" value="上海"/>
</map>
</constructor-arg>
<!--aliasSet -->
<constructor-arg index="3">
<set>
<value>陳恒100</value>
<value>陳恒101</value>
<value>陳恒102</value>
</set>
</constructor-arg>
<!--array -->
<constructor-arg index="4">
<array>
<value>aaaaa</value>
<value>bbbbb</value>
</array>
</constructor-arg>
</bean>
<!--4.1.2 使用setter方法注入方式裝配 ComplexUser實(shí)例user2 -->
<bean id="user2" class="assemble.ComplexUser">
<property name="uname" value="chenheng2"/>
<property name="hobbyList">
<list>
<value>看書</value>
<value>學(xué)習(xí)Spring</value>
</list>
</property>
<property name="residenceMap">
<map>
<entry key="shenzhen" value="深圳"/>
<entry key="gaungzhou" value="廣州"/>
<entry key="tianjin" value="天津"/>
</map>
</property>
<property name="aliasSet">
<set>
<value>陳恒103</value>
<value>陳恒104</value>
<value>陳恒105</value>
</set>
</property>
<property name="array">
<array>
<value>cccccc</value>
<value>dddddd</value>
</array>
</property>
</bean>
</beans>
ComplexUser.java
package assemble;
import java.util.List;
//4.1基于xml的裝配方式
import java.util.Map;
import java.util.Set;
public class ComplexUser {
private String uname;
private List<String> hobbyList;
private Map<String,String> residenceMap;
private Set<String> aliasSet;
private String[] array;
/*
* 使用構(gòu)造方法注入州叠,需要提供帶參數(shù)的構(gòu)造方法
*/
public ComplexUser(String uname, List<String> hobbyList, Map<String, String> residenceMap, Set<String> aliasSet,
String[] array) {
super();
this.uname = uname;
this.hobbyList = hobbyList;
this.residenceMap = residenceMap;
this.aliasSet = aliasSet;
this.array = array;
}
/*
* 使用屬性的setter方法注入棵红,提供默認(rèn)無(wú)參數(shù)的構(gòu)造方法,并未住入的屬性提供setter方法
*/
public ComplexUser() {
super();
}
public void setUname(String uname) {
this.uname = uname;
}
public void setHobbyList(List<String> hobbyList) {
this.hobbyList = hobbyList;
}
public void setResidenceMap(Map<String, String> residenceMap) {
this.residenceMap = residenceMap;
}
public void setAliasSet(Set<String> aliasSet) {
this.aliasSet = aliasSet;
}
public void setArray(String[] array) {
this.array = array;
}
@Override
public String toString() {
return "uname=" + uname + ";hobbyList =" + hobbyList + ";residenceMap="
+ residenceMap +";aliasSet=" + aliasSet + ";array=" + array;
}
}
TestAssemble.java 測(cè)試
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import assemble.ComplexUser;
public class TestAssemble {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext appCon = new ClassPathXmlApplicationContext("applicationContext.xml");
//測(cè)試構(gòu)造方法裝配
ComplexUser u1 = (ComplexUser)appCon.getBean("user1");
System.out.println(u1);
//測(cè)試setter方法注入
ComplexUser u2 = (ComplexUser)appCon.getBean("user2");
System.out.println(u2);
}
}
2.基于注解的裝配方式
@Component
該注解是一個(gè)泛化的概念咧栗,僅僅表示一個(gè)組件對(duì)象(Bean)逆甜,可以作用在任何層次上
AnnotationUser.java
package annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component()//相當(dāng)于@Component("annotationUser")或者@Component(value = "annotationUser")虱肄,annotationUser為Bean的id,默認(rèn)為首字母小寫的類名
public class AnnotationUser {
@Value("chenheng")
private String uname;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
@Override
public String toString() {
return "uname=" + uname;
}
}
在annotationContext里面告訴Spring在哪掃描
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用context命名空間交煞,通過(guò)Spring掃描指定包下所有Bean的實(shí)現(xiàn)類浩峡,進(jìn)行注解解析 -->
<context:component-scan base-package="annotation"/>
</beans>
在TestAnnotation里面測(cè)試(注意這里要導(dǎo)入spring-aop-5.0.5.RELEASE.jar的包)
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import annotation.AnnotationUser;
public class TestAnnotation {
public static void main(String[] args) {
ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml");
AnnotationUser au = (AnnotationUser)appCon.getBean("annotationUser");
System.out.println(au.getUname());
}
}
為了層次化:@Repository標(biāo)注DAO層,@Service標(biāo)注業(yè)務(wù)邏輯層错敢、@Controller標(biāo)注控制層
(1)創(chuàng)建dao層
package annotation.dao;
import org.springframework.stereotype.Repository;
@Repository("testDao")
public class TestDaoImpl implements TestDao{
@Override
public void save() {
System.out.println("testDao save");
}
}
(2)創(chuàng)建service層
package annotation.service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import annotation.dao.TestDao;
@Service("testService")//????@Service
public class TestSeviceImpl implements TestService{
@Resource(name="testDao")
private TestDao testDao;
@Override
public void save() {
testDao.save();
System.out.println("testService save");
}
}
(3)創(chuàng)建Controller層
package annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import annotation.service.TestService;
@Controller
public class TestController {
@Autowired
private TestService testService;
public void save() {
testService.save();
System.out.println("testController save");
}
}
配置注解:
之前已經(jīng)配置過(guò)了(通過(guò)Spring掃描指定包下所有Bean的實(shí)現(xiàn)類)
<context:component-scan base-package="annotation"/>
創(chuàng)建測(cè)試類:
TestMoreAnnotation
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import annotation.controller.TestController;
public class TestMoreAnnotation {
public static void main(String[] args) {
ApplicationContext appCon = new ClassPathXmlApplicationContext("annotationContext.xml");
TestController testcon = (TestController)appCon.getBean("testController");
testcon.save();
}
}
【四】遇到問(wèn)題:
The import javax.servlet.annotation cannot be resolved
導(dǎo)入:servlet-api.jar包翰灾,然后在buildpath-addlibrary里面點(diǎn)擊runtimesever添加服務(wù)器