最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Spring框架:構(gòu)造器注入,ref,servlet或Java項(xiàng)目中使用,面向切面,裝配【詩(shī)書畫唱】

2021-03-17 15:11 作者:詩(shī)書畫唱  | 我要投稿

本期的內(nèi)容:

使用Spring框架來調(diào)用Student的屬性和構(gòu)造方法,構(gòu)造器注入等等的例子


要點(diǎn)概況(講義)


什么是面向切面


解耦的概念


spring框架的核心就是spring容器,所以在寫代碼的時(shí)候要想辦法獲取spring容器


"控制反轉(zhuǎn)的意思就是當(dāng)我們需要調(diào)用一個(gè)類中的方法時(shí),盡量不要自己去new出這個(gè)類,而是直接問spring容器要這個(gè)類就可以了。"

SPRINgcORE框架簡(jiǎn)介


Spring框架的最終目的就是簡(jiǎn)化java開發(fā)。


在servlet中獲取Spring容器的方法


推薦閱讀


1、SPRINgcORE框架_簡(jiǎn)介.ppt?


2、SPRINgcORE框架_裝配Bean.ppt?


創(chuàng)建一個(gè)spring框架的項(xiàng)目的方法?


個(gè)人對(duì)容器的理解?

個(gè)人理解:容器就是把數(shù)據(jù)放到里面,可以通過"."獲取要拿出來的數(shù)據(jù)


教你理解spring框架中的配置文件中的bean標(biāo)簽的id等內(nèi)容


在servlet中獲取spring容器的方法


在main方法中獲取spring容器的代碼方法


一般出現(xiàn)了類的全路徑,比如class=“XXX”的內(nèi)容的話,一般就是使用了反射的技術(shù)。


教你理解loC(控制反轉(zhuǎn))


Spring中的依賴注入(DI)的方式:構(gòu)造器注入,setter注入


構(gòu)造器注入(也就是值注入,其中創(chuàng)建的類中會(huì)創(chuàng)建構(gòu)造方法,傳值)


這里的spring配置的設(shè)置,讓調(diào)用構(gòu)造方法等傳的axe類,調(diào)用方法時(shí),axe不會(huì)為空


ApplicationContext就是應(yīng)用上下文的意思


在Java項(xiàng)目中使用Spring框架打印“Hello?world”?

在Java Web項(xiàng)目中使用Spring框架打印“Hello?world”


單詞解釋:application(應(yīng)用)Context(上下文).xml:應(yīng)用上下文的xml文件。

個(gè)人注釋:其實(shí)”容器“思想貫徹幾乎所有的編程思想中,總可以找到身為”容器“身份的對(duì)象,因?yàn)楹芎糜谩?/p>

個(gè)人感悟:其實(shí)Spring框架等最關(guān)建的就是知道如何根據(jù)需求設(shè)置配置文件等等。



使用Spring框架來調(diào)用Student的屬性和構(gòu)造方法,構(gòu)造器注入等等的例子 START


package com.SSHC.bean;

//axe:斧頭

public class Axe {

? ? public void cut(String sth){

? ? System.out.println("Axe類中的cut方法正在" + sth);

? ? }

}

package com.SSHC.bean;


public class HelloBean {

? ? public void sayHello(){

? ? System.out.println("Hello world");

? ? }

}

package com.SSHC.bean;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Person {

private Axe axe;

public Person(Axe m){

this.axe = m;

axe.cut("砍樹【Person(Axe m)方法中】");/*這句可以注釋掉*/

}

/*axe: 斧頭

perform:執(zhí)行 */

? ? public void perform(){

? ? //創(chuàng)建斧頭的權(quán)利被剝奪了,由spring容器來創(chuàng)建

? ? //Axe axe = new Axe();

? ? axe.cut("砍樹【perform()方法中】");

? ? }

}

package com.SSHC.bean;


public class Student {

? ? private String name;

? ? private Integer age;

? ? private Axe axe;

? ??

? ? public Student(){

? ?

? ? }

? ? public Student(String name){

? ? this.name = name;

? ? }

? ? public Student(String name,Integer age,Axe m){

? ? this.name = name;

? ? this.age = age;

? ? this.axe = m;

? ? }

? ??

? ? @Override

? ? public String toString() {

? ? // TODO Auto-generated method stub

? ? if(this.axe != null) {

? ? ? ? this.axe.cut("劈柴");

? ? }

? ? return "姓名是:" + this.name + ",年齡是:" + this.age;

? ? }

}

