网站推广的必要性长沙网站优化页面
网站推广的必要性,长沙网站优化页面,erp系统登录入口,网站模板搭建记录在听黑马课的时候的笔记以及课堂上练习的代码#xff0c;文章图源于我在听课的时候所截的屏#xff0c;所以有些不清晰#xff0c;请见谅。下面是课程链接#xff0c;可点击自行跳转。
【黑马程序员JavaWeb开发教程#xff0c;实现javaweb企业开发全流程#xff08;…记录在听黑马课的时候的笔记以及课堂上练习的代码文章图源于我在听课的时候所截的屏所以有些不清晰请见谅。下面是课程链接可点击自行跳转。【黑马程序员JavaWeb开发教程实现javaweb企业开发全流程涵盖SpringMyBatisSpringMVCSpringBoot等】https://www.bilibili.com/video/BV1m84y1w7Tb/?share_sourcecopy_webvd_sourced521b664e1113402904fa9336bd1d0ac目录配置优先级bean的管理bean的获取bean的作用域第三方beanSpringBoot原理起步依赖自动配置原理源码跟踪分析条件装配组件Conditional注解案例--自定义starterWeb后端开发总结配置优先级命令行参数优先级大于Java系统属性优先级bean的管理bean的获取获取bean实现package com.jianglin; import com.jianglin.controller.DeptController; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; SpringBootTest public class BeanTest { Autowired private ApplicationContext applicationContext; Test public void testGetBean(){ //根据bean的名称获取 DeptController bean1 (DeptController) applicationContext.getBean(deptController); System.out.println(bean1); //根据bean的类型获取 DeptController bean2 (DeptController) applicationContext.getBean(DeptController.class); System.out.println(bean2); //根据bean的名称及类型获取 DeptController bean3 (DeptController) applicationContext.getBean(deptController,DeptController.class); System.out.println(bean3); } }都是同一个bean对象bean的作用域利用注解配置作用域//DeptController.java package com.jianglin.controller; import com.jianglin.pojo.Dept; import com.jianglin.pojo.Result; import com.jianglin.service.DeptService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Scope; import org.springframework.web.bind.annotation.*; import java.util.List; /** * 增加log日志 * **/ Slf4j RequestMapping(/depts) RestController //Lazy //延迟初始化至第一次创建bean对象 Scope(prototype)//使bean对象不再是单例创建 public class DeptController { Autowired private DeptService deptService; //无参构造方法实现便于观察bean对象的初始化 public DeptController(){ System.out.println(DeptController constructor...); } /** * 查询所有部门信息 * */ GetMapping public Result list(){ log.info(查询全部部门数据); ListDept deptList deptService.list(); return Result.success(deptList); } /** * 根据id进行删除部门信息 * */ DeleteMapping(/{id}) public Result delete(PathVariable Integer id){ log.info(根据{}进行删除部门信息,id); deptService.delete(id); return Result.success(); } /** * 新增部门信息 * */ PostMapping public Result add(RequestBody Dept dept){ log.info(新增部门信息); deptService.add(dept); return Result.success(); } /** * 根据ID查询部门数据 * */ GetMapping(/{id}) public Result getById(PathVariable Integer id){ log.info(根据ID查询部门数据); Dept dept deptService.getById(id); return Result.success(dept); } /** * 修改部门数据 * */ PutMapping public Result update(RequestBody Dept dept){ log.info(修改部门数据); deptService.update(dept); return Result.success(); } } //BeanTest.java package com.jianglin; import com.jianglin.controller.DeptController; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; SpringBootTest public class BeanTest { Autowired private ApplicationContext applicationContext; Test public void testGetBean(){ //根据bean的名称获取 DeptController bean1 (DeptController) applicationContext.getBean(deptController); System.out.println(bean1); //根据bean的类型获取 DeptController bean2 (DeptController) applicationContext.getBean(DeptController.class); System.out.println(bean2); //根据bean的名称及类型获取 DeptController bean3 (DeptController) applicationContext.getBean(deptController,DeptController.class); System.out.println(bean3); } Test public void testBean(){ for (int i 0; i 10; i) { DeptController bean (DeptController) applicationContext.getBean(deptController); System.out.println(bean); } } }每次新建bean对象时都会进行初始化。第三方beanbean对象就是交给ioc容器管理之后每一次使用时都不用再去new一个对象了节约了资源用的时候直接加一个依赖注入就可以了。第三方bean配置//CommonConfig.java package com.jianglin.config; import com.jianglin.service.impl.DeptServiceImpl; import org.dom4j.io.SAXReader; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class CommonConfig { //声明第三方bean Bean //将当前方法的返回值对象交给IOC容器管理成为IOC容器bean //通过Bean注解的name/value属性指定bean名称如果未指定默认是方法名 //也可以添加依赖注入DeptServiceImpl deptServiceimpl public SAXReader saxReader(DeptServiceImpl deptServiceimpl) { System.out.println(deptServiceimpl); SAXReader saxReader new SAXReader(); return saxReader; } } //pom.xml !--Dom4j-- dependency groupIdorg.dom4j/groupId artifactIddom4j/artifactId version2.1.3/version /dependency //BeanTest.java package com.jianglin; import com.jianglin.controller.DeptController; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationContext; SpringBootTest public class BeanTest { Autowired private ApplicationContext applicationContext; Test public void testGetBean(){ //根据bean的名称获取 DeptController bean1 (DeptController) applicationContext.getBean(deptController); System.out.println(bean1); //根据bean的类型获取 DeptController bean2 (DeptController) applicationContext.getBean(DeptController.class); System.out.println(bean2); //根据bean的名称及类型获取 DeptController bean3 (DeptController) applicationContext.getBean(deptController,DeptController.class); System.out.println(bean3); } Test public void testBean(){ for (int i 0; i 10; i) { DeptController bean (DeptController) applicationContext.getBean(deptController); System.out.println(bean); } } Autowired private SAXReader saxReader; //第三方bean的管理 Test public void testThirdBean() throws Exception { // SAXReader saxReader new SAXReader(); Document document saxReader.read(this.getClass().getClassLoader().getResource(static/1.xml)); Element rootElement document.getRootElement(); String name rootElement.element(name).getText(); String age rootElement.element(age).getText(); System.out.println(name : age); } } //TliasApplication.java package com.jianglin; import org.dom4j.io.SAXReader; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.context.annotation.Bean; ServletComponentScan SpringBootApplication public class TliasApplication { public static void main(String[] args) { SpringApplication.run(TliasApplication.class, args); } //不推荐这种方式影响启动类的整洁性 // Bean // public SAXReader saxReader() { // SAXReader saxReader new SAXReader(); // return saxReader; // } }component及衍生注解与bean注解使用场景项目中自定义的使用component及其衍生注解项目中引入第三方的使用bean注解SpringBoot原理起步依赖自动配置原理自动配置实现import com.google.gson.Gson; import com.jianglin.pojo.Result; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; /** * 自动配置原理测试 */ SpringBootTest public class AutoConfigurationTests { Autowired private Gson gson; Test public void testJson(){ String json gson.toJson(Result.success()); System.out.println(json); } }自动配置用Import导入//EnableHeaderConfig.java package com.example; import org.springframework.context.annotation.Import; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; Retention(RetentionPolicy.RUNTIME) Target(ElementType.TYPE) Import(MyImportSelector.class) public interface EnableHeaderConfig { } //SpringbootWebConfig2Application.java import com.example.MyImportSelector; import com.example.TokenParser; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Import; //ComponentScan({com.example,com.itheima}) //Import({TokenParser.class}) //导入普通类交给IOC容器管理 //Import({HeaderConfig.class}) //导入配置类交给IOC容器管理 // Import({MyImportSelector.class}) //导入ImportSelector接口实现类 EnableHeaderConfig//直接导入整个第三方包com.example SpringBootApplication public class SpringbootWebConfig2Application { public static void main(String[] args) { SpringApplication.run(SpringbootWebConfig2Application.class, args); } /*...*/ } //MyImportSelector.java package com.example; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; public class MyImportSelector implements ImportSelector { Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[]{com.example.HeaderConfig}; } }源码跟踪分析条件装配组件Conditional注解条件装配Conditional注解实现//HeaderConfig.java import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; Configuration public class HeaderConfig { Bean //ConditionalOnClass(name io.jsonwebtoken.Jwts) //环境中存在指定的这个类才会将该bean加入IOC容器中 //ConditionalOnMissingBean //不存在该类型的bean才会将该bean加入IOC容器中 —— 指定类型(value属性) 或 名称(name属性) ConditionalOnProperty(name name, havingValue itheima) //配置文件中存在指定的属性与值才会将该bean加入IOC容器中 public HeaderParser headerParser(){ return new HeaderParser(); } Bean public HeaderGenerator headerGenerator(){ return new HeaderGenerator(); } } //Test.java中的代码 //获取TokenParser Test public void testTokenParser() { System.out.println(applicationContext.getBean(TokenParser.class)); } //获取HeaderParser Test public void testHeaderParser() { System.out.println(applicationContext.getBean(HeaderParser.class)); } //获取HeaderGenerator Test public void testHeaderGenerator() { System.out.println(applicationContext.getBean(HeaderGenerator.class)); } //配置文件信息 spring: #数据库连接信息 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/tlias username: root password: 1234 #Mybatis配置 mybatis: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true name: itheima案例--自定义starterWeb后端开发总结这篇文章就先更新到这里接下来的内容可查看我的下一篇博客感谢观看希望对你有帮助。