Spring简介

  • Spring是一个开源的业务层的框架,是为了解决企业应用程序开发复杂性而创建的
  • 以前自己管理对象的创建与销毁,现在对象的创建全部交给Spring的IOC容器管理,需要就找它要
  • 重点
    • IOC控制反转
      • 和以往由程序员管理对象不同,它将对象的管理交给IOC容器,控制反转
    • DI依赖注入
    • SpringAOP面向切面编程
    • 事务控制
  • 官网 https://spring.io/
  • 使用Spring方式

    • 通过xml配置方式使用
    • 创建springboot项目,内置了spring

      IOC

  • 创建SpringBoot初始化项目

  • 新建controller、service包
    • controller包下建类 UserController
      • @Autowired注解告诉框架从IOC容器中取出一个对象赋值给变量
    • service包下接口UserService
      • 使用时利用多态,通用性强
    • service包下接口子类
      • 程序启动时,@Service告诉spring框架创建该类的一个对象,存到spring 框架中的ioc容器(hashMap)
  1. package com.tedu.webDemo.service;
  2. public interface UserService {
  3. public String login(String name,String pwd);
  4. }
package com.tedu.webDemo.service;

import org.springframework.stereotype.Service;
//@Service告诉spring框架创建该类的对象,存到spring 框架中的ioc容器(hashMap)
@Service
public class UserServiceImpl implements UserService {

    @Override
    public String login(String name, String pwd) {
        // TODO Auto-generated method stub
        return name+"登录成功!";
    }
}
package com.tedu.webDemo.controller;

@RestController
public class UserController {
    //@Autowired注解告诉框架给变量赋值一个对象,多态
    //接口定义通用性强
    @Autowired
    UserService userService;

    @RequestMapping("/ulogin")
    public String userLogin(String name,String pwd) {
        //使用子类对象的重写方法
        String str = userService.login(name, pwd);
        return str+"恭喜他";
    }
}

自动装配

  • 在service服务层中,一个接口有两个实现类,那么框架赋给哪个实现类呢?
  • 接口实现类1 ```java package com.tedu.webDemo.service; //@Service告诉spring框架创建该类的对象,存到spring 框架中的ioc容器(hashMap) @Service public class UserServiceImpl implements UserService {

    @Override public String login(String name, String pwd) {

      // TODO Auto-generated method stub
      return "NO";
    

    }

}


- 接口实现类2
```java
package com.tedu.webDemo.service;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl2 implements UserService {

    @Override
    public String login(String name, String pwd) {
        // TODO Auto-generated method stub
        return "yes";
    }

}
  • 默认是根据类型找对象
    • 当有两个实现类发生冲突时,再根据名字找对象
    • 此时是在一个hashmap结构中找,键是类名首字母小写,值是这个类对应的对象
      • 两种方式指定使用的子类对象
        • 特定的变量名: 子类类名首字母小写
        • 注解@Qualifier(“子类类名首字母小写”) ```java package com.tedu.webDemo.controller;

@RestController public class UserController { /默认是根据类型找对象,而接口有两个业务层实现类,默认方式出错,不知道找哪个 @Autowired UserService userService; /

//根据类型找不到对象,再根据名字(类名首字母小写)找对象
//找UserServiceImpl的对象,类UserServiceImpl的首字母小写userServiceImpl
@Autowired
UserService userServiceImpl;

//找UserServiceImpl的对象,类UserServiceImpl2的首字母小写userServiceImpl2
@Autowired
UserService userServiceImpl2;

@Autowired
@Qualifier("userServiceImpl2")//指定根据名称找对象,和上面意思一样
UserService userService;

@RequestMapping("/test")
public String test() {
    return userServiceImpl.toString()+"<br>"
            +userServiceImpl2.toString()+"<br>"
            +userService.toString()+"<br>"
            +this.toString();
}

}

<a name="WbbXE"></a>
## scope生命周期

- @component的功能与@service类似,加了@component后框架会创建对象,@service表示创建的是业务层对象,@component表示创建的是普通对象。
- @scope("singleton")单例,只创建一个对象,系统启动创建,唯一
- @scope("protoType") 原型,每次从容器中取对象时,创建新的对象
- //@Scope("session")一个用户(浏览器)创建一个对象
<a name="e7cKl"></a>
### 案例

- 需求:
   - 网站上来一个用户加1,放在accessCounter
   - 用户购物车:userCart
   - 日志:logInfo
- 分析:
   - AccessCount应该只有一个
   - LogInfo是每次创建新的
   - UserCart是一个用户创建一个
- 编码实现
- AccessCount
```java
package com.tedu.webDemo.service;

import org.springframework.context.annotation.Scope;

import org.springframework.stereotype.Component;
//scope:singleton单例,每次得到的对象是一样的
@Component
@Scope("singleton")
public class AccessCount {

}
  • LogInfo ```java package com.tedu.webDemo.service;

import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; //@scope(“protoType”) 原型,每次从容器中取对象时,创建新的对象 @Component @Scope(“prototype”) public class LogInfo {

}


- UserCart
```java
package com.tedu.webDemo.service;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//@Scope("session")一个用户创建一个对象
@Component
@Scope("session")
public class UserCart {

}
  • 控制层 ```java package com.tedu.webDemo.controller;

@RestController @Scope(“prototype”) public class UserController { //框架分配对象 @Autowired AccessCount accessCount;

@Autowired
LogInfo logInfo;

@Autowired
UserCart userCart;

@RequestMapping("/test2")
public String test2() {
    return accessCount.toString()+"<br>"
            +logInfo.toString()+"<br>"
            +userCart.toString();
}

}

```