栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Spring】Spring基础(上)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Spring】Spring基础(上)

目录

1、Spring

1.1、简介

1.2、优点

1.3、组成

1.4、拓展

2、IOC理论推导

2.1、UserDao接口

2.2、UserDaoImpl实现类

2.3、UserService业务接口

2.4、UserServiceImpl业务实现类

3、HelloSpring例子

3.1、注入依赖(导包)

3.2、写配置文件applicationContext.xml

3.3、接口

3.4、实现类

3.5、将控制权交于Spring

3.6、测试

3.7、项目结构:

4、IOC创建对象的方式

4.2、使用有参构造创建对象

5、Spring配置

5.1、别名

5.2、Bean的配置

5.3、import

6、依赖注入(DI)

6.1、构造器注入

6.2、Set注入【重点】

6.2.1、复杂类型

6.2.2、真实测试

6.2.3、Spring配置

6.2.4、输出

6.3、拓展方式注入

6.3.1、p命名空间注入和c命名空间注入

6.4、Bean的作用域

6.4.1、单例模式(Spring默认单例)

6.4.2、原型模式

6.4.3、其余的request、session、application这些只能在web开发中使用到


1、Spring

1.1、简介

官方参考文件:Core Technologies

1.2、优点
  • Spring是一个开源的免费框架(容器)

  • Spring是一个轻量级的、非入侵式的框架

  • 控制反转(IOC),面向切面编程(AOP)√

  • 支持事务的处理,对框架整合的支持

总结:Spring是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架

1.3、组成

七大模块:

1.4、拓展
  • Spring Boot

    • 一个快速开发的脚手架

    • 基于SpringBoot可以快速的开发单个微服务

    • 预定大于配置!

  • Spring Cloud

    • Spring Cloud是基于SpringBoot实现的

学习SringBoot的前提要完全掌握Spring及SpringMVC!!

2、IOC理论推导

2.1、UserDao接口
package dao;
​
public interface TestDao {
    public void sayHello();
}
 

2.2、UserDaoImpl实现类
public class TestDaoImpl implements TestDao {
​
    @Override
    public void sayHello() {
        System.out.println("Hello Spring Welcome");
    }
}

2.3、UserService业务接口
public interface TestService {
    void say();
}
​

2.4、UserServiceImpl业务实现类
public class TestServiceImpl implements TestService {
    private TestDao testDao;
​
    //通过setTestDao将控制权转移给用户
    public void setTestDao(TestDao testDao){
        this.testDao = testDao;
    }
​
    public void say() {
        testDao.say();
    }
}

在写业务之前,用户不同的需求可能会影响原来的代码,需要根据用户需求更改源代码,如果数据过多,就惨了。

使用set接口实现:

public void setTestDao(TestDao testDao){
    this.testDao = testDao;
}
  • 之前,程序时主动创建对象,控制权在程序员手上,

  • 使用set注入后,程序不在具有主动性,而是变成了被动的接收对象

减少了程序的耦合性。

3、HelloSpring例子

3.1、注入依赖(导包)

    org.springframework
    spring-webmvc
    5.0.2.RELEASE

    org.springframework
    spring-jdbc
    5.0.2.RELEASE

    commons-logging
    commons-logging
    1.2


    log4j
    log4j
    1.2.17


    org.testng
    testng
    RELEASE
    compile

3.2、写配置文件applicationContext.xml

3.3、接口
public interface TestDao {
    public void say();
}

3.4、实现类
public class TestDaoImpl implements TestDao {
​
    @Override
    public void say() {
        System.out.println("Hello Spring first");
    }
}
public class TestDaoImpl02 implements TestDao{
​
    @Override
    public void say() {
        System.out.println("Welcome to second");
    }
}

3.5、将控制权交于Spring


​
    
    
    
    
​
    
    
    
        
    

3.6、测试
//        TestService test = new TestServiceImpl();
//        ((TestServiceImpl) test).setTestDao(new TestDaoImpl());
//        test.say();
​
//测试类里只需要找到配置文件,然后通过getBean方法来获得所需要的东西
​
//获取applicationContext:拿到Spring容器
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
//拿到容器,需要什么,就直接get什么
TestServiceImpl testServiceImpl = (TestServiceImpl) Context.getBean("TestServiceImpl");
//调用方法
testServiceImpl.say();
}

3.7、项目结构:

