Spring之Spring环境搭建

时间:2023-03-09 04:15:06
Spring之Spring环境搭建

Spring之Spring环境搭建

一、什么是Spring?

  Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。
    ◆目的:解决企业应用开发的复杂性
    ◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
    ◆范围:任何Java应用
  Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架。

  1、Spring的核心是一个轻量级(Lightweight)的容器(Container)。
  2、Spring是实现IoC(Inversion of Control)容器和非入侵性(No intrusive)的框架。
  3、Spring提供AOP(Aspect-oriented programming)概念的实现方式。
  4、Spring提供对持久层(Persistence)、事物(Transcation)的支持。
  5、Spring供MVC Web框架的实现,并对一些常用的企业服务API(Application Interface)提供一致的模型封装。
  6、Spring提供了对现存的各种框架(Structs、JSF、Hibernate、Ibatis、Webwork等)相整合的方案。

二、Spring之JavaSE环境搭建

 1、基本的Jar 包:

  commons-logging-1.2.jar
  spring-aop-4.3.0.RELEASE.jar
  spring-aspects-4.3.0.RELEASE.jar
  spring-beans-4.3.0.RELEASE.jar
  spring-context-4.3.0.RELEASE.jar
  spring-core-4.3.0.RELEASE.jar
  spring-expression-4.3.0.RELEASE.jar

 2、项目目录结构

Spring之Spring环境搭建

3、源代码

①、HelloWorld.java(POJO)

 package cn.com.zfc.helloworld;

 public class HelloWorld {

     // 私有属性
private String name; public HelloWorld() {
System.out.println("HelloWorld Constructor()...");
} public void sayHello() {
System.out.println("Hello " + this.name);
} public String getName() {
System.out.println("HelloWorld getName()...");
return name;
} public void setName(String name) {
System.out.println("HelloWorld setName()...");
this.name = name;
} }

②、bean-helloworld.xml(Spring配置文件)

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 配置bean:id,bean的唯一标识;class,bean实现的实现类 -->
<bean id="helloWorld" class="cn.com.zfc.helloworld.HelloWorld">
<!-- 使用属性注入 -->
<property name="name" value="Spring"></property>
</bean>
</beans>

③、TestHelloWorld.java(测试类)

 package cn.com.zfc.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.com.zfc.helloworld.HelloWorld; public class TestHelloWorld {
public static void main(String[] args) {
/* 1、获取Ioc容器 */
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-helloworld.xml"); /* 2、获取bean */ //根据id获取bean
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld"); /* 3、调用bean的方法 */
helloWorld.sayHello();
}
}

④、运行效果

Spring之Spring环境搭建

二、Spring之JavaEE环境搭建

1、基本Jar包

  commons-logging-1.2.jar
  spring-aop-4.3.0.RELEASE.jar
  spring-beans-4.3.0.RELEASE.jar
  spring-context-4.3.0.RELEASE.jar
  spring-core-4.3.0.RELEASE.jar
  spring-expression-4.3.0.RELEASE.jar
  spring-web-4.3.0.RELEASE.jar

2、目录结构

Spring之Spring环境搭建

3、源代码

①HelloWorld.java(POJO)

 package cn.com.zfc.bean;

 public class HelloWord {
private String name; public HelloWord() {
super();
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "HelloWord [name=" + name + "]";
} }

②web.xml(注册Spring的配置文件)

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Spring_02</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 注册 Spring 配置文件 -->
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- Spring 配置文件的位置 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器,加载 Spring 配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

③applicationContext.xml(Spring的配置文件)

 <?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"
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.3.xsd">
<!-- 配置 bean -->
<bean id="" class="cn.com.zfc.bean.HelloWord">
<!-- 使用属性注入为 HelloWorld 的属性赋值 -->
<property name="name" value="zfc"></property>
</bean> </beans>

④index.jsp(测试页面)

 <%@page import="cn.com.zfc.bean.HelloWord"%>
<%@page import="org.springframework.context.ApplicationContext"%>
<%@page
import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring</title>
</head>
<body>
<h1>Spring 在 web 项目中的应用</h1>
<%
/* 在 jsp 页面中获取 Spring 的 Ioc 容器 */
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(application);
/* 获取 bean,使用类型 */
HelloWord helloWord = ctx.getBean(HelloWord.class);
%>
<h2>
在 jsp 中获取 bean:<%=helloWord%></h2>
</body>
</html>

4、运行效果

Spring之Spring环境搭建