zoukankan      html  css  js  c++  java
  • Spring1

    Spring1

    Spring 是⼀个企业级开发框架,是软件设计层⾯的框架,优势在于可以将应⽤程序进⾏分层,开发者可 以⾃主选择组件。

    MVC:Struts2、Spring MVC

    ORMapping:Hibernate、MyBatis、Spring Data

    Spring 框架两⼤核⼼机制(IoC、AOP)

    IoC(控制反转)/ DI(依赖注⼊)

    AOP(⾯向切⾯编程)

    如何使⽤ IoC

    1.创建 Maven ⼯程,pom.xml 添加依赖

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.southwind</groupId>
        <artifactId>aispringioc</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.11.RELEASE</version>
            </dependency>
        </dependencies>
    </project>
    

    2.创建实体类 Student

    import lombok.Data;
    @Data
    public class Student {
    private long id;
    private String name;
    private int age;
    }
    

    3.传统的开发⽅式,⼿动 new Student

    Student student = new Student();
    student.setId(1L);
    student.setName("张三");
    student.setAge(22);
    System.out.println(student);
    

    4.通过 IoC 创建对象,在配置⽂件中添加需要管理的对象,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:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    ">
        <bean id="student" class="com.southwind.entity.Student">
            <property name="id" value="1"></property>
            <property name="name" value="张三"></property>
            <property name="age" value="22"></property>
        </bean>
    </beans>
    

    5.从 IoC 中获取对象,通过 id 获取。

    //加载配置⽂件
    ApplicationContext applicationContext = new
    ClassPathXmlApplicationContext("spring.xml");
    Student student = (Student) applicationContext.getBean("student");
    System.out.println(student);
    
  • 相关阅读:
    C# 控制台应用程序输出颜色字体[更正版]
    ORM for Net主流框架汇总与效率测试
    php 去掉字符串的最后一个字符
    bzoj1185 [HNOI2007]最小矩形覆盖 旋转卡壳求凸包
    bzoj [Noi2008] 1061 志愿者招募 单纯形
    bzoj1009 [HNOI2008] GT考试 矩阵乘法+dp+kmp
    扩展欧几里得(ex_gcd),中国剩余定理(CRT)讲解 有代码
    BZOJ 2103/3302/2447 消防站 树的重心【DFS】【TreeDP】
    hihocoder 1449 后缀自动机三·重复旋律6
    hihocoder 后缀自动机二·重复旋律5
  • 原文地址:https://www.cnblogs.com/wind-and-sky/p/14213424.html
Copyright © 2011-2022 走看看