[Spring--day01--第一节课]初识Spring

[Spring--day01--第一节课]初识Spring,第1张

一:Spring基本概念 1.什么是Spring:Spring是一个开源的,控制反转(Ioc)和面向切面(AOP)的容器框架,它的目的是简化企业开发 2.Spring特点:使用Spring来完成bean的构建,使得其他应用层只专注自己的业务逻辑 3.Spring通过依赖注入(DI)或者控制反转(IOC)的方式来管理各个对象:

(1)IOC:控制反转就是应用本身不负责对象的创建及维护,对象的创建和维护由外部容易负责,所以控制权就由应用转移到外部容器,控制权的转移就是所谓的控制反转。
(2)DI:在运行时,由外部容易将对象注入到组件中。

4.Spring是一个轻量级的,低侵入式的框架 5.Spring的七大模块

(1)核心模块core:负责Bean的构建和管理,底层使用的是BeanFactory工厂模式来实现。BeanFactory使用依赖注入的形式提供给组件组件依赖
(2)Spring上下文模块context:国际化,Spring上下文是一个配置文件,主要向框架提供上下文信息。
(3)Spring AOP:面向切面的编程
(4)Spring Dao:它主要和dao层相关,可以用来构件数据库的异常处理,和不同数据库供应商抛出异常信息。
(5)Sping ORM:实体关系映射
(6)Spring Web:Web上下文模块建立在应用程序的上下文模块之上,为基于web的应用程序提供了上下文,Web模块还简化了处理多部分请求及将请求参数绑定到域对象的过程。
(7)Spring Web MVC:

二: 控制反转IOC

1.创建一个基于Maven项目,不用选模板 2.并在pom中,添加core,beans,和context模块,junit单元测试模块 3.编写Spring的配置文件,XXX.xml,一般applicationContext.xml

配置文件pom.xml

<properties>
        <spring.version>5.3.19spring.version>
    properties>
    <dependencies>
        <dependency>
            
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.11version>

            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.24version>
            <scope>providedscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${spring.version}version>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.9version>
        dependency>
    dependencies>

applicatin.xml


<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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <bean id="p1" class="entity.Person">
        <property name="name" value="张三">property>
        <property name="gender" value="">property>
        <property name="age" value="18">property>
    bean>

    <bean id="car1" class="entity.Car">
        <property name="brand" value="奔驰">property>
        <property name="color" value="white">property>
        <property name="price" value="50">property>
    bean>
    <bean id="house1" class="entity.House">
        <property name="location" value="和平区">property>
        <property name="size" value="100">property>
        <property name="price" value="600">property>
    bean>
    <bean id="house2" class="entity.House">
        <property name="location" value="南开区">property>
        <property name="size" value="200">property>
        <property name="price" value="500">property>
    bean>

    <bean id="p2" class="entity.Person">
        <property name="name" value="张三">property>
        <property name="gender" value="">property>
        <property name="age" value="18">property>
        <property name="car" ref="car1">property>
        <property name="houseList">
            <list>
                <ref bean="house1">ref>
                <ref bean="house2">ref>
                <bean class="entity.House">
                    <property name="location" value="蓟州区">property>
                    <property name="size" value="500">property>
                    <property name="price" value="800">property>
                bean>
            list>
        property>
        <property name="map">
            <map>
                <entry key="address" value="天津市和平区">entry>
                <entry key="phone" value="139000000">entry>
                <entry key="info">
                    <bean class="entity.Info">
                        <property name="address" value="天津市和平区">property>
                        <property name="phone" value="13900000">property>
                        <property name="email" value="1213@ooo.com">property>
                    bean>
                entry>
                <entry key="niceName" value="老李">entry>
            map>
        property>
    bean>
    












***src/java/entity/Car.class***
package entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Car {
    private String brand;
    private String color;
    private int price;
}

src/java/entity/House.class

package entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class House {
    private String location;
    private int size;
    private int price;
}

src/java/entity/Info.class

package entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Info {
    private String address;
    private String phone;
    private String email;
}

src/java/entity/Person.class


```java
package entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;
import java.util.Map;
@NoArgsConstructor
@AllArgsConstructor
@Data
@ToString
public class Person {
    private String name;
    private String gender;
    private int age;
    private Car car;
    private List houseList;
    private Map map;

}
#### 4.在测试类中,测试对象的数据

```java
import entity.Person;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyText {
    @Test
    public void textPerson(){
        ApplicationContext application=new ClassPathXmlApplicationContext("application.xml");
        Person person1=(Person) application.getBean("p1");
        System.out.println(person1);
        Person person2=(Person) application.getBean("p2");
        System.out.println(person2);
        Person person3=(Person) application.getBean("p3");
        System.out.println(person3);
        Person person4=(Person) application.getBean("p4");
        System.out.println(person4);

    }

}

总结1:创建项目选择mevan,首先先向pom.xml添加依赖,在resources下创建xxx.xml写Spring配置文件,其次在entity写所需要的类。然后进行数据关联。数据关联,在xxm中用创建对象,id="xxx"为对象名,与类进行绑定,与类中属性进行绑定,name绑定属性名称,value为值。
可与list集合绑定,底下的书写规则同
eg:

private List<House> houseList;
<property name="houseList">
            <list>
                <ref bean="house1">ref>
                <ref bean="house2">ref>
                <bean class="entity.House">
                    <property name="location" value="蓟州区">property>
                    <property name="size" value="500">property>
                    <property name="price" value="800">property>
                bean>
            list>
        property>

可与map集合绑定
eg:

private Map<String,Object> map;
<property name="map">
            <map>
                <entry key="address" value="天津市和平区">entry>
                <entry key="phone" value="139000000">entry>
                <entry key="info">
                    <bean class="entity.Info">
                        <property name="address" value="天津市和平区">property>
                        <property name="phone" value="13900000">property>
                        <property name="email" value="1213@ooo.com">property>
                    bean>
                entry>
                <entry key="niceName" value="老李">entry>
            map>
        property>

总结2:
ref可用于引入其他对象到该对象下!

eg:


```xml
<bean id="car1" class="entity.Car">
        <property name="brand" value="奔驰"></property>
        <property name="color" value="white"></property>
        <property name="price" value="50"></property>
    </bean>
    <bean id="house1" class="entity.House">
        <property name="location" value="和平区"></property>
        <property name="size" value="100"></property>
        <property name="price" value="600"></property>
    </bean>

```xml

```xml
    
        
        
        
    

        
        
        
        
        
            
                
                
                
                    
                    
                    
                
            
        
      

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/727888.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存