日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring--ioc

發布時間:2024/3/26 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring--ioc 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring–ioc

1. 什么是spring,它能夠做什么?

Spring是一個開源框架,它由Rod Johnson創建。它是為了解決企業應用開發的復雜性而創建的。
Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。
然而,Spring的用途不僅限于服務器端的開發。從簡單性、可測試性和松耦合的角度而言,任何Java應用都可以從Spring中受益。
目的:解決企業應用開發的復雜性
功能:使用基本的JavaBean代替EJB,并提供了更多的企業應用功能
范圍:任何Java應用
簡單來說,Spring是一個輕量級的控制反轉(IoC)和面向切面(AOP)的容器框架。

  • 中間層框架、萬能膠
    struts2
    spring
    hibernate
  • 容器框架
    JavaBean 項目中的一個個類
    IOC和AOP
  • 2. 什么是控制反轉(或依賴注入)

    控制反轉(IoC=Inversion of Control)IoC,用白話來講,就是由容器控制程序之間的(依賴)關系,而非傳統實現中,由程序代碼直接操控。這也就是所謂“控制反轉”的概念所在:(依賴)控制權由應用代碼中轉到了外部容器,控制權的轉移,是所謂反轉。
    IoC還有一個另外的名字:“依賴注入 (DI=Dependency Injection)” ,即由容器動態的將某種依賴關系注入到組件之中
    案例:實現Spring的IoC

    IOC/DI
    將以前由程序員實例化對象/賦值的工作交給了spring處理

    3. 如何在spring當中定義和配置一個JavaBean(使用無參構造方法+set方法創建一個JavaBean)

  • id:在容器中查找Bean的id(唯一、且不能以/開頭)
  • class:bean的完整類名
  • name:在容器中查找Bean的名字(唯一、允許以/開頭、允許多個值,多個值之間用逗號或空格隔開)
  • scope:(singleton|prototype)默認是singleton
    4.1 singleton(單例模式):在每個Spring IoC容器中一個bean定義對應一個對象實例
    4.2 prototype(原型模式/多例模式):一個bean定義對應多個對象實例
  • abstract:將一個bean定義成抽象bean(抽象bean是不能實例化的),抽象類一定要定義成抽象bean,非抽象類也可以定義成抽象bean
  • parent:指定一個父bean(必須要有繼承關系才行)
  • init-method:指定bean的初始化方法
  • constructor-arg:使用有參數構造方法創建javaBean
  • 注1:struts2的Action請使用多例模式

    4. 簡單屬性的配置:

    8基礎數據&String+3個sql
    java.util.Date
    java.sql.Date
    java.sql.Time
    java.sql.Timestamp
    通過標簽賦值即可

    5. 復雜屬性的配置

  • JavaBean
    ref bean=""
  • List或數組
  • Map
  • Properties
  • 6. 針對項目,配置文件路徑的2種寫法

    ApplicationContextString path = "applicationContext.xml";String path = "classpath:applicationContext-*.xml";//srcString[] path = new String[] { "applicationContext-a.xml", "applicationContext-b.xml" };//分模塊開發

    7. spring與web項目的集成

    WEB項目如何讀取spring上下文通過監聽器實現ServletContextListenercontextConfigLocation:classpath:applicationContext-*.xml

    8. log4j2

    9. spring.pom

    spring-context
    spring-orm
    spring-web
    spring-aspects
    注:創建spring的XML文件時,需要添加beans/aop/tx/context標簽支持

    <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.3.10.RELEASE</version> </dependency>

    簡介
    spring-context.xml
    ioc
    set注入
    基本數據類型注入
    集合注入
    對象注入
    構造注入
    基本數據類型注入
    自動裝配

    tomcat整合ioc容器

    1、spring tool suite官方下載地址:http://spring.io/tools/sts/all

    2、很詳細的網文在線安裝介紹: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、導入spring依賴 --><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(唯一、且不能以/開頭)class:bean的完整類名name:在容器中查找Bean的名字(唯一、允許以/開頭、允許多個值,多個值之間用逗號或空格隔開)scope:(singleton|prototype)默認是singletonsingleton(單例模式):在每個Spring IoC容器中一個bean定義對應一個對象實例prototype(原型模式/多例模式):一個bean定義對應多個對象實例abstract:將一個bean定義成抽象bean(抽象bean是不能實例化的),抽象類一定要定義成抽象bean,非抽象類也可以定義成抽象beanparent:指定一個父bean(必須要有繼承關系才行)init-method:指定bean的初始化方法constructor-arg:使用有參數構造方法創建javaBean --><!-- default-autowire="byType" --><!-- byName按name去進行自動裝配 --><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;/*** 通過企業案例來講解使用spring ioc的必要性* v1.0:實現游戲上傳功能* v2.0:對游戲上傳功能進行優化* ioc的具體體現* @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("實現游戲上傳的功能");}}

    UserBizImpl2

    package com.lrc.ioc.biz;import com.lrc.ioc.biz.UserBiz;public class UserBizImpl2 implements UserBiz {@Overridepublic void upload() {System.out.println("實現游戲上傳的功能");}}

    UserAction

    package com.lrc.ioc.web;import java.util.List;import com.lrc.ioc.biz.UserBiz;/*** IOC的注入方式及各類類型 set注入 基本類型與String 數組 自定義類型 構造注入 自動裝配* * 構造注入 自動裝配 spring4之后出現的 byType:根據配置的Bean中的接口,在Spring的上下文中尋找對應的實現類* byName:根據配置的Bean中的接口名字,在Spring的上下文中尋找對應的實現類**/ 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();}/*** 注入問題*/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();} }

    創建一個監聽器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作為管理整個工程中的所有Javabean,那么如何在用戶發送請求的時候能夠訪問到 處理方式:* 在監聽器中將spring的上下文交給tomcat的上下文中進行管理* 瀏覽器--》request-->servletContext-->springContext-->任意的javaBean* **/ @WebListener public class SpringLoaderListener implements ServletContextListener {@Overridepublic 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);}}

    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("處理用戶請求");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>

    總結

    以上是生活随笔為你收集整理的spring--ioc的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。