/**CTRL+F:

?* 使用ApplicationContext獲取自己命名為ctx的Spring容器

?* */



package com.SSHC.Text;

import java.util.Scanner;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.SSHC.bean.Axe;

import com.SSHC.bean.HelloBean;

import com.SSHC.bean.Person;

import com.SSHC.bean.Student;



public class Test {


public static void main(String[] args) {

// TODO Auto-generated method stub



/*使用ApplicationContext獲取自己命名為ctx的Spring容器 START

?

?*每次調(diào)用Spring容器的時(shí)候就會(huì)調(diào)用Axe類中聲明的cut方法

?*(Axe類中的cut方法)。

?*/



ApplicationContext ctx = new?

ClassPathXmlApplicationContext("applicationContext.xml");

/*使用ApplicationContext獲取自己命名為ctx的Spring容器 END

*/

while(true){


System.out.print("請(qǐng)選擇操作:"

+ "\n 1.不使用Spring就調(diào)用"

+ "\n HelloBean中的sayHello方法(以后不用)"

+ ",\n 2.使用Spring框架來調(diào)用HelloBean中的sayHello方法,"

+ "\n 3.原始地運(yùn)用new等關(guān)鍵字來調(diào)用構(gòu)造方法"

+ "\n (個(gè)人的理解:將被Spring容器"

+ "\n 的獲取數(shù)據(jù)的方法取代,因?yàn)橛小榜詈稀钡娜秉c(diǎn)),"

+ "\n 4.構(gòu)造器注入(引用注入)可以設(shè)置構(gòu)造方法傳的類不為空,"

+ "\n 同時(shí)調(diào)用含“傳的類.傳的聲明的方法”的方法。"

+ "\n 5.使用Spring框架來調(diào)用"

+ "\n Student的屬性和構(gòu)造方法等等(沒有使用value和ref)"

+ "\n 6.使用Spring框架來調(diào)用"

+ "\n Student的屬性和構(gòu)造方法等等(使用了value,沒使用ref)"

+ "\n 7.使用Spring框架來調(diào)用"

+ "\n Student的屬性和構(gòu)造方法等等(使用了value和ref)"

+ "");

? ?Scanner input = new Scanner(System.in);

? ?int num = input.nextInt();

? ?System.out.println(num);

?

? ?if(num==1){

HelloBean hb = new HelloBean();

hb.sayHello();

}

if(num==2){


HelloBean a = (HelloBean) ctx.getBean("hb");

a.sayHello();

}

if(num==3){

/*在person中創(chuàng)建Axe屬性,同時(shí)創(chuàng)建傳

* Axe的構(gòu)造方法,下面本來是調(diào)用其構(gòu)造方法的方式,

* 但是使用spring框架后就不用像下面的方式來寫代碼了。

*?

* 下面就是調(diào)用其構(gòu)造方法*/



Person p = new Person(new Axe());


/*Person p = new Person(new Axe());

* 就是調(diào)用 Person(Axe類)的構(gòu)造方法。用

* p.perform();調(diào)用不了其

* perform方法*/

//System.out.println(p);

}

if(num==4){

Person p = (Person)ctx.getBean("p");

p.perform();

/*因?yàn)樵赟pring框架配置文件中設(shè)置了

*? <bean id="axe" class="com.SSHC.bean.Axe"></bean>

? ? <bean id="p" class="com.SSHC.bean.Person">

? ? ? ? <constructor-arg ref="axe"></constructor-arg>

? ? </bean>

? ? ,所以p.perform();的調(diào)用才不會(huì)報(bào)錯(cuò)。

? ? 要知道perform方法中其實(shí)是“聲明調(diào)用了傳過來的

? ? Axe類中聲明的方法”的一個(gè)方法。

? ??

? ??

? ??

? ? */


}

if(num==5){

/* 下面是配置文件中的設(shè)置:

?*?

?* <bean id="s1" class="com.SSHC.bean.Student"></bean>

? ? <bean id="s2" class="com.SSHC.bean.Student">

? ? ? ? <constructor-arg value="小明"></constructor-arg>

? ? </bean>

? ? <bean id="s3" class="com.SSHC.bean.Student">

? ? ? ? <constructor-arg value="Kite"></constructor-arg>

? ? ? ? <constructor-arg value="20"></constructor-arg>

? ? ? ? <constructor-arg ref="axe"></constructor-arg>

? ? </bean>

?

?s1是沒有給Student類用value設(shè)置構(gòu)造方法中要打印的

?屬性的值,也沒有用ref調(diào)用只傳axe類的構(gòu)造方法,

?所以打印的語句的輸出的結(jié)果

?的話就是空。

?

? 而s2是只用value設(shè)置構(gòu)造方法中要打印的屬性的值,

? 但沒有用ref調(diào)用只傳axe類的構(gòu)造方法。

??

s3是用value設(shè)置構(gòu)造方法中要打印的屬性的值,

? 也用了ref來調(diào)用只傳axe類的構(gòu)造方法。

?* */



Student s1 = (Student) ctx.getBean("s1");

System.out.println(s1);

}

if(num==6){

Student s2 = (Student) ctx.getBean("s2");

System.out.println(s2);

}

if(num==7){ Student s3 = (Student) ctx.getBean("s3");

System.out.println(s3);}



}

}

}

