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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

spring_01概念及案例

發布時間:2025/3/15 编程问答 11 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring_01概念及案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

1.什么是IOC?  

  IOC概念:inverse of Controll,控制反轉,所謂控制反轉,就是把創建對象和維護對象關系的權利從程序中轉移到spring的容器中(applicationContext.xml),而程序本身不再維護

?

2.什么是di?

  dependency injection,依賴注入,di和IOC是一個概念,spring的設計者認為di等更能準確表達spring

?

3.學習框架,最主要的就是學習各個配置

?

4.spring層次圖?

  

?

5.初次相遇spring,我的看法:

  剛剛接觸spring,素問spring是一個非常強大的框架,現在一看果然,他能夠創建并管理幾乎所有的bean對象,這里的bean對象包括:domain,dao,javabean,service...

  它就像是一個工程師,協調各個框架(springMVC,struts,hibernate......),注入靈魂,從而創建出一個個偉大的項目,

?

6.搭建spring項目簡單步驟:

  1),導入jar包,spring.jar(這個包包含spring框架常用的包),common-logging.jar為日志包,前面兩個包為必須,其余包按照需求選擇導入

  

  2),在src目錄下建立文件applicationContext.xml,一般都是在這個目錄下配置,,并且名字為applicationContext.xml,部分開發人員也喜歡使用beans.xml這個名字

  3),根據項目需要創建相關的類,并且配置到配置文件applicationContext.xml文件中,配置到配置文件中的類必須滿足javabean類的規格

?

7.簡單案例,對比使用傳統方法和使用spring框架輸出"hello,愛華頓g":

  

package com.ahd.service;public class UserService {private String username;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public void sayHello(){System.out.println("hello "+username);} } Service類UserService類:

?

  src目錄下建立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"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-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 在容器文件中配置數據源(bean/entity/service/dao/pojo) --><!-- bean元素的作用是當spring框架加載的時候,spring會自動為bean類 UserService類的屬性設置值 id就是創建對象的對象名 --><bean id="userService" class="com.ahd.service.UserService"> <!—name對應java類中的屬性,value是賦值--!><property name="username"><value>愛華頓g</value></property></bean> </beans>

?

  測試類Test

package com.ahd.test;import static org.junit.Assert.*;import com.ahd.service.UserService;public class Test {@org.junit.Testpublic void test() {//不使用spring框架,使用傳統編程方法//1.創建對象UserService userService=new UserService();//2.設置屬性userService.setUsername("愛華頓g");//3.調用方法 userService.sayHello();}@org.junit.Testpublic void test1(){//使用spring來完成上面的流程//1.得到spring的容器對象ApplicationContextApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");//2.獲取bean對象UserService us=(UserService) ac.getBean("userService");//3.調用方法us.sayHello();} }

?

  結果截圖:

  

?

轉載于:https://www.cnblogs.com/aihuadung/p/10356252.html

總結

以上是生活随笔為你收集整理的spring_01概念及案例的全部內容,希望文章能夠幫你解決所遇到的問題。

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