spring中的scope和static

前言

scope的作用域。默认是单例模式,即scope=“singleton”。另外scope还有prototype、request、session、global session作用域。scope="prototype"多例。再配置bean的作用域时,它的头文件形式如下:

1、 在spring2.0之前bean只有2种作用域即:singleton(单例)、non-singleton(也称 prototype)

2、Spring2.0以后,增加了session、request、global session三种专用于Web应用程序上下文的Bean。因此,默认情况下Spring2.0现在有五种类型的Bean。当然,Spring2.0对 Bean的类型的设计进行了重构,并设计出灵活的Bean类型支持,理论上可以有无数多种类型的Bean,用户可以根据自己的需要,增加新的Bean类 型,满足实际应用需求。

scope详情
singleton表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
prototype表示每次获得bean都会生成一个新的对象
request表示在一次http请求内有效(只适用于web应用
session表示在一个用户会话内有效(只适用于web应用)
globalSession表示在全局会话内有效(只适用于web应用)

一般情况下,我们只会使用singleton和prototype两种scope,如果在spring配置文件内未指定scope属性,默认为singleton。

单例的原因有二:
1、为了性能。
2、不需要多例。

默认情况下,从bean工厂所取得的实例为Singleton(bean的singleton属性) Singleton: spring容器只存在一个共享的bean实例,
Prototype: 每次对bean的请求都会创建一个新的bean实例。二者选择的原则:有状态的bean都使用Prototype作用域 ,而对无状态的bean则应该使用singleton作用域。

1、测试

最佳实践:定义一个非静态成员变量时候,则通过注解@Scope(“prototype”),将其设置为多例模式(每次都会重新new一个)。


@RestController
@RequestMapping("scope")
public class ScopeController {

    public static int static_n = 1 ;
    public  int no_static_n  = 1 ;

    //http://localhost:8080/scope/default
    @ResponseBody
    @GetMapping("default")
    public String getN(){
        ++ static_n ;
        ++ no_static_n ;
        return "静态:"+static_n +"+非静态"+no_static_n ;
    }

    //静态:2+非静态2
    //静态:3+非静态3
    //静态:4+非静态4
    //静态:5+非静态5
    //静态:6+非静态6
    //静态:7+非静态7

}


@Scope("singleton")
@RestController
@RequestMapping("scope")
public class SingletonController {

    public static int static_n = 1 ;
    public  int no_static_n  = 1 ;

    //http://localhost:8080/scope/singleton
    @ResponseBody
    @GetMapping("singleton")
    public String getN(){
        ++ static_n ;
        ++ no_static_n ;
        return "静态:"+static_n +"+非静态"+no_static_n ;
    }

    //静态:2+非静态2
    //静态:3+非静态3
    //静态:4+非静态4
    //静态:5+非静态5
    //静态:6+非静态6
    //静态:7+非静态7
}



@Scope("prototype")
@RestController
@RequestMapping("scope")
public class PrototypeController {

    public static int static_n = 1 ;
    public  int no_static_n  = 1 ;

    //http://localhost:8080/scope/prototype
    @ResponseBody
    @GetMapping("prototype")
    public String getN(){
        ++ static_n ;
        ++ no_static_n ;
        return "静态:"+static_n +"+非静态"+no_static_n ;
    }

    //静态:2+非静态2
    //静态:3+非静态2
    //静态:4+非静态2
    //静态:5+非静态2
    //静态:6+非静态2
    //静态:7+非静态2
    //静态:8+非静态2

}

2、使用:

2.1、每次请求都会传age过来,如果这个Action是个单例的话,后面请求的age,就把前面的给覆盖了,所以必须设置成prototype

Class TestAction{  
  
    private int age;  
  
}  

2.2、由于它没有实例变量,所以不存在冲突的问题,用默认的单例就可以

class TestService{  
  
    @Autowired  
    private IUserDAO dao;  
  
}  

2.3、总结:

1.对于有实例变量的类,要设置成prototype;没有实例变量的类,就用默认的singleton

2.Action一般我们都会设置成prototype,而Service只用singleton就可以。

2、static

2.1、接口


public interface StaticService {

     String addNostaticN();

     String addStaticN() ;
}




2.2、实现


@Service
@Slf4j //下面这些变量与scope的状态有关,主要是看它有没有成员变量 ,默认是single
public class StaticServiceImpl implements StaticService {

    public static int static_n = 1 ;
    public  int no_static_n  = 1 ;

    public String addNostaticN(){
        ++no_static_n ;
        return "非静态"+no_static_n;
    }

    public String addStaticN(){
        ++static_n ;
        return "静态"+static_n;
    }

}

2.3、测试

@RestController
@RequestMapping("static")
public class StaticController {
    @Resource
    private    StaticService staticService ;

    //http://localhost:8080/static/addNostaticN
    @GetMapping("addNostaticN")
    @ResponseBody
    public String addNostaticN(){
        return staticService.addNostaticN();
    }
    //非静态2
    //非静态3
    //非静态4
    //非静态5
    //非静态6
    //非静态7
    //非静态8

    //http://localhost:8080/static/addNostaticN
    @GetMapping("addStaticN")
    @ResponseBody
    public String addStaticN(){
        return staticService.addStaticN();
    }
    //静态2
    //静态3
    //静态4
    //静态5
    //静态6
    //静态7
    //静态8
}


ContactAuthor

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Spring Boot整合MyBatis和Vue的案例: 1. 前端Vue部分 首先,我们创建一个Vue项目。在命令行输入以下命令: ``` vue create vue-mybatis-demo ``` 接着,在`vue-mybatis-demo`目录下执行以下命令: ``` npm install axios --save ``` 这将安装`axios`,一个用于发送HTTP请求的JavaScript库。 接下来,我们在`src`目录下创建一个`components`目录,并在其创建一个`UserList.vue`组件。在该组件,我们将使用`axios`从后端获取用户列表。以下是`UserList.vue`组件的代码: ```html <template> <div> <h2>User List</h2> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr v-for="user in users" :key="user.id"> <td>{{ user.id }}</td> <td>{{ user.name }}</td> <td>{{ user.age }}</td> </tr> </tbody> </table> </div> </template> <script> import axios from 'axios'; export default { data() { return { users: [] } }, mounted() { axios.get('/api/users') .then(response => { this.users = response.data; }) .catch(error => { console.log(error); }); } } </script> ``` 2. 后端Spring Boot部分 我们使用Spring Boot创建一个RESTful API,用于从数据库获取用户列表。首先,在`pom.xml`文件添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> </dependencies> ``` 然后,在`application.properties`文件添加以下配置: ``` spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= mybatis.mapper-locations=classpath:mapper/*.xml ``` 接下来,我们创建一个`User`实体: ```java public class User { private Long id; private String name; private Integer age; // getters and setters } ``` 然后,我们创建一个`UserMapper`接口和对应的XML文件,用于从数据库获取用户列表: ```java @Mapper public interface UserMapper { @Select("SELECT * FROM user") List<User> findAll(); } ``` ```xml <mapper namespace="com.example.demo.mapper.UserMapper"> <select id="findAll" resultType="com.example.demo.entity.User"> SELECT * FROM user </select> </mapper> ``` 最后,我们创建一个`UserController`,用于处理从前端发送的请求: ```java @RestController @RequestMapping("/api") public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> findAllUsers() { return userMapper.findAll(); } } ``` 3. 整合前后端 现在,我们需要将前端Vue项目打包并将其静态文件放入Spring Boot项目的`resources/static`目录下。在`vue-mybatis-demo`目录下执行以下命令: ``` npm run build ``` 这将生成一个`dist`目录,其包含前端Vue项目的静态文件。将该目录下的所有文件复制到Spring Boot项目的`resources/static`目录下。 最后,我们启动Spring Boot应用程序,并在浏览器访问`http://localhost:8080`,即可看到从数据库获取的用户列表。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值