Java基礎-SSM之Spring快速入門篇java
做者:尹正傑
spring
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。apache
Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著做Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是爲了解決企業應用開發的複雜性而建立的。Spring使用基本的JavaBean來完成之前只可能由EJB完成的事情。然而,Spring的用途不只限於服務器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用均可以從Spring中受益。 簡單來講,Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器框架。服務器
1>.建立模塊,添加spring依賴框架
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.org.yinzhengjie</groupId> <artifactId>MySpring</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.5.RELEASE</version> </dependency> </dependencies> </project>
2>.編輯Spring的配置文件(beans.xml)maven
1 <?xml version="1.0"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.3.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 15 <!-- 配置bean --> 16 <bean id="welcomeService" class="cn.org.yinzhengjie.spring.service.WelcomeServiceImpl"> 17 <property name="name" value="I'm YinZhengJie" /> 18 <!-- 注意,屬性標籤中的ref表示的是引用的意思,咱們這裏指向了下面的那個「byeService」標籤。 --> 19 <property name="bs" ref="byeService" /> 20 </bean> 21 <bean id="byeService" class="cn.org.yinzhengjie.spring.service.ByeServiceImpl"> 22 <property name="bye" value="byebye" /> 23 </bean> 24 </beans>
3>.設計類WelcomeService.java和ByeService.java文件內容以下ide
接口文件內容以下:測試
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.spring.service; 7 8 public interface WelcomeService { 9 public abstract void sayHello(); 10 }
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.spring.service; 7 8 public interface ByeService { 9 public abstract void sayBye(); 10 }
實現類文件內容以下:this
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.spring.service; 7 8 public class ByeServiceImpl implements ByeService { 9 private String bye ; 10 11 public String getBye() { 12 return bye; 13 } 14 15 public void setBye(String bye) { 16 this.bye = bye; 17 } 18 19 public void sayBye() { 20 System.out.println(bye); 21 } 22 }
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.spring.service; 7 8 public class WelcomeServiceImpl implements WelcomeService { 9 private String name ; 10 private ByeService bs ; 11 12 public ByeService getBs() { 13 return bs; 14 } 15 16 public void setBs(ByeService bs) { 17 this.bs = bs; 18 } 19 20 public String getName() { 21 return name; 22 } 23 24 public void setName(String name) { 25 this.name = name; 26 } 27 28 public void sayHello() { 29 bs.sayBye(); 30 System.out.println(name); 31 } 32 }
4>.測試類代碼以下:spa
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 package cn.org.yinzhengjie.spring.service; 7 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.support.ClassPathXmlApplicationContext; 10 11 public class Demo { 12 public static void main(String[] args) { 13 //加載類的路徑XMl配置文件,實例化容器。獲得ApplicationContext的對象ac 14 ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); 15 //經過該ac的getBean方法獲取到配置文件中的bean標籤的指定id爲"welcomeService"的對象,因爲返回的是Object對象,所以咱們須要強轉一下。 16 WelcomeService ws = (WelcomeService) ac.getBean("welcomeService"); 17 //調用ws的實現方法。 18 ws.sayHello(); 19 ByeService bs = (ByeService) ac.getBean("byeService"); 20 bs.sayBye(); 21 } 22 }
運行以上代碼執行結果以下: