1.什么是spring,它能够做什么?
Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。
目的:解决企业应用开发的复杂性
功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
2.什么是控制反转(或依赖注入)
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
案例:实现Spring的IoC
IOC/DI
将以前由程序员实例化对象/赋值的工作交给了spring处理
3.如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)
3.1 id:在容器中查找Bean的id(唯一、且不能以/开头)
3.2 class:bean的完整类名
3.3 name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
3.4 scope:(singleton|prototype)默认是singleton
3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
3.4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
3.4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
3.5 parent:指定一个父bean(必须要有继承关系才行)
3.6 init-method:指定bean的初始化方法
3. 如何在spring当中定义和配置一个JavaBean(使用无参构造方法+set方法创建一个JavaBean)
3.1 id:在容器中查找Bean的id(唯一、且不能以/开头)
3.2 class:bean的完整类名
3.3 name:在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
3.4 scope:(singleton|prototype)默认是singleton
3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
3.4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
3.4 abstract:将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
3.5 parent:指定一个父bean(必须要有继承关系才行)
3.6 init-method:指定bean的初始化方法
3.7 constructor-arg:使用有参数构造方法创建javaBean
set注入
首先导入spring的相关依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
然后配置xml文件
<bean class="com.chenkang.ioc.biz.web.UserAction" id="xxx">
<!--set注入用 property标签 -->
<property name="userBiz" ref="userBiz"></property>
<property name="uname" value="chenkang"></property>
<property name="age" value="19"></property>
<property name="hobby">
<list>
<value>吃饭</value>
<value>睡觉</value>
<value>打王者</value>
</list>
</property>
建立UserAction类里面有自定义,数组,string,int数据类型
package com.chenkang.ioc.biz.web;
import java.util.List;
import com.chenkang.ioc.biz.UserBiz;
/**
* IOC的注入方式及各类值类型
* set注入
* 基本类型与string
* 数组
* 自定义类型
* 构造注入
* 自动装配
*
* @author Administrator
*
*/
public class UserAction {
private UserBiz userBiz;
private String uname;
private int age;
private List<String>hobby;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
public void upload() {
userBiz.upload();
}
/**
* 注入问题
*/
public void test1() {
System.out.println(this.uname);
System.out.println(this.age);
System.out.println(this.hobby);
}
}
测试代码
package com.chenkang.ioc.biz.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.chenkang.ioc.biz.web.UserAction;
public class IocTest {
public static void main(String[] args) {
ApplicationContext springContext = new ClassPathXmlApplicationContext("/spring-context.xml");
UserAction userAction = (UserAction) springContext.getBean("xxx");
userAction.test1();
}
}
结果:
构造注入:
在UserAction写一个无参构造和有参构造
package com.chenkang.ioc.biz.web;
import java.util.List;
import com.chenkang.ioc.biz.UserBiz;
/**
* IOC的注入方式及各类值类型
* set注入
* 基本类型与string
* 数组
* 自定义类型
* 构造注入
* 自动装配
*
* @author Administrator
*
*/
public class UserAction {
private UserBiz userBiz;
private String uname;
private int age;
private List<String>hobby;
public UserAction() {
super();
}
public UserAction(String uname, int age) {
super();
this.uname = uname;
this.age = age;
}
public List<String> getHobby() {
return hobby;
}
public void setHobby(List<String> hobby) {
this.hobby = hobby;
}
public UserBiz getUserBiz() {
return userBiz;
}
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
public void upload() {
userBiz.upload();
}
/**
* 注入问题
*/
public void test1() {
System.out.println(this.uname);
System.out.println(this.age);
System.out.println(this.hobby);
}
}
然后在xml文件进行配置
<!--构造器注入用 constructor-arg标签 -->
<constructor-arg name="uname" value="chenkang111"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
<property name="hobby">
<list>
<value>吃饭</value>
<value>睡觉</value>
<value>打王者</value>
</list>
</property>
运行代码和运行结果还是一样的我就不展示了哈!!
自动装配
在xml文件中添加
default-autowire="byType"
就算没有配置<!-- <property name="userBiz" ref="userBiz"></property> -->
他自己也能配置成功!
default-autowire="byName"
要与<bean class="com.chenkang.ioc.biz.Impl.UserBizImpl1" id="userBiz"></bean>
代码中的id名字一样!
spring与tomcat的运用
监听文件
在运行tomcat的时候就会执行这个类 主要是因为加了@WebListener
package com.chenkang.ioc.biz.listenter;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* spring 作为管理整个工程中的所有的javabean,那么如何在用户发送请求的时候能够访问到指定的javabean
* 处理方式:
* 在监听器中将spring的上下文交给Tomcat的上下文进行管理
* 浏览器-->request-->ServletContext-->springContext-->任意的javaBean
* @author Administrator
*
*/
@WebListener
public class SpringLoaderListenter implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Tomcat一启动就触发了。。");
ServletContext tomcatContext = sce.getServletContext();
String springXmlLocation = tomcatContext.getInitParameter("springXmlLocation");
System.out.println("spring的上下文配置文件:"+springXmlLocation);
ApplicationContext springContext = null;
if(springXmlLocation == null || "".equals(springXmlLocation)) {
springContext = new ClassPathXmlApplicationContext("/spring-context.xml");
}else {
springContext = new ClassPathXmlApplicationContext(springXmlLocation);
}
tomcatContext.setAttribute("spring_key", springContext);
}
}
在上面就已经将springContext加入到了servletContext中
所以就可以直接获取到springContext的键值对。
package com.chenkang.ioc.biz.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
@WebServlet("/user")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("处理用户请求");
ApplicationContext springContext= (ApplicationContext) req.getServletContext().getAttribute("spring_key");
UserAction userAction = (UserAction) springContext.getBean("xxx");
userAction.upload();
}
}