spring--ioc
spring–ioc
1. 什么是spring,它能夠做什么?
Spring是一個(gè)開(kāi)源框架,它由Rod Johnson創(chuàng)建。它是為了解決企業(yè)應(yīng)用開(kāi)發(fā)的復(fù)雜性而創(chuàng)建的。
Spring使用基本的JavaBean來(lái)完成以前只可能由EJB完成的事情。
然而,Spring的用途不僅限于服務(wù)器端的開(kāi)發(fā)。從簡(jiǎn)單性、可測(cè)試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。
目的:解決企業(yè)應(yīng)用開(kāi)發(fā)的復(fù)雜性
功能:使用基本的JavaBean代替EJB,并提供了更多的企業(yè)應(yīng)用功能
范圍:任何Java應(yīng)用
簡(jiǎn)單來(lái)說(shuō),Spring是一個(gè)輕量級(jí)的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架。
struts2
spring
hibernate
JavaBean 項(xiàng)目中的一個(gè)個(gè)類(lèi)
IOC和AOP
2. 什么是控制反轉(zhuǎn)(或依賴(lài)注入)
控制反轉(zhuǎn)(IoC=Inversion of Control)IoC,用白話(huà)來(lái)講,就是由容器控制程序之間的(依賴(lài))關(guān)系,而非傳統(tǒng)實(shí)現(xiàn)中,由程序代碼直接操控。這也就是所謂“控制反轉(zhuǎn)”的概念所在:(依賴(lài))控制權(quán)由應(yīng)用代碼中轉(zhuǎn)到了外部容器,控制權(quán)的轉(zhuǎn)移,是所謂反轉(zhuǎn)。
IoC還有一個(gè)另外的名字:“依賴(lài)注入 (DI=Dependency Injection)” ,即由容器動(dòng)態(tài)的將某種依賴(lài)關(guān)系注入到組件之中
案例:實(shí)現(xiàn)Spring的IoC
IOC/DI
將以前由程序員實(shí)例化對(duì)象/賦值的工作交給了spring處理
3. 如何在spring當(dāng)中定義和配置一個(gè)JavaBean(使用無(wú)參構(gòu)造方法+set方法創(chuàng)建一個(gè)JavaBean)
4.1 singleton(單例模式):在每個(gè)Spring IoC容器中一個(gè)bean定義對(duì)應(yīng)一個(gè)對(duì)象實(shí)例
4.2 prototype(原型模式/多例模式):一個(gè)bean定義對(duì)應(yīng)多個(gè)對(duì)象實(shí)例
注1:struts2的Action請(qǐng)使用多例模式
4. 簡(jiǎn)單屬性的配置:
8基礎(chǔ)數(shù)據(jù)&String+3個(gè)sql
java.util.Date
java.sql.Date
java.sql.Time
java.sql.Timestamp
通過(guò)標(biāo)簽賦值即可
5. 復(fù)雜屬性的配置
ref bean=""
6. 針對(duì)項(xiàng)目,配置文件路徑的2種寫(xiě)法
ApplicationContextString path = "applicationContext.xml";String path = "classpath:applicationContext-*.xml";//srcString[] path = new String[] { "applicationContext-a.xml", "applicationContext-b.xml" };//分模塊開(kāi)發(fā)7. spring與web項(xiàng)目的集成
WEB項(xiàng)目如何讀取spring上下文通過(guò)監(jiān)聽(tīng)器實(shí)現(xiàn)ServletContextListenercontextConfigLocation:classpath:applicationContext-*.xml8. log4j2
9. spring.pom
spring-context
spring-orm
spring-web
spring-aspects
注:創(chuàng)建spring的XML文件時(shí),需要添加beans/aop/tx/context標(biāo)簽支持
簡(jiǎn)介
spring-context.xml
ioc
set注入
基本數(shù)據(jù)類(lèi)型注入
集合注入
對(duì)象注入
構(gòu)造注入
基本數(shù)據(jù)類(lèi)型注入
自動(dòng)裝配
tomcat整合ioc容器
1、spring tool suite官方下載地址:http://spring.io/tools/sts/all
2、很詳細(xì)的網(wǎng)文在線安裝介紹:http://www.cnblogs.com/liuyungao/p/6213997
pom.xml
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lrc</groupId><artifactId>spring</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>spring Maven Webapp</name><url>http://maven.apache.org</url><properties><spring.version>5.0.1.RELEASE</spring.version><javax.servlet.version>4.0.0</javax.servlet.version><junit.version>4.12</junit.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!-- 2、導(dǎo)入spring依賴(lài) --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>${spring.version}</version></dependency><!-- 5.1、junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><!-- 5.2、servlet --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>${javax.servlet.version}</version><scope>provided</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.10.RELEASE</version></dependency></dependencies><build><finalName>spring</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.7.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build> </project>spring-context.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" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://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"><!-- id:在容器中查找Bean的id(唯一、且不能以/開(kāi)頭)class:bean的完整類(lèi)名name:在容器中查找Bean的名字(唯一、允許以/開(kāi)頭、允許多個(gè)值,多個(gè)值之間用逗號(hào)或空格隔開(kāi))scope:(singleton|prototype)默認(rèn)是singletonsingleton(單例模式):在每個(gè)Spring IoC容器中一個(gè)bean定義對(duì)應(yīng)一個(gè)對(duì)象實(shí)例prototype(原型模式/多例模式):一個(gè)bean定義對(duì)應(yīng)多個(gè)對(duì)象實(shí)例abstract:將一個(gè)bean定義成抽象bean(抽象bean是不能實(shí)例化的),抽象類(lèi)一定要定義成抽象bean,非抽象類(lèi)也可以定義成抽象beanparent:指定一個(gè)父bean(必須要有繼承關(guān)系才行)init-method:指定bean的初始化方法constructor-arg:使用有參數(shù)構(gòu)造方法創(chuàng)建javaBean --><!-- default-autowire="byType" --><!-- byName按name去進(jìn)行自動(dòng)裝配 --><bean id="userBiz" class="com.LHJ.ioc.biz.impl.UserBizImpl1"></bean><bean id="userAction" class="com.LHJ.ioc.web.UserAction"><!-- <property name="uid" value="22"></property> <property name="uname" value="LHJ"></property> --><property name="userBiz" ref="userBiz"></property><constructor-arg name="uid" value="22"></constructor-arg><constructor-arg name="uname" value="LHJ"></constructor-arg><property name="hobby"><list><value>唱</value><value>跳</value><value>rap</value></list></property></bean> </beans>UserBiz
package com.lrc.ioc.biz;/*** 通過(guò)企業(yè)案例來(lái)講解使用spring ioc的必要性* v1.0:實(shí)現(xiàn)游戲上傳功能* v2.0:對(duì)游戲上傳功能進(jìn)行優(yōu)化* ioc的具體體現(xiàn)* @author MACHENIKE**/ public interface UserBiz {public void upload(); }UserBizImpl1
package com.lrc.ioc.biz;import com.lrc.ioc.biz.UserBiz;public class UserBizImpl1 implements UserBiz {@Overridepublic void upload() {System.out.println("實(shí)現(xiàn)游戲上傳的功能");}}UserBizImpl2
package com.lrc.ioc.biz;import com.lrc.ioc.biz.UserBiz;public class UserBizImpl2 implements UserBiz {@Overridepublic void upload() {System.out.println("實(shí)現(xiàn)游戲上傳的功能");}}UserAction
package com.lrc.ioc.web;import java.util.List;import com.lrc.ioc.biz.UserBiz;/*** IOC的注入方式及各類(lèi)類(lèi)型 set注入 基本類(lèi)型與String 數(shù)組 自定義類(lèi)型 構(gòu)造注入 自動(dòng)裝配* * 構(gòu)造注入 自動(dòng)裝配 spring4之后出現(xiàn)的 byType:根據(jù)配置的Bean中的接口,在Spring的上下文中尋找對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)* byName:根據(jù)配置的Bean中的接口名字,在Spring的上下文中尋找對(duì)應(yīng)的實(shí)現(xiàn)類(lèi)**/ public class UserAction {private UserBiz userBiz;private String uname;private int age;private List<String> hobby;public UserAction() {super();// TODO Auto-generated constructor stub}public UserAction(String uname, int age) {super();this.uname = uname;this.age = age;}public UserBiz getUserBiz() {return userBiz;}public void setUserBiz(UserBiz userBiz) {this.userBiz = userBiz;}public List<String> getHobby() {return hobby;}public void setHobby(List<String> hobby) {this.hobby = hobby;}public void upload() {userBiz.upload();}/*** 注入問(wèn)題*/public void test1() {System.out.println(this.uname);System.out.println(this.age);System.out.println(this.hobby);}}IocTest
package com.lrc.ioc.test;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import com.lrc.ioc.web.UserAction;public class IocTest {public static void main(String[] args) {// UserAction userAction=new UserAction();// userAction.upload();ApplicationContext springContext = new ClassPathXmlApplicationContext("/spring-context.xml");UserAction userAction = (UserAction) springContext.getBean("xxx");OrderAction orderAction = (OrderAction) springContext.getBean("jy");userAction.upload();orderAction.upload();// userAction.test1();} }創(chuàng)建一個(gè)監(jiān)聽(tīng)器SpringLoaderListener
package com.lrc.ioc.listener;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作為管理整個(gè)工程中的所有Javabean,那么如何在用戶(hù)發(fā)送請(qǐng)求的時(shí)候能夠訪問(wèn)到 處理方式:* 在監(jiān)聽(tīng)器中將spring的上下文交給tomcat的上下文中進(jìn)行管理* 瀏覽器--》request-->servletContext-->springContext-->任意的javaBean* **/ @WebListener public class SpringLoaderListener implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("tomcat一啟動(dòng)就觸發(fā)了...");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);}}UserServlet
package com.lrc.ioc.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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("處理用戶(hù)請(qǐng)求");ApplicationContext springContext = (ApplicationContext) req.getServletContext().getAttribute("spring_key");UserAction userAction = (UserAction) springContext.getBean("xxx");userAction.upload();} }配置web.xml
<?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"version="3.1"><display-name>Archetype Created Web Application</display-name><context-param><param-name>springXmlLocation</param-name><param-value>/spring-other.xml</param-value></context-param> </web-app>總結(jié)
以上是生活随笔為你收集整理的spring--ioc的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FFmpeg 基础库(二)音频格式
- 下一篇: 关于如何卸载IE11恢复IE8