zoukankan      html  css  js  c++  java
  • 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()));
        
            }
        }
  • 相关阅读:
    Error: unable to connect to node rabbit@mail: nodedown
    flash上传控件跨域
    Nginx配置指定媒体类型文件强制下载
    Nginx反向代理配置可跨域
    日志:using the Connector/J connection property 'autoReconnect=true' to avoid this problem
    web 安全(一)--web简介及 前端技术(js)
    Android 自动化之元素定位 xpath
    Wireshark入门
    自动化测试工具
    Android App 专项测试--压力测试篇(三)
  • 原文地址:https://www.cnblogs.com/zrmj/p/11482122.html
Copyright © 2011-2022 走看看