4、Spring之依賴註入

来源:https://www.cnblogs.com/Javaer1995/archive/2023/08/02/17583906.html
-Advertisement-
Play Games

> 依賴註入就是對類的屬性進行賦值 ## 4.1、環境搭建 > 創建名為spring_ioc_xml的新module,過程參考[3.1節](https://www.cnblogs.com/Javaer1995/p/17570068.html "3.1節") ### 4.1.1、創建spring配置文 ...


依賴註入就是對類的屬性進行賦值

4.1、環境搭建

創建名為spring_ioc_xml的新module,過程參考3.1節

4.1.1、創建spring配置文件

image

<?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">

</beans>

4.1.2、創建學生類Student

image

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/27 - 22:33
 */
public class Student {

    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, String sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

4.2、setter註入(常用)

4.2.1、配置bean

image

    <!--
        property標簽:通過組件類的setXxx()方法給組件對象設置屬性
            name屬性:指定屬性名
            value屬性:設置屬性值
    -->
    <bean id="student" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
    </bean>

4.2.2、測試

image

package org.rain.spring.test;

import org.junit.Test;
import org.rain.spring.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author liaojy
 * @date 2023/7/27 - 22:43
 */
public class IOCByXmlTest {

    @Test
    public void testDISetter(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("student", Student.class);
        System.out.println(student);
    }
}

4.3、構造器註入

4.3.1、配置bean

image

註意:constructor-arg標簽的數量,必須和某一個構造器方法的參數數量一致

    <!--
        constructor標簽:通過組件類的有參構造方法給組件對象設置屬性
            name屬性:指定有參構造方法參數名
            value屬性:設置有參構造方法參數值
    -->
    <bean id="studentTwo" class="org.rain.spring.pojo.Student">
        <constructor-arg name="id" value="1002"></constructor-arg>
        <constructor-arg name="name" value="李四"></constructor-arg>
        <constructor-arg name="age" value="24"></constructor-arg>
        <constructor-arg name="sex" value="女"></constructor-arg>
    </bean>

4.3.2、測試

image

    @Test
    public void testDIConstructor(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentTwo", Student.class);
        System.out.println(student);
    }

4.4、特殊值處理

4.4.1、null值

4.4.1.1、配置bean

image

註意:該<property name="sex" value="null">寫法,實際為sex所賦的值是字元串null

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <property name="name" value="張三"></property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

4.4.1.2、測試

image

由控制台日誌可知,此時sex的值為null

    @Test
    public void testDIspecial(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentThree", Student.class);
        System.out.println(student.getSex().toString());
    }

+++++++++++++++++++++++++++++++++++分割線+++++++++++++++++++++++++++++++++++

image

由控制台日誌可知,此時age的值也為null;所以不配置屬性也能實現同樣的效果

4.4.2、xml字元值

4.4.2.1、方式一:實體

image

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <!--用實體來代替xml字元-->
        <property name="name" value="&lt;張三&gt;"></property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

4.4.2.2、方式二:CDATA節

image

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <!-- CDATA中的C代表Character,是文本、字元的含義,CDATA就表示純文本數據 -->
        <!-- XML解析器看到CDATA節就知道這裡是純文本,就不會當作XML標簽或屬性來解析 -->
        <!-- 所以CDATA節中寫什麼符號都隨意 -->
        <property name="name">
            <value><![CDATA[<張三>]]></value>
        </property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

4.4.2.3、測試

image

4.5、為類類型的屬性賦值

4.5.1、方式一:外部bean(常用)

4.5.1.1、創建班級類Clazz

image

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/28 - 7:54
 */
public class Clazz {
    private Integer cid;
    private String cname;

    public Clazz() {
    }

    public Clazz(Integer cid, String cname) {
        this.cid = cid;
        this.cname = cname;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Clazz{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                '}';
    }
}

4.5.1.2、修改Student類

image

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/27 - 22:33
 */
public class Student {

