美文网首页
Spring in action 阅读笔记1

Spring in action 阅读笔记1

作者: 言西枣 | 来源:发表于2017-05-25 09:33 被阅读19次

    第一章

    简化Java开发

    • 基于POJO的轻量级和最小侵入性编程
    • 通过依赖注入和面向接口实现松耦合
    • 基于切面和惯例进行声明式编程
    • 通过切面和模板减少样板式代码

    第二章

    2.1Spring配置方案

    • XML
    • Java Config
    • 自动化装配
    2.1.1自动化装配

    A. 组件扫描(component scan)
    自动发现配置

    配置方式 使用元素
    @Configuration @ComponentScan(value="package", basePackages={"",""})
    XML <context:component-scan>

    bean注解

    Spring JSR330(Java Dependency Injection) value属性
    @Component @Named bean id

    B. 自动装配(autowiring)

    Spring|JSR330(Java Dependency Injection)
    ---|---|---
    @Autowired|@Inject

    2.1.2JavaConfig装配
    • 主要用于将第三方库的组件装配为bean
    • 配置代码,不应包含业务逻辑
    • 强大,类型安全,重构友好
      @Bean注解
      方法名或者注解name属性作为bean的id,Spring会拦截所有@Bean标注的方法的调用,不会每次都进行实际的调用,而会直接返回该方法创建的bean(所以多次调用返回的是同一个对象)
    2.1.3XML装配

    <construct-arg> 构造器配置
    <property> setter配置
    <util:list> util命名空间将某个类型的值或者引用暴露为bean

    第三章

    3.1 Profile

    @Profile("dev") | <beans profile="dev">

    通过spring.profiles.active和spring.profiles.default来配置,可以在如下的地方配置;

    • DispatcherServlet的初始化参数<init-param>
    • Web应用的上下文参数<context-param>
    • JNDI条目
    • 环境变量
    • JVM系统属性
    • 测试类上,@ActiveProfiles
    3.2 条件bean

    @Conditional(Condition.class)

    public interface Condition {
    
        /**
         * Determine if the condition matches.
         * @param context the condition context
         * @param metadata metadata of the {@link org.springframework.core.type.AnnotationMetadata class}
         * or {@link org.springframework.core.type.MethodMetadata method} being checked.
         * @return {@code true} if the condition matches and the component can be registered
         * or {@code false} to veto registration.
         */
        boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
    
    }
    
    3.3消除自动装配的歧义

    @Autowired and @Inject

    1. Matches by Type
    2. Restricts by Qualifiers
    3. Matches by Name

    @Resource

    1. Matches by Name
    2. Matches by Type
    3. Restricts by Qualifiers (ignored if match is found by name)
    3.4bean作用域

    @Scope(value=WebApplicationContext.SCOPE_SESSION, proxyMode=ScopedProxyMode.INTERFACES)

    <bean scope="session">
    <aop:scoped-proxy proxy-target-class="false" />
    </bean>

    3.5运行时值注入
    • property placeholder
    • SpEL
    3.5.1placeholder
    1. @PropertySource("properties") 配置文件源
    2. @Bean PropertySourcesPlaceholderConfigurer | <context:property-placeholder> 解析占位符${}
    3. 使用占位符->Environment
    3.5.2SpEL
    • 使用bean id引用bean
    • 调用方法和访问属性
    • 进行逻辑、关系和算术运算
    • 正则表达式 matches 返回true|false
    • 集合操作
      [index]
      .?[test expression] 用于生成满足条件的子集
      .^[test] | .$[test] 查询第一个和最后一个匹配项
      .![attribute] 投影元素的特定属性,生成这个属性的集合

    "#{1}" 字面值
    "#{beanid.method()|attribute}" 获取bean的方法和属性
    "#{T(com.meizu.yard.spring.Env).getProperty('ebk.database.url')}" 读取配置文件(类作用域的方法和属性)
    "#{systemProperties['jdk']}" 系统属性

    相关文章

      网友评论

          本文标题:Spring in action 阅读笔记1

          本文链接:https://www.haomeiwen.com/subject/gntuxxtx.html