控制反转:

  • 控制

    • 谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象由Spring来创建。

  • 反转

    • 程序本身不创建对象,而变成被动的接收对象

  • 依赖注入

    • 利用set方法来进行注入

IOC是一种编程思想,由主动的编程变成被动的接收

可以通过newClassPathXmlApplicationContext去浏览源码

使用Spring后,就可以不再去程序中更改代码,要实现不同的操作,只需要在xml配置文件中进行修改,所谓IOC,就是由Spring来创建,管理,装配。

4、IOC创建对象的方式

4.1、无参构造创建对象,(默认实现!)

public class User {
    private String name;
​
    public void User(){
        System.out.println("这是一个无参构造!!!");
    }
    //有参构造
    //    public User(String name){
    //        this.name = name;
    //    }
​
    public String getName() {
        return name;
    }
​
    public void setName(String name) {
        this.name = name;
    }
​
    public void show(){
        System.out.println("name="+name);
    }
 

    
    
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
​
    User name = (User) context.getBean("user");
​
    name.show();
​
}

4.2、使用有参构造创建对象
  • 下标赋值


    
  • 类型


    
  • 参数名


    

5、Spring配置

5.1、别名
 

5.2、Bean的配置

    

5.3、import

一般用于团队开发使用,可以将多个配置文件导入合并为一个

假设,先要将项目中多个人开发,这三个人复制不同的类开发,不同的类需要注册不同的bean,我们可以利用import将所有人的配置文件合并为一个总的,放在总的配置文件中:applicationContext.xml

  • FirstDemo.xml

  • ScendDemo.xml

  • ThirdDemo.xml

  • applicationContext.xml

    
    
    

使用的时候直接配置即可

6、依赖注入(DI)

6.1、构造器注入

前面说过

6.2、Set注入【重点】
  • 依赖注入:Set注入!

    • 依赖:bean对象的创建依赖于容器

    • 注入:bean对象中的所有属性由容器来注入

【环境搭建】

6.2.1、复杂类型
public class Address {
    private String address;
​
    public String getAddress() {
        return address;
    }
​
    public void setAddress(String address) {
        this.address = address;
    }
}
public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List hobbies;
    private Map card;
    private Set game;
    private Properties info;
    private String wife;
    
    getter  setter.....
}

6.2.2、真实测试
public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    Student student = (Student) context.getBean("student");
    System.out.println(student.toString());
}

6.2.3、Spring配置

    


    
    
​
    
    
​
    
    
        
            书01
            书02
            书03
        
    
​
    
    
        
            敲代码
            玩游戏
            看电影
        
    
​
    
    
        
            
            
        
    
​
    
    
        
            LOL
            COC
            BOB
        
    
​
    
    
        
    
​
    
    
        
            2312314
            10.10.94.65
            64343437437
            game
        
    

6.2.4、输出
Student{name='天赐', address=Address{address='四川'}, books=[书01, 书02, 书03], hobbies=[敲代码, 玩游戏, 看电影], card={身份证=510123146521346, 银行卡=5321661316546513}, game=[LOL, COC, BOB], info={hobbies=game, url=10.10.94.65, card=64343437437, driver=2312314}, wife='null'}

6.3、拓展方式注入

6.3.1、p命名空间注入和c命名空间注入

1.加入约束

首先要在配置文件内加入xml约束:xmlns:p="http://www.springframework.org/schema/p";xmlns:c="http://www.springframework.org/schema/c"


​
    
    
​
    
    

2.测试:

public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    //通过反射,User.class,不用再强转
    //User user = context.getBean("user", User.class);
    User user = context.getBean("user2", User.class);
    System.out.println(user);
}

3.官方解释:

6.4、Bean的作用域

6.4.1、单例模式(Spring默认单例)

public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    //通过反射,User.class,不用再强转
    User user01 = context.getBean("user1", User.class);
    User user02 = context.getBean("user1", User.class);
    
    System.out.println(user01.hashCode());  //825658265
    System.out.println(user02.hashCode());  //825658265
    System.out.println(user01 == user02);   //true
}

6.4.2、原型模式

每次从容器中get的时候,都会产生一个新对象

public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    //通过反射,User.class,不用再强转
    User user01 = context.getBean("user2", User.class);
    User user02 = context.getBean("user2", User.class);
    
    System.out.println(user01.hashCode());  //787738361
    System.out.println(user02.hashCode());  //607932305
    System.out.println(user01 == user02);   //false
}

6.4.3、其余的request、session、application这些只能在web开发中使用到

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605547.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号