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)
<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置bean -->
<bean id="welcomeService" class="cn.org.yinzhengjie.spring.service.WelcomeServiceImpl">
<property name="name" value="I'm YinZhengJie" />
<!-- 注意,属性标签中的ref表示的是引用的意思,我们这里指向了下面的那个“byeService”标签。 -->
<property name="bs" ref="byeService" />
</bean>
<bean id="byeService" class="cn.org.yinzhengjie.spring.service.ByeServiceImpl">
<property name="bye" value="byebye" />
</bean>
</beans>
3>.设计类WelcomeService.java和ByeService.java文件内容如下
接口文件内容如下:
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.service; public interface WelcomeService {
public abstract void sayHello();
}
WelcomeService.java (接口文件内容)
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.service; public interface ByeService {
public abstract void sayBye();
}
ByeService.java (接口文件内容)
实现类文件内容如下:
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.service; public class ByeServiceImpl implements ByeService {
private String bye ; public String getBye() {
return bye;
} public void setBye(String bye) {
this.bye = bye;
} public void sayBye() {
System.out.println(bye);
}
}
ByeServiceImpl.java (实现类文件内容)
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.service; public class WelcomeServiceImpl implements WelcomeService {
private String name ;
private ByeService bs ; public ByeService getBs() {
return bs;
} public void setBs(ByeService bs) {
this.bs = bs;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void sayHello() {
bs.sayBye();
System.out.println(name);
}
}
WelcomeServiceImpl.java (实现类文件内容)
4>.测试类代码如下:
/*
@author :yinzhengjie
Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/
EMAIL:y1053419035@qq.com
*/
package cn.org.yinzhengjie.spring.service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo {
public static void main(String[] args) {
//加载类的路径XMl配置文件,实例化容器。得到ApplicationContext的对象ac
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
//通过该ac的getBean方法获取到配置文件中的bean标签的指定id为"welcomeService"的对象,由于返回的是Object对象,因此我们需要强转一下。
WelcomeService ws = (WelcomeService) ac.getBean("welcomeService");
//调用ws的实现方法。
ws.sayHello();
ByeService bs = (ByeService) ac.getBean("byeService");
bs.sayBye();
}
}
运行以上代码执行结果如下: