园区网站建设需求调研报告,精美网站设计,一家做特卖的网站,网站建设推广方式第一个Spring程序 创建新的空工程spring6 设置JDK版本17#xff0c;编译器版本17 设置IDEA的Maven#xff1a;关联自己的maven 在空的工程spring6中创建第一个maven模块#xff1a;spring6-001-first 在pom.xml添加spring context依赖和junit依赖#xff0c; ?x…第一个Spring程序 创建新的空工程spring6 设置JDK版本17编译器版本17 设置IDEA的Maven关联自己的maven 在空的工程spring6中创建第一个maven模块spring6-001-first 在pom.xml添加spring context依赖和junit依赖 ?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdspring-001-first/artifactIdversion1.0-SNAPSHOT/version!--打包方式jar--packagingjar/packaging!--依赖--dependencies!--引入spring context依赖之后表示将Spring的基础依赖引入了--!--想使用spring的jdbc或者其他的tx还需要再次添加依赖--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion6.0.2/version/dependency!--junit依赖--dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/versionscopetest/scope/dependency/dependenciespropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.target/properties/project当加入spring context的依赖之后会关联引入其他依赖 spring aop面向切面编程 spring beansIoC核心 spring corespring的核心工具包 spring jclspring的日志包 spring expressionspring表达式 定义beanUser package com.powernode.spring6.bean;/*** bean,封装用户信息*/
public class User {
}使用IDEA工具自带的spring配置文件的模板创建spring.xml。该文件放在类的根路径下 名字可以任意 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd!--Spring配置文件--!--配置beanspring才可以帮助我们管理这个对象--!--bean标签的两个重要属性id:这个bean的唯一标识不能重复class:必须填写类的全限定类名带包名--bean iduserBean classcom.powernode.spring6.bean.User/
/beans编写测试程序 package com.powernode.spring6.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class FirstSpringTest {Testpublic void testFirstSpringCode(){// 第一步:获取Spring容器对象/*ApplicationContext : spring容器是一个接口接口下有很多实现类有一个实现类ClassPathXmlApplicationContextClassPathXmlApplicationContext 专门从类路径中加载spring配置文件的一个Spring上下文对象这行代码只要执行就相当于启动了Spring容器解析spring.xml文件并且实例化所有的bean对象放到spring容器当中。*/ApplicationContext applicationContext new ClassPathXmlApplicationContext(spring.xml);// 第二步:根据bean的id从Spring容器中获取对象Object userBean applicationContext.getBean(userBean);System.out.println(userBean);}
}详细剖析 bean标签的id属性不可以重复 底层是通过反射机制调用无参数构造方法创建对象的 // dom4j解析beans.xml文件从中获取class的全限定类名
// 通过反射机制调用无参数构造方法创建对象
Class clazz Class.forName(com.powernode.spring6.bean.User);
Object obj clazz.newInstance();创建好的对象是存储到一个Map集合当中 spring配置文件的名字可以是任意的 spring的配置文件可以有多个在ClassPathXmlApplicationContext构造方法的参数上传递文件路径即可。 通过源码可以看到: public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {this(configLocations, true, (ApplicationContext)null);
}在spring配置文件中配置的bean可以是任意类只要这个类不是抽象的并且提供了无参数构造方法。 getBean()方法调用时如果bean的id不存在不会返回null而是出现异常 使用getBean()方法的第二个参数来指定返回的bean的类型 User userBean applicationContext.getBean(userBean, User.class);xml配置文件如果没有在类路径当中使用FileSystemXmlApplicationContext类进行加载配置文件 ApplicationContext applicationContext new FileSystemXmlApplicationContext(d:/spring.xml);这种方式很少用。一般都是将配置文件放到类路径当中这样可移植性更强。 ApplicationContext接口的超级父接口BeanFactory。 BeanFactory翻译为Bean工厂就是能够生产Bean对象的一个工厂对象。 BeanFactory是IoC容器的顶级接口。 Spring的IoC容器底层实际上使用了工厂模式。 Spring底层的IoC是通过 XML解析 工厂模式 反射机制 实现的 不是在调用getBean()方法的时候创建对象执行以下代码的时候就会实例化对象。 new ClassPathXmlApplicationContext(spring.xml);Spring6启用Log4j2日志框架
从Spring5之后Spring框架支持集成的日志框架是Log4j2 引入Log4j2的依赖 !--log4j2的依赖--
dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-core/artifactIdversion2.19.0/version
/dependency
dependencygroupIdorg.apache.logging.log4j/groupIdartifactIdlog4j-slf4j2-impl/artifactIdversion2.19.0/version
/dependency在类的根路径下提供log4j2.xml配置文件文件名固定为log4j2.xml文件必须放到类根路径下。 ?xml version1.0 encodingUTF-8?configurationloggers!--level指定日志级别从低到高的优先级ALL TRACE DEBUG INFO WARN ERROR FATAL OFF--root levelDEBUGappender-ref refspring6log//root/loggersappenders!--输出日志信息到控制台--console namespring6log targetSYSTEM_OUT!--控制日志输出的格式--PatternLayout pattern%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n//console/appenders/configuration配置好了就启用了Log4j2日志框架 自己使用log4j2记录日志信息 // 第一步创建日志记录器对象
// 获取FirstSpringTest类的日志记录器对象也就是说只要是FirstSpringTest类中的代码执行记录日志的话就输出相关的日志信息。
Logger logger LoggerFactory.getLogger(FirstSpringTest.class);// 第二步记录日志根据不同的级别来输出日志
logger.info(一条消息);
logger.debug(一条调试信息);
logger.error(一条错误信息);