<?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"

? ? xmlns:mvc="http://www.springframework.org/schema/mvc"

? ? xmlns:tx="http://www.springframework.org/schema/tx"

? ? 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-4.1.xsd

? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

? ? ? ? http://www.springframework.org/schema/tx?

? ? ? ? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

? ? <bean id="hb" class="com.SSHC.bean.HelloBean"></bean>

? ??

? ??

? ? <bean id="axe" class="com.SSHC.bean.Axe"></bean>

? ? <bean id="p" class="com.SSHC.bean.Person">

? ? ? ? <constructor-arg ref="axe"></constructor-arg>

? ? </bean>

? ? <bean id="s1" class="com.SSHC.bean.Student"></bean>

? ? <bean id="s2" class="com.SSHC.bean.Student">

? ? ? ? <constructor-arg value="小明"></constructor-arg>

? ? </bean>

? ? <bean id="s3" class="com.SSHC.bean.Student">

? ? ? ? <constructor-arg value="詩(shī)書畫唱"></constructor-arg>

? ? ? ? <constructor-arg value="22"></constructor-arg>

? ? ? ? <constructor-arg ref="axe"></constructor-arg>

? ? </bean>

</beans>




ApplicationContext就是應(yīng)用上下文的意思



使用Spring框架來調(diào)用Student的屬性和構(gòu)造方法,構(gòu)造器注入等等的例子?END




Spring中的依賴注入(DI)的方式:構(gòu)造器注入,setter注入








構(gòu)造器注入(也就是值注入,其中創(chuàng)建的類中會(huì)創(chuàng)建構(gòu)造方法,傳值)

總之的話,使用spring框架就是要盡量減少new,import等關(guān)鍵字的使用,要把這些關(guān)鍵字的使用配置文件的設(shè)置來替代。


這里的spring配置的設(shè)置,讓調(diào)用構(gòu)造方法等傳的axe類,調(diào)用方法時(shí),axe不會(huì)為空

總之,其實(shí)這些代碼的話,都是有邏輯的,思考后就可以知道其規(guī)律等等。


這里的spring配置的設(shè)置,讓調(diào)用構(gòu)造方法等傳的axe類,調(diào)用方法時(shí),axe不會(huì)為空






控制反轉(zhuǎn)的例子








創(chuàng)建一個(gè)spring框架的項(xiàng)目的方法 START


首先,創(chuàng)建lib的文件夾,之后把spring的復(fù)制粘貼到lib的文件夾中,這樣的話就會(huì)更方便地找。之后就是把a(bǔ)pplicationContext.xml(雖然批量統(tǒng)一路徑等改也可以運(yùn)行,但最好不要改名字,為了讓別人知道我使用了Spring框架)



首先,創(chuàng)建lib的文件夾,之后把spring的復(fù)制粘貼到lib的文件夾中,這樣的話就會(huì)更方便地找。之后就是把a(bǔ)pplicationContext.xml(雖然批量統(tǒng)一路徑等改也可以運(yùn)行,但最好不要改名字,為了讓別人知道我使用了Spring框架)


上面的紅色部分是不使用Spring框架時(shí)在main方法中調(diào)用bean等中方法的方式,被Spring取代了,很多值內(nèi)容統(tǒng)一地寫在了xml配置文件中,方便查看,修改,且別的內(nèi)容很少要修改。

先設(shè)置好配置文件,之后在獲取配置文件內(nèi)容,調(diào)用內(nèi)容等等。

