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();
} } @Configuration
@EnableWebMvc
@ComponentScan
public 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();
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() > ) {
dataSource.setInitialSize(properties.getInitialSize());
}
if (properties.getMinIdle() > ) {
dataSource.setMinIdle(properties.getMinIdle());
}
if (properties.getMaxActive() > ) {
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;
}
}

SPRINGBOOT 读书笔记的更多相关文章

  1. 【读书笔记】SpringBoot读书笔记

    整体目录结构: 一.入门 二.开发第一个应用程序 三.自定义配置 四.测试 五.Groovy与Spring Boot Cli 六.在Spring Boot中使用Grails 七.深入Actuator ...

  2. [读书笔记] 一、Spring boot项目搭建与配置文件

    读书笔记:[JavaEE开发的颠覆者 Spring Boot实战] 作者:汪云飞 从今天开始坚持读书,并记录下此读书笔记. 一,初接触 Spring boot 项目Hello world搭建 1.po ...

  3. 《精通Spring 4.X企业应用开发实战》读书笔记1-1(IoC容器和Bean)

    很长一段时间关注在Java Web开发的方向上,提及到Jave Web开发就绕不开Spring全家桶系列,使用面向百度,谷歌的编程方法能够完成大部分的工作.但是这种不系统的了解总觉得自己的知识有所欠缺 ...

  4. 《深入理解 Java 内存模型》读书笔记

    ![img](https://mmbiz.qpic.cn/mmbiz_jpg/1flHOHZw6RtPu3BNx3zps1JhSmPICRw7QgeOmxOfTbCT3RLgIo4qRpn6xL4qg ...

  5. 转《深入理解 Java 内存模型》读书笔记

    转:https://mp.weixin.qq.com/s/2hA6u4hLEPWlTPdD-XB-bg 前提 <深入理解 Java 内存模型>程晓明著,该书在以前看过一遍,现在学的东西越多 ...

  6. 读书笔记汇总 - SQL必知必会(第4版)

    本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...

  7. 读书笔记--SQL必知必会18--视图

    读书笔记--SQL必知必会18--视图 18.1 视图 视图是虚拟的表,只包含使用时动态检索数据的查询. 也就是说作为视图,它不包含任何列和数据,包含的是一个查询. 18.1.1 为什么使用视图 重用 ...

  8. 《C#本质论》读书笔记(18)多线程处理

    .NET Framework 4.0 看(本质论第3版) .NET Framework 4.5 看(本质论第4版) .NET 4.0为多线程引入了两组新API:TPL(Task Parallel Li ...

  9. C#温故知新:《C#图解教程》读书笔记系列

    一.此书到底何方神圣? 本书是广受赞誉C#图解教程的最新版本.作者在本书中创造了一种全新的可视化叙述方式,以图文并茂的形式.朴实简洁的文字,并辅之以大量表格和代码示例,全面.直观地阐述了C#语言的各种 ...

随机推荐

  1. 查看当前支持的shell,echo -e相关转义符,一个简单shell脚本,dos2unix命令把windows格式转为Linux格式

    /etc/shells [root@localhost ~]# more /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bi ...

  2. CentOS 7部署ASP.NET Core应用程序

    看了几篇大牛写的关于Linux部署ASP.NET Core程序的文章,今天来实战演练一下.2017年最后一个工作日,提前预祝大家伙元旦快乐.不扯淡,直接进入正题.您有任何问题请在评论区留言. 1.环境 ...

  3. iOS 环信集成项目应用

    环信iOS端3.0版本集成记录--聊天界面篇 环信离线推送证书... 1,环信处在后台的时候,消息的接收与推送 离线发推送 配置属性 EMCallOptions *options = [[EMClie ...

  4. qt关键字高亮

    qt的高亮显示主要是使用qsyntaxhighlighter类,由于qsyntaxhighlighter是抽象基类,所以需要继承并自己实现 //头文件 #ifndef MARKDOWN_HIGHLIG ...

  5. 来腾讯云开发者实验室 学习.NET

    腾讯云开发者实验室为开发者提供了一个零门槛的在线实验平台,开发者实验室提供的能力: 零门槛扫码即可免费领取实验机器,支持使用自有机器参与,实验完成后支持保留实验成果: 在线 WEB IDE 支持 sh ...

  6. [置顶] xamarin android使用gps定位获取经纬度

    看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...

  7. 尤克里里 ukulele 单板 非kaka tom uma

    本店冲人气优惠,不搞倒闭之类的事 23寸尤克里里 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 配件选购40元全套(加棉琴包.金属变调夹. ...

  8. Webpack 2 视频教程 006 - 使用快捷方式进行编译

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  9. kafka资料

    https://www.cnblogs.com/the-tops/p/5685955.html

  10. python实现散列表的链表法

    在散列中,链接法是一种最简单的碰撞解决技术,这种方法的原理就是把散列到同一槽中的所有元素 都放在一个链表中. 链接法有两个定理,定理一: 在简单一致散列的假设下,一次不成功查找的期望时间为O(1 + ...