spring

Spring

简介

  • spring----->春天

  • 2002年首次推出了Spring的雏形,interface 21框架

  • Spring框架以interface21框架为基础,经过重新设计,并不断丰富他的内容,于2004年3月24日发布了1.0的正式版

  • Rod johnson Spring Framework创始人,著名作者。 Rod在悉尼大学不仅获得了计算机学位,同时还获得了音乐学位。

  • Spring理念:是现有的技术更加容易使用,本身就是一个大杂烩,整合了现有技术框架。

  • SSH:struct2 + Spring +Hibernade

  • SSM:SpringMvc + Spring + mybatis

  • 官网:https://spring.io/projects/spring-framework/

  • 官方下载地址:https://repo.spring.io/artifactory/release/org/springframework/spring/

  • GIthub:

 <dependency>
   <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.8</version>
</dependency>

 <dependency>
   <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.8</version>
</dependency>

优点

  • spring是一个开源的免费的的框架
  • Spring是一个轻量级的,非入侵式的框架
  • 控制反转(IOC),面向切面编程(AOP)
  • 支持事务的处理,对框架整合的支持

Spring就是一个 控制反转(IOC),面向切面编程(AOP)的框架

组成

在这里插入图片描述

拓展

  • 现代化的java开发,基于spring开发
    在这里插入图片描述
  • Spring Boot
    • 一个快速开发的脚手架
    • 基于SpringBoot可以快速开发单个微服务
    • 约定大于配置
  • Spring Cloud
  • SpringCloud是基于SpringBoot实现的
  • 弊端:发展了太久以后,违背了原来的理念,配置十分繁琐,人称配置地狱

IOC理论推导

  • UserDao接口
package com.hua.Dao;

public interface UserDao {
    void getUser();
}
  • UserImpl实现类
package com.hua.Dao;


public class UserDaoImpl implements UserDao {
    @Override
    public void getUser() {
        System.out.println("实现方法");
    }
}
  • UserService业务接口
package com.hua.Service;

public interface UserService {
    void getUser();
}
  • UserServiceImlp业务实现类
package com.hua.Service;

import com.hua.Dao.UserDao;
import com.hua.Dao.UserDaoImpl;
import com.hua.Dao.UserMysqlImpl;
import com.hua.Dao.UserOracleImpl;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void getUser() {

        userDao.getUser();
    }
}
import com.hua.Dao.UserOracleImpl;
import com.hua.Service.UserServiceImpl;

public class Mytest {
    public static void main(String[] args) {
        UserServiceImpl userServicelmpl = new UserServiceImpl();
        userServicelmpl.setUserDao(new UserOracleImpl());
        userServicelmpl.getUser();
    }
}

使用set使用进行动态的值的实现

IOC本质

控制反转loC(Inversion of Control),是-种设计思想,DI(依赖注入)是实现IoC的一 种方法,也有人认为DI只是IoC的另一种说法。没有IoC的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序国际奥委会的另一种说法.没有国际奥委会的程序中,我们使用面向对象编程,对象的创建与对象间的依赖关系完全硬编码在程序中,对象的创建由程序自己控制,控制反转后将对象的创建转移给第三方,个人认为所谓控制反转就是:获得依赖对象的方式反转了.

采用XML方式配置Bean的时候,Bean的定 义信息是和实现分离的,而采用注解的方式可以把两者合为- -体,
Bean的定义信息直接以注解的形式定义在实现类中,从而达到了零配置的目的。

控制反转是- -种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中实现控制反
转的是IoC容器,其实现方法是依赖注入(Dependency Injection,DI)。

Spring配置

  • 別名:
<beans:alias name="person" alias="user"/>
  • bean的配置
    <beans:bean id="person" class="com.hua.pojo.person" name="li wu,ha;lu">
        <beans:constructor-arg name="s" value="通过参数来命名"/>
    </beans:bean>
  • import
    这个import,一般=用于团队开发使用,他可以将多个配置文件,导入合并成一个
    假设项目中有多个人开发,几个人负责不同的类开发,不同的类需要注册在不同的bean中,可以利用import将所有人的beans.xml合并在一起

依赖注入

  • 构造器注入

  • Set方式注入

    • 依赖注入:set注入
    • 依赖:beans对象依赖于容器
    • 注入:bean对象中的所有属性,由容器来注入
  • 环境搭建
    在这里插入图片描述

  • 实体类Student

package com.hua.pojo;