在main方法中獲取spring容器的代碼方法:

上面的紅色部分是不使用Spring框架時(shí)在main方法中調(diào)用bean等中方法的方式,被Spring取代了,很多值內(nèi)容統(tǒng)一地寫在了xml配置文件中,方便查看,修改,且別的內(nèi)容很少要修改。先設(shè)置好配置文件,之后在獲取配置文件內(nèi)容,調(diào)用內(nèi)容等等。



一般出現(xiàn)了類的全路徑,比如class=“XXX”的內(nèi)容的話,一般就是使用了反射的技術(shù)。


一般出現(xiàn)了類的全路徑,比如class=“XXX”的內(nèi)容的話,一般就是使用了反射的技術(shù)。




創(chuàng)建一個(gè)spring框架的項(xiàng)目的方法 END


在servlet中獲取spring容器的方法 START

在servlet中獲取spring容器的方法 END

教你理解loC(控制反轉(zhuǎn))START

個(gè)人理解:

loC,控制反轉(zhuǎn)就是本來可以在一個(gè)類中new出別的類(比如有main方法的text類中,new實(shí)例化了person類),單在依賴注入的模式下,這種可以new出別的類的“控制權(quán)”,被剝奪,

就是“控制權(quán)”被“反轉(zhuǎn)”了,被“造反”了。


教你理解loC(控制反轉(zhuǎn))END



個(gè)人對(duì)容器的理解 START


個(gè)人理解:容器就是把數(shù)據(jù)放到里面,可以通過"."獲取要拿出來的數(shù)據(jù)

個(gè)人理解:容器就是把數(shù)據(jù)放到里面,可以通過"."獲取要拿出來的數(shù)據(jù)




個(gè)人對(duì)容器的理解 END

教你理解spring框架中的配置文件中的bean標(biāo)簽的id等內(nèi)容 START


其實(shí)在applicationContext.xml文件中聲明以上的bean標(biāo)簽中的內(nèi)容的話,就是相當(dāng)于在main方法中new實(shí)例化了一個(gè)類,同時(shí)id的值就像是特殊的獨(dú)一無二的“飯票”,用獨(dú)一無二的“飯票”,通過“."獲取獨(dú)一無二的對(duì)應(yīng)的內(nèi)容,比如圖中的MyHello類中的內(nèi)容。


其實(shí)在applicationContext.xml文件中聲明以上的bean標(biāo)簽中的內(nèi)容的話,就是相當(dāng)于在main方法中new實(shí)例化了一個(gè)類,同時(shí)id的值就像是特殊的獨(dú)一無二的“飯票”,用獨(dú)一無二的“飯票”,通過“."獲取獨(dú)一無二的對(duì)應(yīng)的內(nèi)容,比如圖中的MyHello類中的內(nèi)容。






教你理解spring框架中的配置文件中的bean標(biāo)簽的id等內(nèi)容 END



視頻教程筆記 START


個(gè)人的理解:

開源指的是github上有源碼。

spring的輕量級(jí)就是相對(duì)于EJB這個(gè)有非常笨重這個(gè)企業(yè)級(jí)的Java Beans而言的。

EJB是的Enterprise Java Beans技術(shù)的簡(jiǎn)稱, 又被稱為企業(yè)Java Beans。這種技術(shù)最早是由美國(guó)計(jì)算公司研發(fā)出來的。

“服務(wù)于其他的框架”的意思是可以整合其他的框架,讓其他的框架可以一起結(jié)合起來使用,相當(dāng)于”媒婆“。


Spring的低侵入式設(shè)計(jì)(比如依賴注入(DI),解耦就是體現(xiàn)了這種設(shè)計(jì))就是指增強(qiáng)了各個(gè)部分的互不影響性,獨(dú)立性,一個(gè)部分出BUG,修改后,別的部分出BUG等的影響會(huì)很小。因?yàn)轫?xiàng)目會(huì)針對(duì)接口編程,dao,service類都會(huì)有一個(gè)接口,之前的MVC開發(fā)模式中使用了dao,service,結(jié)合之前自己使用接口的感覺后對(duì)其好處優(yōu)點(diǎn)深有體會(huì)和認(rèn)同。


Spring框架可以減少import,new等關(guān)鍵字的使用,減少修改時(shí)出的BUG等“一傳十,十傳百”的情況。Spring是接口編程,一些要new等調(diào)用的方法,就是被容器等代替了。spring

