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

Spring基础 第二章

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

Spring基础 第二章

第二章Spring的初体验 1.环境搭建

Spring jar包


    org.springframework
    spring-context
    5.1.4.RELEASE

Spring的配置文件

    配件文件位置没有要求配置文件名称没有要求以后需要主动告诉Spring
2.Spring的核心API

ApplicationContext

作用:创建对象,解耦合

它是一个接口,屏蔽具体的实现细节

    非web环境: ClassPathXmlApplicationContextweb环境: xmlWebApplicationContext

重量级资源

对象占用大量内存

不会频繁的创建对象: 一个应用只会创建一个对象,面临着多线程问题

ApplicationContext一定是线程安全的(多线程并发访问)

3.程序开发
1.创建类型
2.配置文件 applicationContext
	
3.通过工厂创建对象
	ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
    Person person = (Person) ctx.getBean("person");
4.细节分析

Spring创建的对象也叫做bean或者是component

常用方法

Person person0 = (Person)ctx.getBean("person");
Person person1 = ctx.getBean("person", Person.class);
// 使用这种方式获取对象,配置文件中只能有一个bean标签的类型是Person
Person person2 = ctx.getBean(Person.class);

// 获取Spring配置文件中所有bean的id值
String[] names = ctx.getBeanDefinitionNames();
// 根据类型获取Bean的id值
String[] namesForType = ctx.getBeanNamesForType(Person.class);
// 判断配置文件中是否含有id == person 
boolean containsBeanDefinition = ctx.containsBeanDefinition("person");
// 判断配置文件中的 name == person ||id == person
boolean containsBean = ctx.containsBean("person");

配置文件细节

1.只配置class属性

	Spring默认id值:com.xia.basic.Person#0
	若只使用一次
	但是若使用多次且会被其他bean引用那么就需要起id
2.name属性相当于是bean的小名可以有多个,id是大名是唯一的

5.整合日志框架

pom


     log4j
     log4j
     1.2.17



    org.slf4j
    slf4j-log4j12
    1.7.25

log4j.properties

log4j.rootLogger = debug,console

log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/731554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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