4-springboot知识点(2)-springboot加载xml配置、系统启动任务、整合Web组件、路径映射、类型转换器、整合AOP、自定义欢迎页、自定义favicon、除去自动化配置

一、springboot加载xml配置

  1. bean.xml文件
@Configuration
//spring中的注解。导入xml配置
@ImportResource("classpath:/bean.xml")
public class WebMvcConfig {
    
}
  1. 配置类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="org.lyl.xml.controller.HelloController" id="hello"/>
</beans>
  1. 测试
@SpringBootTest
class XmlApplicationTests {

    @Autowired
    HelloController helloController;

    @Test
    void contextLoads() {
        System.out.println(helloController.hello());
    }

}

二、系统启动任务

介绍:项目启动时会启动一次

  1. CommandLineRunner
  • MyCommandLineRunner1
@Component
//设置优先级:数越大,优先级越低
@Order(value = 99)
public class MyCommandLineRunner1 implements CommandLineRunner {

    //args:从CommandlinerunnerApplication.java中接收过来
    //run:要执行的系统启动任务位置
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner1--------" + Arrays.toString(args));
    }
}
  • MyCommandLineRunner2
@Component
//设置优先级:数越大,优先级越低
@Order(value = 98)
public class MyCommandLineRunner2 implements CommandLineRunner {

    //args:从CommandlinerunnerApplication.java中接收过来
    //run:要执行的系统启动任务位置
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner2--------" + Arrays.toString(args));
    }
}
  • CommandlinerunnerApplication
@SpringBootApplication
public class CommandlinerunnerApplication {
    //方法一:在配置启动的spring boot项目--Configuration--Environment--Program arguments中设置。个数用空格隔开
    //方法二:运用maven的package进行项目打包,然后在Terminal中进入target中,输入java -jar 所用jar包 启动时要设置的任务的args(多个用空格隔开)
    public static void main(String[] args) {
        SpringApplication.run(CommandlinerunnerApplication.class, args);
    }

}

在这里插入图片描述

  1. ApplicationRunner
  • MyApplicationRunner1
@Component
@Order(value = 99)
//与commandLineRunner没有太大的区别,只是参数不一样
public class MyApplicationRunner1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取所有的args.
        //带key的args在写的时候需要加--key=value
        String[] sourceArgs = args.getSourceArgs();
        System.out.println("sourceArgs1-----" + Arrays.toString(sourceArgs));
        //获取不带key的args
        List<String> nonOptionArgs = args.getNonOptionArgs();
        System.out.println("nonOptionArgs1-----" + nonOptionArgs);
        //获取带key的args
        Set<String> optionNames = args.getOptionNames();
        for (String optionName : optionNames) {
            System.out.println("optionNames1-------" + optionName + ":" + args.getOptionValues(optionName));
        }
    }
}

  • MyApplicationRunner2
@Component
//@Order设置优先级
@Order(value = 98)
//与commandLineRunner没有太大的区别,只是参数不一样
public class MyApplicationRunner2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取所有的args
        String[] sourceArgs = args.getSourceArgs();
        System.out.println("sourceArgs2----" + Arrays.toString(sourceArgs));
        //获取不带key的args
        List<String> nonOptionArgs = args.getNonOptionArgs();
        System.out.println("nonOptionArgs2--------" + nonOptionArgs);
        //获取带key的args
        Set<String> optionNames = args.getOptionNames();
        for (String optionName : optionNames) {
            System.out.println("optionNames2------" + optionName + ":" + args.getOptionValues(optionName));
        }
    }
}
  • ApplicationrunnerApplication
@SpringBootApplication
public class ApplicationrunnerApplication {

    //方法一:在配置启动的spring boot项目--Configuration--Environment--Program arguments中设置。个数用空格隔开。其中含key的args用--key=value格式。
    //方法二:运用maven的package进行项目打包,然后在Terminal中进入target中,输入java -jar 所用jar包 启动时要设置的任务的args(多个用空格隔开)
    public static void main(String[] args) {
        SpringApplication.run(ApplicationrunnerApplication.class, args);
    }

}

