標籤:web項目 ring ado ddr multi auto aspectj public man
Spring基礎Spring的發展
xml配置 註解配置 Java配置
Spring模組:核心容器 AOP 訊息 web 資料訪問整合
常用的:@Component @Service @Repository @Controller @Configuration @Bean
AOP相關註解
@EnableAspectJAutoProxy @Aspect @PoinCut @After @Before @BeforeThrowing @AtferThrowing @Around
切入點定義
execution(* com..*.* (...))
within(com.text.*)包下任意類
this(com.test.lntf)實現Intf介面的所有類或單個類
@within/target(com.xxx.Transactional)帶有Transactional標註的所有類
@annotion(com.xxx.Transactional)帶有Transactional標註的所有類的任意方法
@args(com.xxx.Transactional)參數是帶有Transactional標註的所有類
args(String)參數類型
SCOPE
@Scope("prototype")或singleton Request Session
@Value(EL)
注入一般字元
作業系統屬性
運算式結果
其他Bean屬性
檔案內容(Resource)
網址內容(Resource)
屬性檔案,注入設定檔需要使用@PropertySource指定檔案地址
在使用@Value注入要配置PropertySourcesPlaceholderConfigurer的Bean
@Bean初始化和銷毀
@Bean(initMethod="init", destroyMethod="destroy")
@Profile
指定profile
1、設定Environment的ActiveProfies
2、jvm的spring.profiles.active
3、servlet的context parameter
事件
1、自訂事件,繼承ApplicationEvent
2、定義事件監聽器,實現ApplicationListener
3、使用容器發布事件
Spring Aware
BeanNameAware
BeanFactoryAware AppiicationContextAware
MessageSourceAware
ApplicationEventPublisherAware
ResourceLoaderAware
繼承上述介面,使用容器提供的功能
此外,可以直接以@Autowired方法向Bean注入
計劃任務
1、配置類使用@EnablbeScheduling
2、方法上使用@Scheduled(fixedRate/fixedDelay/cron)
多線程
1、配置類使用@EnableAsync註解開啟非同步任務支援
2、配置類實現AsyncConfigurer介面並重寫getAsyncExecutor方法,返回ThreadPoolTaskExecutor
3、方法上使用@Async,表明該方法是一個非同步方法呼叫
測試
@RunWith
@ContextConfiguration
@ActiveProfiles
SPRINGMVC基礎搭建web項目
1、配置類使用@EnableWebMvc,可以繼承WebMvcConfigureAdapter,重寫其方法,完成SpringMVC的配置
2、實現WebApplicationInitializer介面,完成web配置
3、打包處理
public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext arg0) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(MyMvcConfig.class); ctx.setServletContext(arg0); Dynamic servlet = arg0.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); }}@Configuration@EnableWebMvc@ComponentScanpublic class MyMvcConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); return viewResolver; } @Bean public DemoInterceptor demoInterceptor() { return new DemoInterceptor(); } @Bean public MultipartResolver multipartResolver() { CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(); multipartResolver.setMaxUploadSize(1000000); return multipartResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(demoInterceptor()); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/index").setViewName("/index"); registry.addViewController("/toUpload").setViewName("/upload"); }}資源放於src/main/resource下,打包後會置於WEB/INF/下常用註解
@Controller @RequestMapping @ResponseBody @RequestBody @PathVariable @RestController
@ControllerAdvice組成
@ExceptionHandler @ModelAttribute @InitBinder
HttpMessageConverter用於處理request和response資料測試
@RunWith
@ContextConfiguration(MVC配置類)
@WebAppconfiguration(資源檔位置)
SpringBoot基礎測試
@RunWith
@SpringApplicationConfiguration
@WebAppConfiguration
入口@SpringBootApplication實際上開啟了
Configuration
EnableAutoConfiguration
ComponentScan
利用型別安全配置@ConfigurationProperties
@ConfigurationProperties(prefix = "druid")public class DruidProperties { private String url; ... }@Configuration@EnableConfigurationProperties(DruidProperties.class)@ConditionalOnClass(DruidDataSource.class)@ConditionalOnProperty(prefix = "druid", name = "url")@AutoConfigureBefore(DataSourceAutoConfiguration.class)public class JpaConfig { @Autowired private DruidProperties properties; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(properties.getUrl()); dataSource.setUsername(properties.getUsername()); dataSource.setPassword(properties.getPassword()); if (properties.getInitialSize() > 0) { dataSource.setInitialSize(properties.getInitialSize()); } if (properties.getMinIdle() > 0) { dataSource.setMinIdle(properties.getMinIdle()); } if (properties.getMaxActive() > 0) { dataSource.setMaxActive(properties.getMaxActive()); } dataSource.setTestOnBorrow(properties.isTestOnBorrow()); try { dataSource.init(); } catch (SQLException e) { throw new RuntimeException(e); } return dataSource; } @Bean PlatformTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setDataSource(dataSource()); return transactionManager; }}
SPRING 學習開發筆記