import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

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

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", card=" + card +
                ", games=" + games +
                ", wife='" + wife + '\'' +
                ", info=" + info +
                '}';
    }
}
  • Address
package com.hua.pojo;

public class Address {
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }
}
  • beans.xml配置文件
<?xml version="1.0" encoding="utf-8" ?>
<beans:beans xmlns:beans="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">
    <beans:bean id="address" class="com.hua.pojo.Address">
        <beans:property name="address" value="192.168.1.1"/>
    </beans:bean>
    <beans:bean id="student" class="com.hua.pojo.Student">
        <beans:property name="address" ref="address"/>
        <beans:property name="name" value="张三"/>
        <beans:property name="books">
            <beans:array>
                <beans:value>java</beans:value>
                <beans:value>html</beans:value>
                <beans:value>C++</beans:value>
                <beans:value>python</beans:value>
            </beans:array>
        </beans:property>
        <beans:property name="hobbys">
            <beans:list>
                <beans:value>吃饭</beans:value>
                <beans:value>睡觉</beans:value>
                <beans:value>吃饭</beans:value>
            </beans:list>
        </beans:property>
        <beans:property name="card">
            <beans:map>
                <beans:entry key="身份证" value="101010100100012121"/>
                <beans:entry key="银行卡" value="5212012201124513256"/>
            </beans:map>
        </beans:property>
        <beans:property name="games">
            <beans:set>
                <beans:value>LOL</beans:value>
                <beans:value>COC</beans:value>
                <beans:value>BOB</beans:value>
            </beans:set>
        </beans:property>
        <beans:property name="wife">
            <beans:null></beans:null>
        </beans:property>
        <beans:property name="info">
            <beans:props>
                <beans:prop key="id">1</beans:prop>
                <beans:prop key="name">匿名</beans:prop>
                <beans:prop key="sex"></beans:prop>
            </beans:props>
        </beans:property>
     </beans:bean>

</beans:beans>
  • 测试类
package test;

import com.hua.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class myTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());
    }
}

扩展方式注入

  • p命名空间和c命名空间
<?xml version="1.0" encoding="utf-8" ?>
<beans:beans xmlns:beans="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"
             xmlns:c="http://www.springframework.org/schema/c"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--可以直接注入-->
        <beans:bean id="user" class="com.hua.pojo.User" p:name="张三" p:age="12"/>

        <!--通过构造器注入-->
        <beans:bean id="user1" class="com.hua.pojo.User" c:name="无名" c:age ="18"/>
</beans:beans>

p命名空间和c命名空间不能直接使用,需要导入xml约束

             *xmlns:p="http://www.springframework.org/schema/p"
             xmlns:c="http://www.springframework.org/schema/c"*

Bean的作用域

  • 单例模式
<beans:bean id="user" class="com.hua.pojo.User" p:name="张三" p:age="12" scope="singleton"/>
  • 原型模式
<beans:bean id="user" class="com.hua.pojo.User" p:name="张三" p:age="12" scope="prototype"/>
  • 其余的request,session,application,这个只能在web开发中才能用到

Bean的自动装配

  • 环境搭建
  • 先创建实体类Dog,Cat,Person
package com.hua.pojo;

public class Cat {
    public void show(){
        System.out.println("猫叫");
    }

}
package com.hua.pojo;

public class Dog {
    public void show(){
        System.out.println("狗叫");
    }
}

package com.hua.pojo;

public class Person {
    private Cat cat;
    private Dog dog;
    private String name;

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Person{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
  • beans.xml配置文件
<?xml version="1.0" encoding="utf-8" ?>
<beans:beans xmlns:beans="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">
        <beans:bean id="cat" class="com.hua.pojo.Cat"/>
        <beans:bean id="dog" class="com.hua.pojo.Dog"/>
        <!--byName:会在容器上下文中查找,和自己对象set方法对应的值对应的beanid-->
        <!--byType:会在容器上下文中查找,和自己对象属性类型相同beanid-->
        <beans:bean id="person" class="com.hua.pojo.Person" autowire="byType">
                <beans:property name="name" value="小明"/>
         </beans:bean>
</beans:beans>
  • 测试类
import com.hua.pojo.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class newTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        Person person = (Person) context.getBean("person");
        person.getCat().show();
        person.getDog().show();
    }
}

byName需要保证beans的id唯一,并且需要这个bean需要自动注入的set方法的值一致
byType需要保证class唯一,并且需要这个bean需要自动注入属性类型一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值