Configuration
From Spring3.0, @ Configuration arranged for defining classes, alternatively xml configuration file, the internal class annotated with one or more methods are annotated @Bean, these methods will be AnnotationConfigApplicationContext AnnotationConfigWebApplicationContext classes or scanning, and with to build bean definition, initialization Spring container.
Normally, before we were Springmvc configuration is that,
@Test
public void run1(){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取对象
AccountService accountService = (AccountService) applicationContext.getBean("accountService");
//调用方法
accountService.findAll();
}
@Configuration marked on the class, such as a spring corresponding to the xml configuration file, function as: a container disposed spring (application context)
package com.dxz.demo.configuration;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration容器启动初始化。。。");
}
}
Two codes are equivalent
package com.dxz.demo.configuration;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfiguration {
public TestConfiguration() {
System.out.println("TestConfiguration容器启动初始化。。。");
}
}
If you want to diy a number of customized functions or just write this component, and then give it to springboot
springboot will help us to automate assembly
springboot of view resolver
package com.mmz.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Locale;
/**
* @Classname MyMvcConfig
* @Description 全面接管/扩展Springmvc
* @Date 2020/2/9 18:12
* @Created by mmz
*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
@Bean
public ViewResolver myMyViewResolver(){
return new MyViewResolver();
}
//自定义了一个视图解析器
public static class MyViewResolver implements ViewResolver{
@Override
public View resolveViewName(String s, Locale locale) throws Exception {
return null;
}
}
}