@Configuration
//spring中的注解。导入xml配置
@ImportResource("classpath:/bean.xml")
public class WebMvcConfig {
}
<?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>
@SpringBootTest
class XmlApplicationTests {
@Autowired
HelloController helloController;
@Test
void contextLoads() {
System.out.println(helloController.hello());
}
}
介绍:项目启动时会启动一次
@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));
}
}
@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));
}
}
@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);
}
}
@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));
}
}
}
@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));
}
}
}
@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);
}
}
@SpringBootApplication
@ServletComponentScan(basePackages = "org.lyl.servlet")
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
//通过@WebServlet来进行注册
@WebServlet(urlPatterns = "/myServlet")
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("MyServlet");
}
}
//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);
}
}
@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");
}
}
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
//使只显示样式的动态页面不需要通过控制器进行访问
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//setViewName:动态页面的名称。
//addViewController:要访问的路径
registry.addViewController("/login").setViewName("login");
}
}
@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;
}
}
<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>
//@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的页面,则访问时直接加载该页面。
注:先静后动
--application.properties
#第一种方法:去除自动化配置
#spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
//第二种方法:去除WebMvcAutoConfiguration.class
//@SpringBootApplication(exclude = WebMvcAutoConfiguration.class)
@SpringBootApplication
public class WelcomeApplication {
public static void main(String[] args) {
SpringApplication.run(WelcomeApplication.class, args);
}
}