Spring-第二章-第一个Spring程序

1.软件版本

	1. JDK1.8+
	2. Maven3.5+
	3. IDEA2018+
	4. SpringFramework 5.1.4
	官⽅⽹站 www.spring.io

2.环境搭建

  • Spring的jar包
	##设置pom pom 依赖
<!-- https://mvnrepository.com/artifact/org.springframework/springcontext -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
  • spring的配置文件
1.配置文件的放置位置:任意位置 没有硬性的要求
2.配置文件的命名   :没有硬性要求,建议使用:applicationContext.xml
思考:日后应用的Spring框架时,需要进行配置文件路径的设置

在这里插入图片描述

3.Spring的核心API

  • ApplicationContext
1.	作用:Spring提供的ApplicationContext这个工厂,用于对象的创建
2. 	好处:解耦合
  • ApplicationContext接口类型
接⼝:屏蔽实现的差异
⾮web环境 : ClassPathXmlApplicationContext (main junit)
web环境 : XmlWebApplicationContext

在这里插入图片描述

  • 重量级资源
ApplicationContext⼯⼚的对象占⽤⼤量内存。
不会频繁的创建对象 : ⼀个应⽤只会创建⼀个⼯⼚对象。
ApplicationContext⼯⼚:⼀定是线程安全的(多线程并发访问)

4.程序开发

1.创建类型
2.配置文件的配置applicationContext.xml
	<bean id="person" class="com.baizhiedu.basic.Person"/>
3.通过工厂类,获得对象
	 ApplicationContext  ApplicationContext ctx = new
	ClassPathXmlApplicationContext("/applicationContext.xml");
 	Person person = (Person)ctx.getBean("person");	

遇到的问题

class path resource [applicationContext.xml] cannot be opened because it does not exist
	at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:180)

解决方案

把resources目录给配置成资源文件

在这里插入图片描述

4.细节分析

  • 名词解释
Spring 工厂创建的对象,叫做bean或者组件
  • Spring工厂的相关方法
    	//默认的方法 需要类型转换
       people people2 = (people)context.getBean("people");
       System.out.println("people2 = " + people2);
       //通过这种方式获取的对象,就不需要强制类型转换
       System.out.println("--------------------------------------");
       people people = context.getBean("people", people.class);
       System.out.println(people);
       //当前Spring的配置文件中,只能有一个<bean class是people类型
       System.out.println("----------------------------------------");
       people people1 = context.getBean(people.class);
       System.out.println("people1 = " + people1);
       //获取的是 Spring⼯⼚配置⽂件中所有bean标签的id值 person person1
       System.out.println("--------------------------------------");
       String[] beanDefinitionNames = context.getBeanDefinitionNames();
       for (String beanDefinitionName : beanDefinitionNames) {
           System.out.println("beanDefinitionName = " + beanDefinitionName);
       }
       //根据类型获得Spring配置文件中对象的id的值
       String[] beanNamesForType = context.getBeanNamesForType(people.class);
       for (String s : beanNamesForType) {
           System.out.println("s = " + s);
       }
  • 配置文件中需要注意的细节
1. 只配置class属性
		<bean class="com.baizhiedu.basic.Person"/>
		a) 上述这种配置 有没有id值 com.baizhiedu.basic.Person#0
		b) 应⽤场景: 如果这个bean只需要使⽤⼀次,那么就可以省略id值
		 如果这个bean会使⽤多次,或者被其他bean引⽤则需要设置id值
2. name属性
		作⽤:⽤于在Spring的配置⽂件中,为bean对象定义别名(⼩名)
		相同:
		 1. ctx.getBean("id|name")-->object
		 2. <bean id="" class=""
		 等效
		 <bean name="" class=""
区别:
	 1. 别名可以定义多个,但是id属性只能有⼀个值
	 2. XML的id属性的值,命名要求:必须以字⺟开头,字⺟ 数字 下划线 连字符 不
	能以特殊字符开头 /person
	 name属性的值,命名没有要求 /person
	 name属性会应⽤在特殊命名的场景下:/person (spring+struts1)

	 XML发展到了今天:ID属性的限制,不存在 /person
 3. 代码
		 //⽤于判断是否存在指定id值得bean,不能判断name值
		 if (ctx.containsBeanDefinition("person")) {
		 System.out.println("true = " + true);
		 }else{
		 System.out.println("false = " + false);
		 }
		 //⽤于判断是否存在指定id值得bean,也可以判断name值
		 if (ctx.containsBean("p")) {
		 System.out.println("true = " + true);
		 }else{
		 System.out.println("false = " + false);
		 }

Spring工厂的底层实现原理(简易版)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_47298981/article/details/107561369
今日推荐