    private Integer id;
    private String name;
    private Integer age;
    private String sex;

    private Clazz clazz;

    public Student() {
    }

    public Student(Integer id, String name, Integer age, String sex, Clazz clazz) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.clazz = clazz;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", clazz=" + clazz +
                '}';
    }
}

4.5.1.3、配置外部bean

image

    <bean id="clazz" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
    </bean>

4.5.1.4、引用外部bean

image

    <bean id="studentfour" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <!-- ref屬性:引用IOC容器中某個bean的id,將所對應的bean為屬性賦值 -->
        <property name="clazz" ref="clazz"></property>
    </bean>

4.5.1.5、測試

image

    @Test
    public void testDIOuterBean(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentfour", Student.class);
        System.out.println(student);
    }

4.5.2、方式二、級聯

4.5.2.1、配置bean

image

    <bean id="clazz" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
    </bean>


    <bean id="studentFive" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <!-- 一定先引用某個bean為屬性賦值,才可以使用級聯方式更新屬性 -->
        <property name="clazz" ref="clazz"></property>
        <property name="clazz.cid" value="2222"></property>
        <property name="clazz.cname" value="五年A班"></property>
    </bean>

4.5.2.2、測試

image

    @Test
    public void testDICascade(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentFive", Student.class);
        System.out.println(student);
    }

4.5.3、內部bean(常用)

4.5.3.1、配置bean

image

    <bean id="studentSix" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <!-- 內部bean只能用於給屬性賦值,不能在外部通過IOC容器獲取,因此可以省略id屬性 -->
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
    </bean>

4.5.3.2、測試

image

    @Test
    public void testDIInnerBean(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentSix", Student.class);
        System.out.println(student);
    }

4.6、為數組類型的屬性賦值

4.6.1、修改Student類

image

在Student類中添加或修改以下代碼:

    private String hobby[];

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", clazz=" + clazz +
                ", hobby=" + Arrays.toString(hobby) +
                '}';
    }

4.6.2、配置bean

image

    <bean id="studentSeven" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <!--如果數組類型是基本類型和string類型,就用value標簽-->
                <!--如果數組類型是類類型,就用ref標簽-->
                <value>游泳</value>
                <value>跑步</value>
                <value>騎車</value>
            </array>
        </property>
    </bean>

4.6.3、測試

image

    @Test
    public void testDIArray(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentSeven", Student.class);
        System.out.println(student);
    }

4.7、為List集合類型的屬性賦值

4.7.1、修改Clazz類

image

在Clazzt類中添加或修改以下代碼:


    private List<Student> students;

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }


    @Override
    public String toString() {
        return "Clazz{" +
                "cid=" + cid +
                ", cname='" + cname + '\'' +
                ", students=" + students +
                '}';
    }


4.7.2、配置bean

image

    <bean id="student" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
    </bean>

    <bean id="studentThree" class="org.rain.spring.pojo.Student">
        <property name="id" value="1003"></property>
        <property name="name">
            <value><![CDATA[<張三>]]></value>
        </property>
        <property name="sex" >
            <null></null>
        </property>
    </bean>

    <bean id="clazz" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
    </bean>

    <bean id="studentfour" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz" ref="clazz"></property>
    </bean>

    <bean id="clazzTwo" class="org.rain.spring.pojo.Clazz">
        <property name="cid" value="1111"></property>
        <property name="cname" value="三年E班"></property>
        <property name="students">
            <list>
                <!--如果list集合元素是基本類型和string類型,就用value標簽-->
                <!--如果list集合元素是類類型,就用ref標簽-->
                <ref bean="student"></ref>
                <ref bean="studentThree"></ref>
                <ref bean="studentfour"></ref>
            </list>
        </property>
    </bean>

4.7.3、測試

image

    @Test
    public void testDIList(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Clazz clazz = applicationContext.getBean("clazzTwo", Clazz.class);
        System.out.println(clazz);
    }

