组件对比
1)WebAppInitializer <---> web.xml(部署描述符文件)(
配置 org.springframework.web.servlet.DispatcherServlet(指定dispatcherServletContext.xml)
,
配置CharacterEncodingFilter等filter
,
配置应用范围内的初始化参数context-param,指定applicationContext.xml和applicationContext-shiro.xml的位置
)
2)AppConfig.class <---> applicationContext.xml(配置dataSource,redis,jdbcTemplate等
)Appconfig中不能写@Value
ApplicationContext 指的是spring里面的Bean工厂,配置通用的bean。
3)WebConfig.class <----> Spring DispatherServlet的配置文件dispatherServletContent.xml(配置component-scan,interceptor,内容协商,视图解析等configureContentNegotiation、contentNegotiatingViewResolver
)
4)mvc:resources -- addResourceHandlers //静态资源解析(重写方法)
mvc:view-controller -- addViewControllers// 解析视图(重写方法)
CommonsMultipartResolver //文件上传表单的视图解析器
1,@Configuration-annotated classes and @Bean-annotated methods.
1)@Bean annotation plays the same role as the <bean/> element。
效果等同于xml的<bean class="">property name="" value=""/></bean>标签
2)indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.指示一个方法实例化,配置,初始化,成为一个被spring IoC容器管理的对象
3)Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions.注解一个类,使用@Configuration,作为bean定义的源。 效果等同于xml的<beans></beans>标签。
可以通过调用@Bean methods,实现inter-bean内部bean依赖。
2,初始化容器
1)WebApplicationInitializer interface,用来确保我们实现被检测到,并自动用来初始化Servlet容器。(通过ServletContext)
2)AbstractDispatcherServletInitializer 抽象类,实现了WebApplicationInitializer接口,基于java-based的抽象类AbstractAnnotationConfigDispatcherServletInitializer继承于它。
getServletMappings, getServletConfigClasses, getRootConfigClasses
3)实现类还提供了getServletFilters,来方便的添加Filter。
4)重写onStartup, 配置filter和listener等。(eg: shiroFilter)
3,WebConfig
@Configuration//<beans>
@EnableWebMvc//<mvc:annotation-driven/>
@EnableAspectJAutoProxy//<aop:aspectj-autoproxy>
@EnableAsync//<task.*>
@ComponentScan(basePackages = "com.hzq")
public class WebConfig extends WebMvcConfigurerAdapter {
// 等效于<mvc:annotation-driven />
}```
@EnableWebMvc,注册了RequestMappingHandlerMapping,RequestMappingHandlerAdapter,提供了processing requests的支持(使用@RequestMapping注解的controller方法)。
mvc:interceptors -- addInterceptors(重写方法),添加拦截器
####4, AppConfig
>
```java
@Configuration
@EnableTransactionManagement//在spring-orm包中,开启事务==<tx:annotation-driven />
@EnableAspectJAutoProxy
@EnableAsync
@EnableScheduling
@ComponentScan(basePackages = "com.hzq",//context:component-scan -- @ComponentScan //包扫描
excludeFilters = {@ComponentScan.Filter(Configuration.class),
@ComponentScan.Filter(Controller.class),
@ComponentScan.Filter(ControllerAdvice.class)})
@Import({DatabaseConfig.class, RedisConfig.class})//@Import 导入一个或者多个配置类
@PropertySources({@PropertySource(value = "classpath:properties/hzq-finance-${hzq.finance.env}.properties")})//-Dhzq.finance.env=local增加启动参数
public class AppConfig {
/**
* 让spring正确解析出${} --> context:property-placeholder
*/
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
5,RedisConfig
@Configuration
public class RedisConfig {
}
6,DataBaseConfig
@Configuration
@EnableTransactionManagement//在spring-orm,开启事务==<tx:annotation-driven />
public class DatabaseConfig implements TransactionManagementConfigurer {}