可以減少除了配置文件外的內(nèi)容的批量地改變等等。



"解耦的概念:在編寫程序時(shí),要讓類和類之間具備一定的關(guān)系方便傳遞消息,也要讓類和類之間具備有一定的獨(dú)立性,這樣能保證當(dāng)你修改一個(gè)類中的BUG,不會(huì)導(dǎo)致其他類出現(xiàn)新的BUG"


過濾器就是用了面向切面(AOP)的思想。

在軟件業(yè),AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預(yù)編譯方式和運(yùn)行期間動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP是OOP的延續(xù),是軟件開發(fā)中的一個(gè)熱點(diǎn),也是Spring框架中的一個(gè)重要內(nèi)容,是函數(shù)式編程的一種衍生范型。利用AOP可以對(duì)業(yè)務(wù)邏輯的各個(gè)部分進(jìn)行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率。


aspect 英[??spekt]美[??spekt]

n.方面;


oriented,英語單詞,主要用作形容詞、動(dòng)詞,作形容詞時(shí)譯為“以……為方向的;重視……的”

oriented英[???rient?d]美[???rient?d]

v.朝向;?



programming英[?pr??ɡr?m??]美[?pro?ɡr?m??]

n.編程;?






視頻教程筆記 END






要點(diǎn)概況(講義) START


1.? ? ? Spring AOP? 面向切面編程思想

2.? ? ? Spring ORM? Hibernate|mybatis|JDO

3.? ? ? Spring Core? 提供bean工廠 IOC

4.? ? ? Spring Dao? JDBC支持

5.? ? ? Spring Context? 提供了關(guān)于UI支持,郵件支持等

6.? ? ? Spring Web 提供了web的一些工具類的支持

7.? ? ? Spring MVC? 提供了web mvc , webviews , jsp ,pdf ,export


"STRUTS2框架:升級(jí)servlet為action,javaweb框架,getParameter和getAttribute和

setAttribute方法都被框架隱藏起來了。"

"MYBATIS框架:升級(jí)DAO,通用框架,以后在javaweb項(xiàng)目中,不需要寫Dao類了,直接通過映射文件實(shí)現(xiàn)SQL語句。"

SPRING框架:通用框架,粘合其他的兩個(gè)框架的。

SPRINGBOOT,SPRINgclOUD,JBPM工作流

SPRING的兩大核心:

AOP:面向切面

OOP:面向?qū)ο?/p>

IOC:控制反轉(zhuǎn)(DI:依賴注入)


SPRINgcORE:框架

EJB:企業(yè)級(jí)的JavaBean,非常笨重


項(xiàng)目會(huì)針對(duì)接口編程:dao,service類都會(huì)有一個(gè)接口


“解耦:在編寫程序時(shí),要讓類和類之間具備一定的關(guān)系方便傳遞消息,也要讓類和類之間具備有一定的獨(dú)立性,這樣能保證當(dāng)你修改一個(gè)類中的BUG,不會(huì)導(dǎo)致其他類出現(xiàn)新的BUG"


AOP:比較難理解


在項(xiàng)目中使用spring框架:

main方法運(yùn)行的項(xiàng)目:

1、導(dǎo)包

2、導(dǎo)入配置文件

3、編程測(cè)試


spring框架的核心就是spring容器,所以在寫代碼的時(shí)候要想辦法獲取spring容器


"控制反轉(zhuǎn)的意思就是當(dāng)我們需要調(diào)用一個(gè)類中的方法時(shí),盡量不要自己去new出這個(gè)類,而是直接問spring容器要這個(gè)類就可以了。"


DI:構(gòu)造器注入和setter注入



要點(diǎn)概況(講義)?END



什么是面向切面 START

Spring提供了面向切面編程的豐富支持,允許通過分離應(yīng)用的業(yè)務(wù)邏輯與系統(tǒng)級(jí)服務(wù)(例如審計(jì)(auditing)和事務(wù)管理)進(jìn)行內(nèi)聚性的開發(fā)。應(yīng)用對(duì)象只實(shí)現(xiàn)它們應(yīng)該做的——完成業(yè)務(wù)邏輯——僅此而已。它們并不負(fù)責(zé)(甚至是意識(shí))其它的系統(tǒng)級(jí)關(guān)注點(diǎn),例如日志或事務(wù)支持。


把和主業(yè)務(wù)無關(guān)的事情,放到代碼外面去做。


