Java基础-SSM之Spring快速入门篇
Java基础-SSM之Spring快速入门篇
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
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)
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文件内容如下
接口文件内容如下:
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 }
实现类文件内容如下:
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>.测试类代码如下:
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 }
运行以上代码执行结果如下:
本文来自博客园,作者:尹正杰,转载请注明原文链接:https://www.cnblogs.com/yinzhengjie/p/9288019.html,个人微信: "JasonYin2020"(添加时请备注来源及意图备注,有偿付费)
当你的才华还撑不起你的野心的时候,你就应该静下心来学习。当你的能力还驾驭不了你的目标的时候,你就应该沉下心来历练。问问自己,想要怎样的人生。