zoukankan      html  css  js  c++  java
  • Spring小结

    一.环境搭建

    创建Maven项目

    • 一般pom.xml会出错,本地若无相应版本的jar包,则无法下载或下载速度非常慢,我的解决方案是,查找本地仓库的jar,修改为本地仓库有的jar即可
    • pom.xml的依赖jar可以从Maven搜索

    二.Spring的 IOC(控制反转)与DI(依赖注入)

    IOC(把Bean 的创建与管理交给Spring容器)

    • 使用@Component,@Service,@Controller,@Repository 注解Bean ,让spring管理,四个功能等效,但应该在相对应的组件上标识
    • @Bean方式,在配置类中(有@Configuration注解的类) 用@Bean注解的方法的返回值类型的Bean会被spring管理

    DI 注入相应的Bean

    • @Autowired,@Inject, @Resource,可以注解在属性或者Set方法上(我一般注解在属性上,这样可以省去set方法)

    三.配置类的注解

    • @Configuration标识该类为配置类
    • @ComponentScan(“包名”)让spring扫描该包下的被注解为Bean的类

    四.AOP(切面编程,基于cglib动态代理或者JDK动态代理)

    • @Aspect标注类为切面类
    • @PoinCut(“execution( com.aop.serivce.*(..))”) 定义切点,被注解的方法名为切点名称
    • @Before前置通知,被标注的方法会在目标方法前执行
    • @After后置通知,被注解的方法会在目标方法执行过执行
    • @AfterReturning返回通知,在目标方法执行后切没有异常时执行
    • @AfterThrowing 异常通知,在目标方法执行抛异常时执行
    • 使用时要在配置类上注解@EnableAspectJAutoProxy,开启spring对AspectJ 的支持

    五.常用配置

    @Scope常用 有两种取值 Singleton 和 Prototype

    • Singleton 为默认值 Bean我单例
    • Prototype Bean为多例

    配置文件读取

    1. 在配置类上注解@ProperSource(“文件路径”)
    2. 用@Value(“${key}”)注入配置文件的值

    Bean的初始化和销毁

    • @PostConstruct 注解的方法会在Bean的构造方法执行后执行
    • @PreDestroy 注解的方法会在Bean销毁前执行
    • @profile(“参数值”)注解Bean,可更具@ActiveProfiles(“参数值”)的参数值创建相应的Bean

    六.多线程

    1. 使用@EnableAsync 注解配置类开启对异步任务的支持
    2. 并且要实现AsyncConfiguer接口
    3. 创建一个线程池
    4. 在方法上注解@Async 就说明这个方法为异步方法
    @Configuration
    @ComponentScan("com.sjx.spring2.multithread")
    @EnableAsync//开启异步支持
    public class TaskExecutorConfig implements AsyncConfigurer{
    
        @Override
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(5);
            executor.setMaxPoolSize(10);
            executor.setQueueCapacity(25);
            executor.initialize();
            return executor;
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return null;
        }
    }

    七.计划任务

    1. 在配置类添加注解@EnableSchedule 开启对计划任务的支持
    2. 在方法上注解@Scheduled申明方法为计划方法
    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        System.out.println("当前时间为"+dateFormat.format(new Date()));
    }
    
    @Scheduled(cron="0 17 10 ? * *")//每天的10点17
    public void fixTimeExecution() {
        System.out.println("当定时时间"+dateFormat.format(new Date())+"执行");
    }

    八.集成Junit

    1. @RunWith(SpringJUnit4ClassRunner.class)提供spring测试的上下文环境
    2. @Test标识方法为测试方法
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes={AwareConfig.class})
    public class AwareTest {
        @Autowired
        private AwareService service;
        @Testpublic void test1(){
            service.outputResult();
        }
    }




  • 相关阅读:
    如何在Eclipse中显示行号
    最值得听的100首英文歌
    ffmpeg示例一:源码
    编码解码中常用术语二
    ffmpeg.c(ffmpeg.exe)调试笔记一
    Debug ffmpeg.c & ffmpeg_g.exe in Ubuntu with Eclipse
    ubuntu下编译ffmpeg with libx264
    利用ffmpeg切割与合并视频(一)调用ffmpeg程序直接切割
    VMWare安装Ubuntu 12.10无法开启虚拟机的Unity Mode模式
    Atitit.基于dsl的methodinvoker
  • 原文地址:https://www.cnblogs.com/jeffen/p/6143749.html
Copyright © 2011-2022 走看看