一段費(fèi)話
Spring是一個(gè)開(kāi)源框架,Spring是于2003 年興起的一個(gè)輕量級(jí)的java 開(kāi)發(fā)框架,由Rod Johnson在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來(lái)。它是為了解決企業(yè)應(yīng)用開(kāi)發(fā)的復(fù)雜性而創(chuàng)建的。框架的主要優(yōu)勢(shì)之一就是其分層架構(gòu),分層架構(gòu)允許使用者選擇使用哪一個(gè)組件,同時(shí)為J2EE應(yīng)用程序開(kāi)發(fā)提供集成的框架。Spring使用基本的JavaBean來(lái)完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限于服務(wù)器端的開(kāi)發(fā)。從簡(jiǎn)單性、可測(cè)試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。Spring的核心是控制反轉(zhuǎn)(IoC)和面向切面(AOP)。簡(jiǎn)單來(lái)說(shuō),Spring是一個(gè)分層的JavaSE/EEfull-stack(一站式)輕量級(jí)開(kāi)源框架。
二、Spring體系結(jié)構(gòu)以上大部分都可以 單獨(dú)使用
2.2、各部分下載地址
http://repo.spring.io/libs-release-local/org/springframework/spring/
五、使用Spring實(shí)現(xiàn)Hello Spring添加包
建立類(lèi)
package com.pb.demo;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathxmlApplicationContext;public class HelloSpring { private String str; public void print() { System.out.println("Hello! " + this.getStr()); } public static void main(String[] args) { // 創(chuàng)建ApplicationContext上下文件對(duì)象 ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); HelloSpring hello=(HelloSpring) context.getBean("hellospring"); hello.print(); } public String getStr() { return str; } public void setStr(String str) { this.str = str; }}
建立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" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="hellospring" class="com.pb.demo.HelloSpring"><property name="str" value="Spring"></property></bean></beans>
另一個(gè)
package com.pb.demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringDemo1 { private String name; private String job; public void print() { System.out.println("姓名: " + this.getName() + " /t職業(yè): " + this.getJob()); } public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); SpringDemo1 demo=(SpringDemo1)context.getBean("springdemo"); demo.print(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getJob() { return job; } public void setJob(String job) { this.job = job; }}
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="hellospring" class="com.pb.demo.HelloSpring"><property name="str" value="Spring"></property></bean><bean id="springdemo" class="com.pb.demo.SpringDemo1"><property name="name" value="林沖"/><property name="job" value="80萬(wàn)禁軍教頭!"/></bean></beans>六、Spring對(duì)單態(tài)和工廠模式的支持6.1、工廠模式
package com.pb.lnk;/** * 墨盒接口 * @author Administrator * */public interface Lnk { public void getColor();}
實(shí)現(xiàn)類(lèi)
package com.pb.lnk;/** * 彩色墨盒 * @author Administrator * */public class Color implements Lnk { @Override public void getColor() { System.out.println("=======使用彩色墨盒打印!========"); }}package com.pb.lnk;/** * 灰色墨盒 * @author Administrator * */public class Grey implements Lnk { @Override public void getColor() { System.out.println("--------使用灰色墨盒打印!---------"); }}
測(cè)試類(lèi)
package com.pb.lnk;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 墨盒測(cè)試類(lèi) * * @author Administrator * */public class LnkTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Lnk k=null; k=(Lnk) context.getBean("color"); k.getColor(); k=(Lnk) context.getBean("grey"); k.getColor(); }}
applicationContext.xml
<bean id="color" class="com.pb.lnk.Color"></bean><bean id="grey" class="com.pb.lnk.Grey"></bean>6.2、單態(tài)模式支持
package com.pb.lnk;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 單態(tài)模式(單實(shí)例模式) * @author Administrator * */public class LnkTest2 { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Lnk lnk1=null; Lnk lnk2=null; lnk1=(Lnk)context.getBean("color"); lnk2=(Lnk)context.getBean("color"); System.out.println(lnk1==lnk2); //結(jié)果是true表示2個(gè)是同一個(gè)對(duì)象 }}七、Spring核心機(jī)制
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注