spring成长1

本文深入探讨了Spring框架的关键特性,包括注解、依赖注入、生命周期管理及对象创建方式等,解析了@Component、@Autowired、构造函数注入、静态工厂和动态工厂的使用,以及单例与多例的作用域区别。

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

注解,常见注解

  1.  就是一个类,用@注解命名
  2. 开发中用注解取代xml配置文件    
  3. 注解扫描
    <context:component-scan base-package="com.cn.peng"/>

     

  4. 使用@Component,取代<bean class=""/> ,任意bean
  5. web开发中使用@Component注解的衍生注解

         @Respository    模型

         @Service          业务

         @Controller     视图

                 

依赖注入 ,DI

  1. 普通值:@value("")
  2. 引入值:   

            类型:@Autowired

            名称 :@Autowired

                         @qualifier("名称") 

           名称 :@Resource("名称")

spring创建对象性的方式 

  1. 构造函数
     <bean id="sys_user" class="com.deng.jing.ling.systest">
         <constructor-arg index="0" name="age" value="12" type="int"/>
           <constructor-arg index="1" name="sex" value="1" type="int"/>
       </bean>
  2. 静态工厂
  3. 动态工厂
    <bean id="工厂id" factory-bean="工厂类" factory-method="方法"/>

     

生命周期,初始化,销毁

   <bean id="testOne" class="com.cn.peng.dao.mapper.TestMapperOne" init-method="start" destroy-method="end" scope="prototype"></bean>

单例,多例 ,作用域scope

   /*单例 并结束*/
        /* <bean id="testOne" class="com.cn.peng.dao.mapper.TestMapperOne" init-method="start" destroy-method="end" scope="singleton"></bean>*/
        ApplicationContext ac = new ClassPathXmlApplicationContext("ssm/spring-context1.xml");
        TestMapper testOne = (TestMapper) ac.getBean("testOne");
        TestMapper testOne1 = (TestMapper) ac.getBean("testOne");
        System.out.println(testOne);
        System.out.println(testOne1);
        ac.getClass().getMethod("close").invoke(ac);
        /*结果
        实例之前
        com.cn.peng.dao.mapper.TestMapperOne@eff876
        com.cn.peng.dao.mapper.TestMapperOne@eff876
        实例之后
        * */
 /*多例 并结束*/
        /* <bean id="testOne" class="com.cn.peng.dao.mapper.TestMapperOne" init-method="start" destroy-method="end" scope="prototype"></bean>*/
        ApplicationContext ac = new ClassPathXmlApplicationContext("ssm/spring-context1.xml");
        TestMapper testOne = (TestMapper) ac.getBean("testOne");
        TestMapper testOne1 = (TestMapper) ac.getBean("testOne");
        System.out.println(testOne);
        System.out.println(testOne1);
        ac.getClass().getMethod("close").invoke(ac);
        /*结果
        实例之前
        实例之前
        com.cn.peng.dao.mapper.TestMapperOne@e6fdee
        com.cn.peng.dao.mapper.TestMapperOne@80d78a
        * */

 集合注入,list,map,set 类似

<bean id="ming" class="cn.ping.jing.sys">
    <property name="listUser">
        <list>
            <value>123</value>
            <value>567</value>
        </list>
    </property>
</bean>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值