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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

01Spring的helloworld程序

發(fā)布時(shí)間:2024/8/5 javascript 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 01Spring的helloworld程序 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一:Spring的helloworld程序(環(huán)境已搭建)

1.導(dǎo)入spring包后,勾選spring配置文件

2.創(chuàng)建Dao.testDao接口,創(chuàng)建它的實(shí)現(xiàn)類testDaolmpl,里面有個(gè)sayhello方法

package com.Dao; public interface testDao {public void sayhello(); } package com.Dao;public class testDaoImpl implements testDao{@Overridepublic void sayhello() {System.out.println("這是一個(gè)hello方法");} }

3.配置文件添加如下語句

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="test" class="com.Dao.testDaoImpl"></bean> </beans>

4.測(cè)試類

注意:目前有兩種參數(shù)通過getBean獲取對(duì)象

  • id的方式創(chuàng)建的對(duì)象與直接用字節(jié)碼創(chuàng)建的對(duì)象地址是一樣的
  • 多次在配置文件中用不同的id聲明同一個(gè)類會(huì)報(bào)錯(cuò)
  • package com.Test;import com.Dao.testDaoImpl; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @Author: ljh* @Date: 2022/3/5 15:30* @Description:*/ public class test {public static void main(String[] args) {//激活配置ClassPathXmlApplicationContext test = new ClassPathXmlApplicationContext("spring-config.xml");testDaoImpl testDao = (testDaoImpl) test.getBean("test");testDao.sayhello();testDaoImpl testDao1 = test.getBean(testDaoImpl.class);testDao1.sayhello();//進(jìn)行比較是否為同一地址testDaoImpl testDao2 = (testDaoImpl) test.getBean("test");System.out.println(testDao==testDao1);System.out.println(testDao1==testDao2);System.out.println(testDao==testDao2);//運(yùn)行結(jié)果 // 這是一個(gè)hello方法 // 這是一個(gè)hello方法 // true // true // true} }

    5.IOC的原理

  • 測(cè)試類:找到構(gòu)造器ClassPathXmlApplicationContext,解析xml文件
  • xml文件:解析到標(biāo)簽后將class通過反射進(jìn)行對(duì)象創(chuàng)建再保存到內(nèi)部的容器中。
  • 測(cè)試類:當(dāng)測(cè)試類使用getBean方法時(shí),從容器中返回這個(gè)對(duì)象
  • 6.默認(rèn)參數(shù)

    注意:在配置文件中聲明有參數(shù)的對(duì)象,實(shí)體類必須依賴setget方法

    Stdent類

    package com.Dao;public class Student {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +'}';} }

    配置文件

    <bean id="std" class="com.Dao.Student"><property name="id" value="191106044"></property><property name="name" value="damin"></property></bean>

    測(cè)試類:

    package com.Test;import com.Dao.Student; import org.springframework.context.support.ClassPathXmlApplicationContext;public class test01 {public static void main(String[] args) {ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-config.xml");Student student = (Student) classPathXmlApplicationContext.getBean("std");System.out.println(student.toString());} }

    總結(jié)

    以上是生活随笔為你收集整理的01Spring的helloworld程序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。