001_spring

  • 对于循环引用:singleton时直接实例化对象,而对于prototype会在准备实例第一个对象的时候去检查是否存在引用循环,存在的化就不会实例化,这样是spring为了防止直接进行实例化的时候进入循环引用而浪费内存。singleton引用出来的对象也是单例的,当singleton引用一个prototype对象时,引用出来的对象也是单例的,所以当全是singleton引用循环的时候回引用成功,而当是prototype时不会成功,或者是当第一个引用对象是singleton时也会成功。强引用的时候(ref),弱引用的时候(depends-on)
  • lazy-init="true"懒加载:在此实例被用到的时候才会被实例化,而不会在spring的配置文件中直接被实例化
  • 注解:
    • <!--直接自动扫描包,将bean注册到spring容器-->
          <context:component-scan base-package="com.zrm"></context:component-scan>

      <context:component-scan>

      <context:component-scan base-package="com.msb"></context:component-scan>

      component-scan可以自动扫描包内容,并注册Bean到Spring容器

      @Component

      在需要注册到容器的类上添加@Component标签,标识这个类由Spring容器接管

      约定大于配置

      在一个类上添加@Component默认会使用首字母小写的类名作为ID注册到Spring容器。

      如果需要手动指定Bean Id可以使用@Component("p")

      同属@Component的额外三个注解

      @Controller @Service @Repository

      这三个注意在MVC开发中会经常用到,除了注解名字和Component不一样之外,其余功能都一样。

      Spring额外提供这三个注解的目的主要是为了区分MVC中每个类的区别。

      @Scope

      使用注解注册Bean 默认的作用域还是singleton,可以使用@Scope("prototype")改变对象作用域

      @Value

       

      在使用注解给对象注入值的时候,不再需要Get/Set方法

      基础类型

      使用@Value注解

           @Value("小明")

           private String name;     

       

      对象引用

           @Autowired

           private Pet MyPet;

      使用@Autowired注解

      默认是ByType的,如果需要ByName需要配合@Qualifier注解

           @Autowired()

           @Qualifier("p2")

           private Pet MyPet;

      实例:如下

      //通过一个简单的MVC模式的实例并且使用注解的方式来实现的逻辑代码如下
      //controller部分
      package com.zrm.MainController;
      
      import com.zrm.MainEntity.Usre;
      import com.zrm.MainService.Service;
      import org.junit.Test;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;
      
      @Component("conl")
      public class Controller {
          @Autowired
          private Usre usre;
          @Autowired
          private Service service;
      
          @Test
          public void out(){
              System.out.println(this.usre);
          }
      
          public void control() {
              if (service.getUser(usre) == null) {
                  System.out.println("找不到对应的信息");
              } else {
                  System.out.println("login successfully");
              }
          }
      }
      
      
      
      service部分
      package com.zrm.MainService;
      
      import com.zrm.MainDao.UserDao;
      import com.zrm.MainEntity.Usre;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Component;
      
      @Component
      public class Service {
          @Autowired
          private UserDao userDao;
      
          public Usre getUser(Usre usre) {
              System.out.println("通过controller的获取到的user去Dao查询的得到的User");
              return userDao.getUser(usre);
          }
      }
      
      
      user部分
      package com.zrm.MainEntity;
      
      import org.junit.Test;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Scope;
      import org.springframework.stereotype.Component;
      
      /*
       * 通过注解方式注入值的时候不需要get,set方法
       * */
      @Component("user")//向spring容器中注册bean
      @Scope("prototype")//修改bean的类型默认为singleton
      public class Usre {
          @Value("18")
          private Integer age;
          @Value("zrm")
          private String name;
      }
      
      
      
      userdao部分
      package com.zrm.MainDao;
      
      import com.zrm.MainEntity.Usre;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.stereotype.Component;
      
      
      @Component
      public class UserDao {
          //@Autowired    //自动注入,默认是bytype如果要通过byname要使用添加@Qualifier("user")
      
          public Usre getUser(Usre usre) {
              System.out.println("这里是UserDao获取的user");
              return new Usre();
          }
      }
       //启动类
      package com.zrm;
      
      import com.zrm.MainController.Controller;
      import com.zrm.MainEntity.Usre;
      import org.apache.commons.lang3.builder.ToStringBuilder;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      public class Main {
          public static void main(String[] args) {
              ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("contextApplication.xml");
              Controller controller = ctx.getBean("conl", Controller.class);
              controller.control();
              Usre user = ctx.getBean("user", Usre.class);
              System.out.println(ToStringBuilder.reflectionToString(user, ToStringBuilder.getDefaultStyle()));
      
          }
      }

       

转载于:https://www.cnblogs.com/zrmj/p/11482122.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值