4.8、為Map集合類型的屬性賦值

4.8.1、創建教師類Teacher

image

package org.rain.spring.pojo;

/**
 * @author liaojy
 * @date 2023/7/30 - 12:10
 */
public class Teacher {
    private Integer tid;
    private String tname;

    public Teacher() {
    }

    public Teacher(Integer tid, String tname) {
        this.tid = tid;
        this.tname = tname;
    }

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "tid=" + tid +
                ", tname='" + tname + '\'' +
                '}';
    }
}

4.8.2、修改Student類

image

在Student類中添加或修改以下代碼:

    private Map<String,Teacher> teacherMap;

    public Map<String, Teacher> getTeacherMap() {
        return teacherMap;
    }

    public void setTeacherMap(Map<String, Teacher> teacherMap) {
        this.teacherMap = teacherMap;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", clazz=" + clazz +
                ", hobby=" + Arrays.toString(hobby) +
                ", teacherMap=" + teacherMap +
                '}';
    }

4.8.3、配置bean

image

    <bean id="teacherOne" class="org.rain.spring.pojo.Teacher">
        <property name="tid" value="10010"></property>
        <property name="tname" value="張老師"></property>
    </bean>

    <bean id="teacherTwo" class="org.rain.spring.pojo.Teacher">
        <property name="tid" value="10086"></property>
        <property name="tname" value="李老師"></property>
    </bean>

    <bean id="studentEight" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <value>游泳</value>
                <value>跑步</value>
                <value>騎車</value>
            </array>
        </property>
        <property name="teacherMap">
            <map>
                <!--如果鍵是字面量,就用key屬性,否則用key-ref屬性 -->
                <!--如果值是字面量,就用value屬性,否則用value-ref屬性 -->
                <entry key="10010" value-ref="teacherOne"></entry>
                <entry key="10086" value-ref="teacherTwo"></entry>
            </map>
        </property>
    </bean>

4.8.4、測試

image

    @Test
    public void testDIMap(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentEight", Student.class);
        System.out.println(student);
    }

4.9、為集合類型的屬性賦值(解耦引用方式)

4.9.1、配置bean

image

註意:使用util:list、util:map標簽必須引入相應的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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--list集合類型的bean-->
    <util:list id="students">
        <ref bean="student"></ref>
        <ref bean="studentThree"></ref>
        <ref bean="studentfour"></ref>
    </util:list>

    <!--map集合類型的bean-->
    <util:map id="teacherMap">
        <entry key="10010" value-ref="teacherOne"></entry>
        <entry key="10086" value-ref="teacherTwo"></entry>
    </util:map>

    <bean id="studentNine" class="org.rain.spring.pojo.Student">
        <property name="id" value="0011"></property>
        <property name="name" value="張三"></property>
        <property name="age" value="23"></property>
        <property name="sex" value="男"></property>
        <property name="clazz">
            <bean class="org.rain.spring.pojo.Clazz">
                <property name="cid" value="1111"></property>
                <property name="cname" value="三年E班"></property>
            </bean>
        </property>
        <property name="hobby">
            <array>
                <value>游泳</value>
                <value>跑步</value>
                <value>騎車</value>
            </array>
        </property>
        <property name="teacherMap" ref="teacherMap"> </property>
    </bean>

</beans>

4.9.2、測試

image

    @Test
    public void testDIUtil(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentNine", Student.class);
        System.out.println(student);
    }

4.10、p命名空間(瞭解)

4.10.1、配置bean

image

註意:p命名空間的依賴註入,本質上是屬性方式(setter方法)的依賴註入

<?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:util="http://www.springframework.org/schema/util"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!--map集合類型的bean-->
    <util:map id="teacherMap">
        <entry key="10010" value-ref="teacherOne"></entry>
        <entry key="10086" value-ref="teacherTwo"></entry>
    </util:map>

    <bean id="studentTen" class="org.rain.spring.pojo.Student"
    p:id="1001" p:name="張三" p:teacherMap-ref="teacherMap"></bean>

