1 id 和 name 的区别

  id:不可重复,不可包含特殊字符

  name:可以重复,可以包含特殊字符

2 scope

  singleton:配置单例模式(默认),在容器启动时创建对象,而且只创建一个

  prototype:配置多例模式,在容器启动时不创建对象,当获取对象时才创建

3 lazy-init

  true:延迟创建对象,容器启动时不创建,获取时再创建,只适用于单例模式

  false:(默认)

4 init-method 和 destroy-mothod

  init-method:初始化调用方法

  destroy-mothod:销毁时调用方法

config:

  <bean name="p1" id="p2" scope="singleton" lazy-init="true" init-method="init" destroy-method="destroy"
class="com.roxy.spring.pojo.Person"></bean> test:
   
   @Test
    public void testIdAndName() {
        
        //创建容器
        AbstractApplicationContext  context = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //查找对象
        Person p1 = (Person)context.getBean("p1");
        Person p2 = (Person)context.getBean("p1");
        
        context.destroy();//执行销毁
//        context.close();//触发销毁
        
    }
   
console:
  
    Creating shared instance of singleton bean 'p2'
    Creating instance of bean 'p2'
    构造方法被调用
    Eagerly caching bean 'p2' to allow for resolving potential circular references
    Invoking init method  'init' on bean with name 'p2'
    Person被初始化!
    Invoking destroy method 'destroy' on bean with name 'p2'
    Person被销毁!
   

5 注解式开发:

注意导入包以及添加命名空间

<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:p="http://www.springframework.org/schema/p"
    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.xsd">
   
   <context:component-scan base-package="com.roxy.spring.pojo"></context:component-scan>
 
</beans> 在使用注解式开发,@Autowired自动装配引入对象类型的时候,会出现一个类型对应多个对象的问题 eg:
  
  car1{
    name : 小黄车
    color : yellow
  }
  car2{
    name : 小蓝车
    color:blue
  }
  person{
    name : draco
    age : 17
    car : ?
  }
    
  使用@Qualifiter指定具体的对象
  使用@Resource制定具体的对象

Spring 基础使用的更多相关文章

  1. Spring基础知识

    Spring基础知识 利用spring完成松耦合 接口 public interface IOutputGenerator { public void generateOutput(); } 实现类 ...

  2. spring基础整理

    spring基础教程:https://www.tutorialspoint.com/spring/spring_overview.htm 注入实例 <bean id="" c ...

  3. Spring 基础知识

    Spring架构简单描述 原文:https://www.shiyanlou.com/courses/document/212 Spring 概述 1. Spring 是什么 Spring是一个开源的轻 ...

  4. Spring基础配置

    从毕业到现在我一直从事Android开发,但是对JavaEE一直念念不忘,毕业校招的时候,一个礼拜拿了三个offer,岗位分别是Android.JavaEE和JavaSE,后来觉得Android比较简 ...

  5. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  6. 第65节:Java后端的学习之Spring基础

    Java后端的学习之Spring基础 如果要学习spring,那么什么是框架,spring又是什么呢?学习spring中的ioc和bean,以及aop,IOC,Bean,AOP,(配置,注解,api) ...

  7. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  8. Spring基础系列-Spring事务不生效的问题与循环依赖问题

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9476550.html 一.提出问题 不知道你是否遇到过这样的情况,在ssm框架中开发we ...

  9. Spring Boot实战(1) Spring基础

    1. Spring基础配置 Spring框架本身有四大原则: 1) 使用POJO进行轻量级和最小侵入式开发 2) 通过依赖注入和基于接口编程实现松耦合 3) 通过AOP和默认习惯进行声明式编程 4) ...

  10. spring基础学习01

    spring基础 Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用 IOC控制反转 把创建对象和维护对象之间的关系权利 ...

随机推荐

  1. [转]CentOS 6和CentOS 7防火墙的关闭

      CentOS6.5查看防火墙的状态: 1 [linuxidc@localhost ~]$service iptable status 显示结果: 1 2 3 4 5 [linuxidc@local ...

  2. OC基础:getter和setter,@public @protected @private 分类: ios学习 OC 2015-06-15 19:23 22人阅读 评论(0) 收藏

    @public 1.公开的,公共的,可以在类的内部和外部访问. 2.类的内部:实例变量名 3.类的外部:对象->实例变量名 @protected 1.受保护的,只能在本类和子类中可以访问 2.类 ...

  3. Object -c基础知识(5)--release 之后 retainCount为何为1

    在XCode中加入如下代码:  UILabel *label=[UILabel alloc]; [label setText:@"TestLabel"]; NSLog(@" ...

  4. HP LoadRunner 12.02 Tutorial T7177-88037教程独家中文版

    HP LoadRunner 12.02 Tutorial T7177-88037教程独家中文版 Tylan独家呕血翻译 转载请注明出自“天外归云”的博客园 Welcome to the LoadRun ...

  5. int类型转string类型c++

    前言 使用VS的过程中,经常会用到需要将int类型数据转换为字符串类型,便于显示信息等. 实现方法 c++11标准中的to_string函数,在VS安装文件的include文件中生成的只读文件,使用起 ...

  6. HDU 2072 单词数 详细解答

    题目 单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  7. 初次实践数据库--SQL Server2016

    初学数据库使用 安装了SQL Server2016的开发者版本,本来以为就可以愉快地开始数据库的挖坑了,发现开出来之后除了创建数据库.选择数据库以外,并没有什么操作. 后来才发现还需要再安装SSMS( ...

  8. git的使用方法学习

    1.git常用命令: git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致.同时,将当前的工作区内容保存到Git栈中. git stash p ...

  9. citus real-time 分析demo( 来自官方文档)

      citus 对于多租户以及实时应用的开发都是比较好的,官方也提供了demo 参考项目 https://github.com/rongfengliang/citus-hasuar-graphql 环 ...

  10. 可靠的推送IM消息

    一.      报文类型: 1.请求报文(request,后简称为为R): 2.应答报文(acknowledge,后简称为A): 3.通知报文(notify,后简称为N). R:客户端主动发送给服务器 ...