Spring入门
2017-12-02 本文已影响7人
CoderHong

Spring概述:
Spring是一个开源的框架。为了解决企业应用开发的复杂性而创建的。框架的主要优势就是分层架构。
Spring的核心 控制反转-IoC、面向切面-AOP
1. Spring入门:
1.1 下载jar包
1.2 创建web项目,引入Spring必须的jar包

1.3 引入配置文件
在src目录下首先引入log4j.properties
跟applicationContext.XML
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
1.4 编写相关类
public interface UserService {
public void sayHello();
}
public class UserServiceImpl implements UserService {
public void sayHello() {
System.out.println("--------helloworld---name="+name);
}
}
1.5 完成配置

1.6 编写测试程序
// 1. 创建工厂 加载核心配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 从工厂中获取对象 getBean(配置文件的id值);
UserService usi = (UserServiceImpl) ac.getBean("userService");
2. Spring相关配置
- id属性
- Bean起的别名,在约束中采用ID唯一。
- scope
- Bean的作用范围
- singleton 默认值单例
-
prototype 多例
- Spring管理Action是必须为多例
- Bean的作用范围

3. Spring的Bean属性注入
构造方法注入
编写测试类 提供构造方法

编写配置文件

set方法注入

对象类型注入

注意:注入不要忘记写属性的set方法
复杂类型注入
List集合属性

Map集合属性

4. Spring整合WEB项目
4.1 创建WEB项目,引入jar包
- WEB层使用Struts2
- Struts2开发的基本jar包
- Spring进行Bean管理
- Spring开发的基本Jar包
4.2 引入配置文件
struts2;
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<!-- 千万不能忘记配置前段控制器 -->
<filter>
<filter-name>struts2</filter-name>
<!-- 核心过滤器的全路径 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
- struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 包结构 -->
<package name="default" namespace="/" extends="struts-default">
<!-- 配置UserAction -->
<action name="user_*" class="UserAction" method="{1}"></action>
</package>
</struts>
新建一个index.jsp 提交访问Action
<a href="${pageContext.request.contextPath }/user_save.action">保存客户</a>
编写一个UserAction类:继承ActionSupport
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String save() {
/ 在Action中获取Spring中的UserService
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userService");
us.save(new User());
}
}
每次请求都会创建一个工厂类,服务端的资源就浪费了,一般情况下一个工程只有一个Spring工厂类就OK了
- 工厂在服务器启动的时候创建好,将这个工厂放入到ServletContext域中,每次获取工厂从ServletContext域中进行获取。
ServletContextLinstener : 监听ServletContext对象的额创建和销毁。

引入Spring-web.jar包

在web.xml配置前端监听器
<!-- 配置Spring框架整合WEB的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 默认只能加载WEB——INF目录下的配置文件,提供配置方式,加载Src目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
改写优化UserAction
public String save() {
System.out.println("------------save---------");
/*
// 在Action中获取Spring中的UserService
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService us = (UserService) ac.getBean("userService");
us.save(new User());
*/
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());
UserService us = (UserService) webApplicationContext.getBean("userService");
System.out.println(us);
us.save(new User());
return NONE;
}