</beans>

4.10.2、測試

image

    @Test
    public void testDIP(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-ioc.xml");
        Student student = applicationContext.getBean("studentTen", Student.class);
        System.out.println(student);
    }

4.11、引入外部屬性文件

4.11.1、引入資料庫相關依賴

image

        <!-- MySQL驅動 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <!-- 數據源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.31</version>
        </dependency>

4.11.2、創建外部屬性文件

image

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

4.11.3、配置bean

image

註意:使用context:property-placeholder標簽必須引入相應的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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--引入外部屬性文件-->
    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>

    <bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--通過${key}的方式訪問外部屬性文件的value-->
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

</beans>

4.11.4、測試

image

    @Test
    public void testDIOuterFile() throws SQLException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-datasource");
        DruidDataSource datasource = applicationContext.getBean("datasource", DruidDataSource.class);
        System.out.println(datasource.getConnection());
    }

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文中所提到的運算都是基於整數來說的,因為只有整數(包括正數和負數)在操作系統中是以二進位的補碼形式運算的,關於原碼、反碼、補碼、位運算、移位運算的背景這裡不再介紹,網上資料很多,感興趣的可自行搜索。 java中能表示整數數據類型的有byte、short、char、int、long,在電腦中占用的 ...
  • ## 編譯器 編譯器的作用就是將高級編程語言翻譯為機器代碼。 編譯器工作過程一般分為: - 詞法分析:將高級語言解析成 Token 集合; - 語法分析:將 Token 集合構建成語法樹,在這個過程可以判斷出語法是否有誤,比如 `while` 後面是否 `{` 等等; - 語義分析:判斷語法樹是否有 ...
  • 為了保障整體的穩定性,在改動成本比較小的情況下,達到快速實現,穩定運行,預防這種偶發異常,我們實現了一種輕量級定時任務來進行無縫隙降級 ...
  • 大家好,我是三友~~ 今天來跟大家聊一聊Spring的9大核心基礎功能。 其實最近有小伙伴私信問我怎麼不寫文章了,催更來了 其實我不是不寫,而是一直在寫這篇文章,只不過令我沒想到的是,從前期的選題、準備、翻源碼、動手到寫完,前後跨度接近一個月的時間,花了好幾個周末,寫了三萬字,最終才算完成。 所以如 ...
  • DripTable 是京東零售推出的一款用於企業級中後臺的動態列表解決方案,項目基於 React 和 JSON Schema,旨在通過簡單配置快速生成頁面動態列表來降低列表開發難度、提高工作效率。 DripTable 目前包含以下子項目:drip-table、drip-table-generator ...
  • ## 教程簡介 D3是Data-Driven Documents的縮寫,D3.js是一個基於數據管理文檔的資源JavaScript庫。 D3 是最有效的數據可視化框架之一。它允許開發人員在 HTML、CSS 和 SVG 的幫助下在瀏覽器中創建動態的互動式數據可視化。數據可視化是將過濾後的數據以圖片和 ...
  • 數據類型 1、能直接處理的基本數據類型有5個:整型、浮點型、字元串、布爾值、空 1)整型(int)=整數,例如0至9,-1至-9,100,-8180等,人數、年齡、頁碼、門牌號等 沒有小數位的數字,是整型 2)浮點型(float)=小數,例如金額、身高、體重、距離、長度、π等 精確到小數位的數字,是 ...
  • # Python 3.12 搶先看——關於 f-string 的改動 哈嘍大家好,我是鹹魚 相信小伙伴們對 python 中的 f-string 都不陌生 f-string 是格式化字元串的縮寫,是以小寫或大寫字母 F 為首碼的字元串文本 f-string 提供簡潔明瞭的語法,**允許對變數和表達式 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...