在这里插入图片描述

  • 运行结果
    在这里插入图片描述

三、整合Web组件

  1. ServletApplication
@SpringBootApplication
@ServletComponentScan(basePackages = "org.lyl.servlet")
public class ServletApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(ServletApplication.class, args);
    }

}
  1. MyServlet
//通过@WebServlet来进行注册
@WebServlet(urlPatterns = "/myServlet")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet");
    }
}
  1. MyFilter
//urlPattern:要拦截的地址
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("MyFilter");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
  1. MyListener
@WebListener
public class MyListener implements ServletRequestListener {
    @Override
    public void requestDestroyed(ServletRequestEvent sre) {
        System.out.println("MyListener-----requestDestroyed");
    }
    @Override
    public void requestInitialized(ServletRequestEvent sre) {
        System.out.println("MyListener-----requestInitialized");
    }
}
  1. 访问:http://localhost:8080/myServlet
  2. 运行结果
    在这里插入图片描述

四、路径映射

  1. WebMvcConfig
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    //使只显示样式的动态页面不需要通过控制器进行访问
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
    //setViewName:动态页面的名称。
    //addViewController:要访问的路径
        registry.addViewController("/login").setViewName("login");
    }
}

五、类型转换器

  1. 自定义字符串转日期组件
@Component
public class StringToDateConverter implements Converter<String, Date> {
    @Override
    public Date convert(String source) {
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
        if(source != null && !"".equals(source)) {
            try {
                return sdf.parse(source);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

六、整合AOP

  1. pom.xml
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
  1. LogComponent
//@Component:定义为一个spring组件
@Component
//@Aspect:表示为一个切面
@Aspect
public class LogComponent {

    //@Pointcut:定义拦截规则
    //excution:
    // 第一个*:代表返回值.
    // 第二个*:代表任意类.
    // 第三个*:代表任意方法.
    // ..:参数任意(1.参数类型任意。2.参数个数任意)

    @Pointcut("execution(* org.lyl.aop.service.*.*(..) )")
    public void pc1() {
    }

    //前置通知
    @Before(value = "pc1()")
    public void before(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("before----------" + name);
    }

    //后置通知
    @After(value = "pc1()")
    public void after(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("after----------" + name);
    }

    //返回通知
    //在该方法中获取拦截方法的返回值(returning)
    @AfterReturning(value = "pc1()",returning = "result")
    public void afterReturning(JoinPoint joinPoint,Object result) {
        String name = joinPoint.getSignature().getName();
        System.out.println("afterReturning----------" + name + "--------" + result);
    }

    //异常通知
    //throwing:抛出的异常是什么
    @AfterThrowing(value = "pc1()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e) {
        String name = joinPoint.getSignature().getName();
        System.out.println("afterThrowing----------" + name + "---------" + e.getMessage());
    }

    //环绕通知:是以上四个的综合
    //返回的值会影响到真正方法的值
    @Around(value = "pc1()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        //在proceed前面写的就先执行
        Object proceed = pjp.proceed();
        //afterReturning(pjp,proceed);
        return "123";
    }
}

七、自定义欢迎页

介绍:在静态页面路径下或者动态路径下存在index的页面,则访问时直接加载该页面。
注:先静后动

八、自定义favicon

  1. favicon制作网站:https://tool.lu/favicon
  2. 存放位置:1.resources/static。2.resources
  3. 优先级:resources/static>resources

九、除去自动化配置

  1. 第一种方法
--application.properties
#第一种方法:去除自动化配置
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
  1. 第二种方法
//第二种方法:去除WebMvcAutoConfiguration.class
//@SpringBootApplication(exclude = WebMvcAutoConfiguration.class)
@SpringBootApplication
public class WelcomeApplication {

    public static void main(String[] args) {
        SpringApplication.run(WelcomeApplication.class, args);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值