某一行代碼經(jīng)常在你的Controller里出現(xiàn),比如方法入口日志打印,那就要考慮使用AOP來精簡(jiǎn)你的代碼了。



推薦閱讀:https://www.zhihu.com/question/24863332



https://www.baeldung.com/spring-aop-vs-aspectj


什么是面向切面 END


1、SPRINgcORE框架_簡(jiǎn)介.ppt START

SPRINgcORE框架簡(jiǎn)介


SPRINgcORE框架簡(jiǎn)介


Spring框架介紹:

開源

輕量級(jí)

服務(wù)于其他框架


優(yōu)點(diǎn):

1、低侵入式設(shè)計(jì)。 2、依賴注入(DI)解耦 3、面向切面(AOP)提高代碼的復(fù)用度 4、高度開放性,可自由選擇功能引入。

Spring框架的核心思想

控制反轉(zhuǎn)(IoC),通過依賴注入(ID)實(shí)現(xiàn)

面向切面編程(AOP)

Spring框架的最終目的就是簡(jiǎn)化java開發(fā)。


Spring容器

在Spring框架參與的應(yīng)用中,你所創(chuàng)建的類可以交由Spring的容器來進(jìn)行管理。

Spring容器將掌控這些類從創(chuàng)建到銷毀的整個(gè)生命周期。

換個(gè)方式理解,就是在使用Spring框架以后,幾乎所有需要?jiǎng)?chuàng)建的類都可以直接從Spring容器中獲取。所以,項(xiàng)目中很少會(huì)用到關(guān)鍵字new來創(chuàng)建對(duì)象。我們把容器中的對(duì)象稱為bean。

Spring容器中bean的生命周期

獲取Spring容器

Spring容器是Spring的核心,一切Spring bean都存儲(chǔ)在Spring容器內(nèi),并由其通過IoC技術(shù)管理。Spring容器也就是一個(gè)bean工廠(BeanFactory)。應(yīng)用中bean的實(shí)例化,獲取,銷毀等都是由這個(gè)bean工廠管理的。

加載并獲取Spring容器的類:

? ?1、FileSystemXmlApplicationContext(從文件系統(tǒng)加載,也就是寫絕對(duì)路徑)

? ?2、ClassPathXmlApplicationContext(從類路徑加載,在src目錄下)

? ?3、WebApplicationContext(在web工程中加載,在servlet上下文中直接獲取)

創(chuàng)建第一個(gè)Spring程序

要使用Spring框架,需要在工程中添加相關(guān)的JAR包。從官方網(wǎng)站下載spring的最新版本。

跟其他的框架一樣,Spring框架也有它自己的配置文件。Spring容器的加載就是依照這些配置文件進(jìn)行的。我們可以在文件系統(tǒng)中直接加載Spring容器,在工程的類路徑中加載Spring容器也可以在servlet上下文獲取Spring容器。不管用那種方式加載,配置文件的內(nèi)容不變。

創(chuàng)建第一個(gè)Spring程序

在工程的src目錄下新增配置文件applicationContext.xml。

創(chuàng)建一個(gè)HelloBean類,該類中有一個(gè)sayHi方法。

創(chuàng)建第一個(gè)Spring程序

在applicationContext.xml文件中添加如下的代碼:

<bean id="helloBean" class="springcore.HelloBean"></bean>

創(chuàng)建第一個(gè)Spring程序

創(chuàng)建一個(gè)測(cè)試類,在這個(gè)類中我們不要自己new出helloBean對(duì)象,而是從Spring容器中拿到這個(gè)對(duì)象,再調(diào)用syHi方法,把Hello world打印出來。

在servlet中獲取Spring容器的方法

前面我們演示的是在main方法中獲取Spring容器的代碼,那么在servlet中如何獲取Spring容器呢?

修改web.xml文件,添加一個(gè)listener標(biāo)簽。

在servlet中獲取Spring容器的方法

將applicationContext.xml文件移到WEB-INF目錄下。

創(chuàng)建一個(gè)servlet,在這個(gè)servlet中獲取Spring容器的方法如下:


1、SPRINgcORE框架_簡(jiǎn)介.ppt END



2、SPRINgcORE框架_裝配Bean.ppt START



2、SPRINgcORE框架_裝配Bean.ppt END




在Java項(xiàng)目中使用Spring框架打印“Hello world” START


項(xiàng)目需要的文件的概覽


package com.SSHC.bean;


