Spring入门之ioc

Spring是什么?

Spring是一个开源框架,它由Rod Johnson创建。它是为了解决企业应用开发的复杂性而创建的。
Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
什么是控制反转
控制反转(IoC=Inversion of Control)IoC,用白话来讲,就是由容器控制程序之间的(依赖)关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在:(依赖)控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
IoC还有一个另外的名字:“依赖注入 (DI=Dependency Injection)” ,即由容器动态的将某种依赖关系注入到组件之中
应用场景:
当需求变化非常快的时候,不便于维护,因为维护的权利是属于程序员的,spring的ioc角色解决这一问题的,将维护代码的权力由程序员转教给spring容器来完成

Spring管理Bean(配置文件参数)

参数解释
id在容器中查找Bean的id(唯一、且不能以/开头)
classbean的完整类名
name在容器中查找Bean的名字(唯一、允许以/开头、允许多个值,多个值之间用逗号或空格隔开)
abstract将一个bean定义成抽象bean(抽象bean是不能实例化的),抽象类一定要定义成抽象bean,非抽象类也可以定义成抽象bean
parent指定一个父bean(必须要有继承关系才行)
init-method指定bean的初始化方法
constructor-arg使用有参数构造方法创建javaBean
 scope:(singleton|prototype)默认是singleton
     3.4.1 singleton(单例模式):在每个Spring IoC容器中一个bean定义对应一个对象实例
     3.4.2 prototype(原型模式/多例模式):一个bean定义对应多个对象实例
   注1:struts2的Action请使用多例模式

set注入

----基本数据类型
----引用数据类型
主要通过set方法来给找到配置文件注入值

package com.liuchunming.web;

import java.util.List;

import com.liuchunming.ioc.biz.UserBiz;

/**
 * 
 * IOC的注入方式及各类类型
 *  set注入
 *  	基本类型与String
 *  	数组
 *  	自定义类型
 *  
 *
 */
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;
	}*/
	/**
	 * @param uname
	 * @param age
	 */
	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();
	}
	
	/**
	 * set注入
	 */
	public void test1() {
		System.out.println(this.uname);
		System.out.println(this.age);
		System.out.println(this.hobby);
	}
	
}

然后spring的配置文件,对应的注入方式配置好property,然后我们就可以在其他类中调用test1输出属性值
引用注入就是把实体类直接注给我们拿配置的另外的类或接口,然后利用ref属性配置名字

<?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: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.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">
       
       <import resource="/spring-a.xml"/>
       <import resource="/spring-b.xml"/>
       
       <bean class="com.liuchunming.impl.UserBizImpl1" id="userBiz"></bean>
       <bean class="com.liuchunming.web.UserAction" id="xxx">
       <!-- set注入用property标签  -->
       		<property name="userBiz" ref="userBiz"></property>
       		<property name="uname" value="zs"></property>
       		<property name="age" value="22"></property> 
       		<!-- 构造器注入用constructor-arg标签  
       		<constructor-arg name="uname" value="ls"></constructor-arg>
       		<constructor-arg name="age" value="22"></constructor-arg>-->
       		<property name="hobby">
       			<list>
       				<value>篮球</value>
       				<value>RAP</value>
       				<value>广场舞</value>
       			</list>
       		</property>
       </bean>
       
       <bean class="com.liuchunming.web.OrderAction" id="ttt">
       		<property name="userBiz" ref="userBiz"></property>
       </bean>
</beans>

构造注入

通过构造函数来给配置文件注入值,实体类和上面是一样的,我们可以把set方法注释掉,然后为属性写上构造方法,再在配置文件中配好constructor-arg,就可以成功注入属性值了

			<constructor-arg name="uname" value="ls"></constructor-arg>
       		<constructor-arg name="age" value="22"></constructor-arg>

自动装配

byName是根据你配置的id来找到类
byType通过类型来配置,只会找到接口等,如果有两个类实现了同一个接口,那么就容易报错

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	default-autowire="byType"
	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.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 class="com.liuchunming.impl.UserBizImpl1" id="userBiz"></bean>
		 <bean class="com.liuchunming.impl.UserBizImpl2" id="userBiz"></bean>
</beans>

将spring交给tomact上下文管理

ApplicationContextListener

package ioc.util;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * spring作为管理整个工程中的所有javabean,那么如何在用户发送请求的时候能够访问到指定的javabean
 * 处理方式:
 * 	在监听器中将spring的上下文交给tomact的上下文进行管理
 * 	浏览器--》request--》servletCotext--》springContext--》任意JavaBean
 *
 */
public class ApplicationContextListener implements ServletContextListener {

	private String path = "spring.xml";

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		System.out.println("tomcat启动就触发");
		ServletContext application = sce.getServletContext();

		String path = application.getInitParameter("contextConfigLocation");
		if (null != path && !"".equals(path.trim())) {
			this.path = path;
		}
		ApplicationContext context = new ClassPathXmlApplicationContext(this.path);

//		 application.setAttribute("abcd", context);
		 SpringWebUtil.setContext(application, context);
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	}
}

SpringWebUtil
两个方法get上下文与set上下文

package ioc.util;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;

public class SpringWebUtil {

	public static final String CONTEXT_KEY = "ssssssssssssssssssssssssfffff";

	private SpringWebUtil() {
	}

	public static void setContext(ServletContext application, ApplicationContext context) {
		application.setAttribute(CONTEXT_KEY, context);
	}

	public static ApplicationContext getContext(ServletContext application) {
		return (ApplicationContext) application.getAttribute(CONTEXT_KEY);
	}
}

配置到Web.xml

<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_3_0.xsd"
	id="WebApp_ID" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  
  <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>spring.xml</param-value>
	</context-param>

	<listener>
		<listener-class>ioc.util.ApplicationContextListener</listener-class>
	</listener>
</web-app>

jsp界面

<%@page import="org.springframework.context.ApplicationContext"%>
<%@page import="ioc.util.SpringWebUtil"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	ApplicationContext applicationContext= SpringWebUtil.getContext(application);
%>

<%=  applicationContext.getBean("s1")%>
</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值