学习笔记----Spring入门xml配置及注解DI

本文详细介绍了Spring框架的基本配置、对象创建、依赖注入等核心概念,包括使用构造函数和静态工厂方法创建对象,以及如何在配置文件中指定bean的scope。此外,还阐述了Spring容器的初始化和销毁机制,以及如何利用注解实现更简洁的依赖注入。最后,文章深入探讨了类扫描机制和常用的典型注解,如@Controller、@Service、@Repository等,帮助开发者更高效地整合和管理业务逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring入门:
基本配置:

把对象的创建交给spring的管理
   程序员不再关注对象的创建
   对象的创建的过程:

    

      1、写一个java类


      2、写一个spring的配置文件,把该类放入到spring容器中

            <bean id="helloWorld" class="cn.itheima03.spring.ioc.createobject.HelloWorld"></bean>

     

      3、启动spring容器

      4、把对象从spring容器中取出来
      5、对象调用方法
      说明:bean中的id值是唯一的,不能出现相同的ID值

   spring创建bean的方式
      1、默认的构造函数
      2、利用静态工厂方法
         1、提供静态工厂类
         2、在spring的配置文件中提供工厂方法
                <bean id="helloWorld_Factory" class="cn.itheima03.spring.ioc.create.HelloWorldFactory" factory-method="getInstance"></bean>
      3、实例工厂方法
   spring的scope
      1、spring创建对象默认是单例模式
          如果把一个list声明在该bean的属性中,这样list成了共享的数据,所以一般情况下把list声明在方法中
      2、把spring创建的bean改成prototype模式,只需要写一个属性:scope为prototype
   spring什么时候创建对象
      1、在默认情况下,spring容器启动的时候,就创建对象了
           有利于开发:
              当spring容器启动的时候,如果配置文件中有错误,则会报错
      2、当从spring容器中获取bean,当bean调用方法的时候创建对象
              如果所有的bean按照这种模式加载,当spring容器启动的时候是不会报错的
              如果一个bean中有大量的数据,需要的是用到的情况下加载

   spring容器的初始化和销毁:init
       前提条件:spring中的bean是单例的
       1、在一个bean中可以提供init方法和destroy方法
       2、当创建对象完成以后,直接执行init方法,该方法由spring容器来执行
       3、当spring容器关闭的时候,执行destroy方法,该方法由spring容器来执行,由该方法释放资源
     
    说明:
      如果spring中的bean是多例的情况下,spring容器不负责对象的销毁,由程序员把该对象设置为null
      如果spring中的bean是多例,这个时候,不管在配置文件中lazy-init设置成什么样的值,在context.getBean时才要创建对象,而且不负责销毁
     


spring的DI:依赖注入
    给属性赋值:
       依靠的是bean的set方法。
       构造函数赋值:
        public class Person{
            private Long pid;
            private String pname;
            private Student student;
            private Set sets;
            private List lists;
            private Map map;
            private Properties properties;  
        }

步骤:
    1、写一个javabean
    2、在javabean中声明几个属性
    3、属性的set和get方法
    4、写spring的配置文件
            <bean id="person" class="cn.itheima03.spring.di.Person">
     <!--
      property用来描述属性
      -->
     <property name="pid" value="1"></property>
     <property name="pname" value="王二麻子"></property>
   </bean>
    5、在客户端进行调用

原理:
    1、启动spring容器
    2、spring容器为person创建对象
    3、利用property中的name属性可以把set方法拼接出来
    4、利用java的反射机制,给属性赋值
    说明:在默认的情况下,第3、4步在spring容器启动的时候完成了,把这样的过程叫做bean的装配


说明:
   把list,set,map的值写在配置文件中,大部分的值都是固定的,用来装配这些集合的。例如:
      可以把hibernate所有的映射文件装配在list中,而这个装配可以写在配置文件中


可以利用构造函数给属性赋值
      <bean id="person_con" class="cn.itheima03.spring.di.constructor.Person">
           <constructor-arg index="0" value="王二麻子"></constructor-arg>
           <constructor-arg index="1" ref="student_con"></constructor-arg>
      </bean>
    通过以上方式也可以给属性赋值,同时创建对象用该构造器就可以了,不用默认的构造函数了
   但是如果不写constructor-arg,则spring容器会用默认的构造函数创建对象。



spring容器的继承机制:
    parent
    abstract

    <bean id="person_extends" class="cn.itheima03.spring.extend.Person" abstract="true">
               <property name="s" value="aaaa"></property>
   </bean>

    <bean id="student_extends" class="cn.itheima03.spring.extend.Student" parent="person_extends"></bean>

     上述的parent:让student_extends拥有person_extends的属性的值
           abstract:不让person_extends创建对象。



利用spring注解的形式实现DI
1、在spring的配置文件中,加入命名空间
        xmlns:context="http://www.springframework.org/schema/context"
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd

2、在spring的配置文件中,开启注解解析器
     
       <context:annotation-config></context:annotation-config>

3、把person和student放入到spring容器中

       <bean id="person" class="cn.itheima03.spring.di.annotation.Person"></bean>
       <bean id="student" class="cn.itheima03.spring.di.annotation.Student"></bean>

4、在Person类中的student不需要set和get方法



原理:
     1、当启动spring容器的时候,给spring容器的bean创建对象
     2、当spring容器解析到<context:annotation-config></context:annotation-config>的时候,
         spring容器扫描在spring容器中的bean
     3、查看bean中的属性或者方法上有没有@Resource
           1、有
               1、如果该注解的属性name的值为"",则会按照属性的名称和spring中的ID值进行匹配,如果匹配
                  成功,则赋值,如果匹配不成功,则按照类型进行匹配,如果类型匹配不成功,则报错
               2、如果该注解的属性name的值不为"",则按照name属性的值和spring中的ID值进行匹配,
                   如果匹配成功,则赋值,匹配不成功,则报错
           2、没有


说明:
   基本类型不能用注解赋值  
   注解的效率没有xml文件的高,但是书写比较简单
   Autowired按照类型进行匹配
   @Autowired
   @Qualifier("studen")按照ID进行匹配
   按照类型匹配比较危险
    


类扫描的机制
    步骤:
    1、在spring的配置文件中,引入命名空间
          xmlns:context="http://www.springframework.org/schema/context"
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd
    
     2、<context:component-scan base-package="cn.itheima03.spring.scan"></context:component-scan>
      既包含了依赖注入的注解解析器,还包含了类扫描的
    
     3、在一个类上标注注解@Component
                        public class Person {}
                        ==
                        <bean id="person" class="..Person">

                        @Component("aa")
                        public class Person {}
                        ==
                        <bean id="aa" class="..Person">
        spring容器会为该bean创建对象,id值为
                                            如果该注解没有value属性,则值为类的第一个字母变成小写,其他的不变
                                            如果有value属性,则用value属性的值作为ID的值
     4、执行@Resource注解的流程


使用的注解越多,在xml文件中写的内容越少,效率越低

更多典型化注解:
     @Controller
     @Service
     @Repository
     其作用和用法与@Component保持一致


注意:
     在一个类中有一个基本类型的属性,如果该类是以注解的形式放入到spring容器中的,基本属性在这里是没有办法赋值的
     利用注解的方式实现继承,不需要使用Parent
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值