栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

浅谈spring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

浅谈spring

目录

1. spring简介

2. spring ioc容器和Beans介绍

2.1 ioc是什么

2.2Beans是什么

2.3 获得容器及对象

2.3.1 实现如下实体类

2.3.2 配置元数据,实例化容器

2.3.3 获得对象,进行测试

 2.3.4最后,附上一个测试

3. DI

3.1简介

3.2 测试


1. spring简介

spring在不同的上下文中有不同的含义,它可以指spring框架项目本身,也可以指整个项目家族。这篇文章关注于spring框架本身。

spring是的创建Java企业应用程序变得很容易,它提供了在企业环境中使用Java语言所需的一切。

【未完,持续更新。。。 

2. spring ioc容器和Beans介绍

2.1 ioc是什么

ioc(控制反转),简单举例来说,就是在获取Java对象的时候,对象的属性不会在类中就定义好,而是会通过set方法来允许用户自己设置。即:对象属性不再由程序员硬编码去控制,而是让用户去控制。

其实,一般我们也是这样做的。

2.2Beans是什么

Bean,其实也就相当于Java对象。使用spring后,获得一个实体对象不再需要new关键字了,你只需要获得一个ApplicationContext的容器,然后需要什么对象就直接用getBean方法就可以获取,不过别忘了强转类型。

也就是说,容器在创建的时候,就已经把在它注册文件里注册的对象 new出来了。并且,通过容器获得对象,这使用的是单例模式,也就是说,你用同样的id获得的多个对象,实际上都是同一个。对其中一个对象的属性进行操作,会使你获得的其它对象的属性也随之更改。

2.3 获得容器及对象

2.3.1 实现如下实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Good {
    private int id;
    private String name;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private Good good;
}

2.3.2 配置元数据,实例化容器

spring ioc容器使用 一种配置元数据的形式。这个配置元数据告诉spring容器如何在应用程序中实例化、配置和组装对象。这篇文章中,配置元数据以简单直观的xml格式提供。

配置元数据文件applicationContext.xml如下





    
        
        
    

    
        
        
        
    

    

利用配置元数据new 一个ClassPathXmlApplicationContext 对象, 获得一个ApplicationContext对象,这个ApplicationContext对象就是我们的spring容器。可以通过它的getBean方法获得对象。(ApplicationContext是ClassPathXmlApplicationContext的超类)

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

2.3.3 获得对象,进行测试

在测试中,通过user获得了user1和user2两个对象,对其中一个的属性进行修改,输出另外一个,发现两个的属性都变了

        User user1 = (User) context.getBean("user");
        System.out.println(user1);

        User user2 = (User)context.getBean("user");
        System.out.println(user1==user2);

        user2.setName("宏猪");
        System.out.println(user1);

 

 2.3.4最后,附上一个测试
        ApplicationContext context1 = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext context2 = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("context1 和 context2是否是同一个对象:");
        System.out.println(context1==context2);
        System.out.println("=======================================");

        User user1 =  (User)context1.getBean("user");
        User user2 = (User)context2.getBean("user");
        System.out.println("c1 和 c2 获得的同id对象是不是同一个:");
        System.out.println(user1==user2);
        System.out.println("=================================");

        System.out.println("改变user1的属性,user2的属性是否会跟着改变:");
        user1.setName("5555555555555555555");
        System.out.println(user2);

 

            ===================

@Data
public class Constructor {
    public Constructor(){
        System.out.println("Constructor对象被创建出来了");
    }
}

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");


    }
}

 

3. DI

3.1简介

DI,即依赖注入,就是bean对象的创建依赖于容器,bean对象的属性依赖于容器注入。

前面的文章中已经提到了基本类型属性和bean对象属性的注入,这一章节将会讲解其它类型的注入。

list | set | map | props | value | null

3.2 测试

创建如下实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private String name;
    private List hobbys;
    private Set books;
    private Map cards;
    private Properties info;
    private String wife;
}

编写student.xml文件

    
        
        
            
                172
                110
            
        

        
            
                看视频
                写代码
                记笔记
                写博客
            
        

        
            
                java核心技术
                csapp
                jvm
            
        

        
            
                
                
            
        

        
            
        

测试

//    输出结果
//    Student(
//      name=哼哼哼, 
//      hobbys=[看视频, 写代码, 记笔记, 写博客], 
//      books=[java核心技术, csapp, jvm], 
//      cards={身份证=555555111111110202, 学生卡=0000555111}, 
//      info={身高=172, 体重=110}, 
//      wife=null)

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("student.xml");
        Student student = context.getBean("student", Student.class);
        System.out.println(student);

    }

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号