spring成长1

注解,常见注解

  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("名称")

扫描二维码关注公众号,回复: 6098913 查看本文章

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>

猜你喜欢

转载自blog.csdn.net/csdnHuiTailang/article/details/89392966