public class MyHello {

? ? public void sayHello(){

? ? System.out.println("Hello world");

? ? }

}



package JavaText;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.SSHC.bean.MyHello;



public class Demo {


public static void main(String[] args) {

// TODO Auto-generated method stub

ApplicationContext ctx = new?

? ? ? ? ClassPathXmlApplicationContext("applicationContext.xml");

MyHello b = (MyHello) ctx.getBean("mh");

b.sayHello();

}


}

<?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"

? ? xmlns:mvc="http://www.springframework.org/schema/mvc"

? ? xmlns:tx="http://www.springframework.org/schema/tx"

? ? 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-4.1.xsd

? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

? ? ? ? http://www.springframework.org/schema/tx?

? ? ? ? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

? ? <bean id="mh" class="com.SSHC.bean.MyHello"></bean>

</beans>






在Java項(xiàng)目中使用Spring框架打印“Hello?world” END


在Java Web項(xiàng)目中使用Spring框架打印“Hello?world” START

該項(xiàng)目所需文件概覽

package com.SSHC.servlet;


import java.io.IOException;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;


import com.SSHC.bean.MyHello;


/**

?* Servlet implementation class TestServlet

?*/

@WebServlet("/ts")

public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

? ? ? ?

? ? /**

? ? ?* @see HttpServlet#HttpServlet()

? ? ?*/

? ? public TestServlet() {

? ? ? ? super();

? ? ? ? // TODO Auto-generated constructor stub

? ? }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

this.doPost(request, response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//獲取javaweb的上下文

ServletContext sc = this.getServletContext();

//獲取spring容器

WebApplicationContext ctx = WebApplicationContextUtils

.getWebApplicationContext(sc);

MyHello b = (MyHello) ctx.getBean("mh");

b.sayHello();

}


}



package com.SSHC.bean;


public class MyHello {

? ? public void sayHello(){

? ??System.out.println("Hello world");

? ? }

}


package com.SSHC.servlet;


import java.io.IOException;


import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;


import com.SSHC.bean.MyHello;


/**

?* Servlet implementation class TestServlet

?*/

@WebServlet("/ts")

public class TestServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

? ? ? ?

? ? /**

? ? ?* @see HttpServlet#HttpServlet()

? ? ?*/

? ? public TestServlet() {

? ? ? ? super();

? ? ? ? // TODO Auto-generated constructor stub

? ? }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

this.doPost(request, response);

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

//獲取javaweb的上下文

ServletContext sc = this.getServletContext();

//獲取spring容器

WebApplicationContext ctx = WebApplicationContextUtils

.getWebApplicationContext(sc);

MyHello b = (MyHello) ctx.getBean("mh");

b.sayHello();

}


}


<?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"

? ? xmlns:mvc="http://www.springframework.org/schema/mvc"

? ? xmlns:tx="http://www.springframework.org/schema/tx"

? ? 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-4.1.xsd

? ? ? ? http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd

? ? ? ? http://www.springframework.org/schema/tx?

? ? ? ? http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

? ? <bean id="mh" class="com.SSHC.bean.MyHello"></bean>

</beans>




<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

? <display-name>Spring2</display-name>

? <listener>

? ? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

? </listener>

? <welcome-file-list>

? ? <welcome-file>index.html</welcome-file>

? ? <welcome-file>index.htm</welcome-file>

? ? <welcome-file>index.jsp</welcome-file>

? ? <welcome-file>default.html</welcome-file>

? ? <welcome-file>default.htm</welcome-file>

? ? <welcome-file>default.jsp</welcome-file>

? </welcome-file-list>

</web-app>



在Java Web項(xiàng)目中使用Spring框架打印“Hello?world” END


Spring框架:構(gòu)造器注入,ref,servlet或Java項(xiàng)目中使用,面向切面,裝配【詩(shī)書畫唱】的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
洞头县| 和林格尔县| 含山县| 景宁| 贡嘎县| 阜城县| 中超| 鄯善县| 千阳县| 元谋县| 广东省| 樟树市| 汾阳市| 安西县| 泸溪县| 辽宁省| 通河县| 石河子市| 英德市| 如皋市| 湾仔区| 武穴市| 丰台区| 桂林市| 辉县市| 无棣县| 偏关县| 碌曲县| 南平市| 香格里拉县| 大荔县| 土默特右旗| 灌云县| 常宁市| 绿春县| 辽阳市| 夏邑县| 汉源县| 高清| 衡阳市| 岱山县|