spring-usage-log

113 阅读2分钟

spring-usage-log

bean-config

Spring配置bean的方式

  • xml
  • javaConfig,@Configuration,主要指替代xml功能的注解

xml

<beans profile="dev">
  <bean id="beanname" class="XX"/>
</beans>

java_config

传统的Spring一般都是基本xml配置的,spring3.0新增了许多java config的注解,特别是spring boot;java config是指基于java配置的spring.

// @Configuration, 表示这个类是配置类 =><beans
// @Bean,bean的定义 => <bean id=
// @ComponentScan => <context:componentscan basepakage=
// @EnableWebMvc  => <mvc:annotation-driven>
// @ImportResource => <import resource=
// @PropertySource, 用于读取properties文件
// @Profile,一般用于多环境配置,激活时可用@ActiveProfiles注解,@ActiveProfiles("dev")

vs

语法检测的时机 XML唯一的问题是,只有在运行时环境时你才能发现各种配置及语法错误, 但是如果使用Spring IDE Plugin(或者STS)的话,它会在编码时提示这些问题。

dynamic-proxy

bean动态代理加载

  1. spring默认使用JDK
  2. springboot默认bean使用cglib,aop还是使用的JDK

threadpool-config

spring使用线程池的地方:1.异步,2.job; 最好不要使用默认的线程池,使用自己定义的线程池

使用步骤

  1. java-code
// 异步注解
// @EnableAsync(proxy-target-class="true") + @Async("creditAsync")
// job注解
// @EnableScheduling + @Scheduled
  1. xml-config
<task:annotation-driven executor="creditAsync" proxy-target-class="true"/>
<task:executor id="creditAsync"
                   pool-size="5-50"
                   queue-capacity="1000"
                   keep-alive="120"
                   rejection-policy="CALLER_RUNS"/>

spring-schema

spring-boot注解

@EnableXXX

  • @EnableCaching
  • @EnableTransactionManagement

@ConditionalXXX

org.springframework.boot.autoconfigure.condition

  • @ConditionalOnBean
  • @ConditionalOnClass
  • @ConditionalOnExpression
  • @ConditionalOnMissingBean
  • @ConditionalOnMissingClass
  • @ConditionalOnResource
  • @ConditionalOnWebApplication
  • @ConditionalOnNotWebApplication

@Configuration

  • @ConfigurationProperties(prefix = "spring.data.mongodb")
    • 等价与@Configuration + @Value(spring4)
  • @Configuration + @Bean

@Autowired@Recource@Qualifier

  • @Autowired:默认byType;同一Type多个实现时,可以通过Qualifier指定注入的名字,或者在@Bean增加@Primary指定加载顺序
  • @Qualifier:指定注入的名字,与@Autowired结合使用
  • @Primary:通过指定同一Type的加载顺序,与@Bean结合使用
  • @Recource:默认byName,是javax语法,其他是spring语法

mybatis

sql-connector

  • com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的
  • com.mysql.jdbc.Driver 是 mysql-connector-java 5中的

mybatis-spring

springboot 集成MyBatis的2个方法: mybatis-spring 或者 mybatis-spring-boot-starter

  1. mybatis-spring
//依赖
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
</dependency>
// 自己实现映射
MyBatisConfig
  1. mybatis-spring-boot-starter
//依赖
   <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis-spring</artifactId>
   </dependency>
// @MapperScan, @Mapper 

springboot-maven

spring-boot-maven

Main-Class=org.springframework.boot.loader.JarLauncher, spring-boot通过该类间接的执行配置的主类Start-Class

Manifest-Version: 1.0
Implementation-Title: spring-boot
Implementation-Version: 1.0-SNAPSHOT
Built-By: a002
Implementation-Vendor-Id: cook.java
Spring-Boot-Version: 2.0.0.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.cook.boot.web.AppStarter
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
ot-starter-parent/spring-boot
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!--指定了执行的主类-->
                <mainClass>org.rainbow.spring.boot.Application</mainClass>
            </configuration>
            <!--插件默认把repackage绑定到maven的package生命周期上,等价于: mvn clean package spring-boot:repackage-->
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

  <!--OR, 主动查询项目内第一个包含main的主类,不通过指定
   <configuration>
        <executable>true</executable>
   </configuration>
   -->

maven-shade

mave打包的启动类:jar启动在MANIFEST.MF中指定Main-Class属性

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: a002
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_131
Main-Class: com.cook.bug.bra.BraMain

next