組件對比
1)WebAppInitializer <---> web.xml(部署描述符文件)(
配置 org.springframework.web.servlet.DispatcherServlet(指定dispatcherServletContext.xml)
,
配置CharacterEncodingFilter等filter
,
配置應(yīng)用范圍內(nèi)的初始化參數(shù)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残制,內(nèi)容協(xié)商,視圖解析等configureContentNegotiation掖疮、contentNegotiatingViewResolver
)
4)mvc:resources -- addResourceHandlers //靜態(tài)資源解析(重寫方法)
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>標(biāo)簽
2)indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.指示一個方法實(shí)例化浊闪,配置恼布,初始化,成為一個被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>標(biāo)簽盖腿。
可以通過調(diào)用@Bean methods爽待,實(shí)現(xiàn)inter-bean內(nèi)部bean依賴损同。
2,初始化容器
1)WebApplicationInitializer interface鸟款,用來確保我們實(shí)現(xiàn)被檢測到膏燃,并自動用來初始化Servlet容器。(通過ServletContext)
2)AbstractDispatcherServletInitializer 抽象類何什,實(shí)現(xiàn)了WebApplicationInitializer接口组哩,基于java-based的抽象類AbstractAnnotationConfigDispatcherServletInitializer繼承于它搔啊。
getServletMappings, getServletConfigClasses, getRootConfigClasses
3)實(shí)現(xiàn)類還提供了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包中望迎,開啟事務(wù)==<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 導(dǎo)入一個或者多個配置類
@PropertySources({@PropertySource(value = "classpath:properties/hzq-finance-${hzq.finance.env}.properties")})//-Dhzq.finance.env=local增加啟動參數(shù)
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辩尊,開啟事務(wù)==<tx:annotation-driven />
public class DatabaseConfig implements TransactionManagementConfigurer {}