• Java


    注解

    @SpringBootApplication:@Configuration + @EnableAutoConfiguration + @ComponentScan

    • @EnableAutoConfiguration:自动配置springboot
    • @ComponentScan:自动扫描和装配所有的Spring组件,包括@Configuration类
    @SpringBootApplication
    public class AfvappApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(AfvappApplication.class, args);
    	}
    }
    

    @Controller:类注解,控制器层,类中方法配合@RequestMapping
    @RestController:类注解,控制器层,REST风格,@Controller + @ResponseBody

    @RestController
    @RequestMapping("/demo")
    public class DemoController {
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String hello() { ... }
    }
    

    @Repository:确保DAO或者repositories提供异常转译

    @Repository
    public interface SqlserverMapper {
        List<String> listVerifyRecords();
    }
    

    @Qualifier:@Qualifier(“name”) + @Autowired
    @Resource

    @Autowired
    @Qualifier(value = "sqlserverDataSource")
    private DataSource sqlserverDataSource;
    
    @Resource(name = "yt_MATHandleServiceImpl")
    private MATHandleService ytService;
    @Resource(name = "cw_MATHandleServiceImpl")
    private MATHandleService cwService;
    

    全局异常处理
    @ControllerAdvice:包含@Component,可以被扫描到,统一处理异常
    @ExceptionHandler(Exception.class):方法注解,表示遇到该异常就执行以下方法

    其他注解

    @Service:类注解,服务层
    @Component:泛指组件
    

    单元测试

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
          <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
          </exclusion>
        </exclusions>
    </dependency>
    

    springboot 2.2.2.RELEASE默认绑定

    org.junit.jupiter:5.5.2
    org.junit.platform:1.5.2
    org.junit.vintage:5.5.2
    

    spring-boot-test自带测试工具

    • JUnit:Java应用程序单元测试标准类库 Junit + Jmockit
    • Spring Test & Spring Boot Test:SpringBoot应用程序功能集成化测试支持
    • Mockito:Java Mock测试框架:(org.mockito v3.1.0)
    • AssertJ:轻量级的断言类库
    • Hamcrest:对象匹配器类库
    • JSONassert:用于JSON的断言库
    • JsonPath:JSON操作类库

    编译时若提示Failed to resolve org.junit.platform:junit-platform-launcher,再引入即可

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.5.2</version>
        <scope>test</scope>
    </dependency>
    

    类注解@RunWith(SpringRunner.class) + @SpringBootTest 和 方法注解@Test

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class MySbUnitTests {
    	@Test
    	public void hello() { ... }
    }
    

    Controller层测试:MockMvc

    public class HelloControlerTests {
        private MockMvc mvc;
      
        @Before //初始化执行
        public void setUp() throws Exception {
            mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
        }
    
        @Test //验证controller是否正常响应并打印返回结果
        public void testHello() throws Exception {
            mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print())
                    .andReturn();
        }
    }
    

    热部署

    <dependencies> <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
    </dependency> </dependencies>
    <build> <plugins> <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration> <fork>true</fork> </configuration>
    </plugin> </plugins> </build>
    

    基础参见:springboot开发-测试-打包-部署-验证简易流程

    打包

    本地调试运行命令:mvn spring-boot:run
    jar包:直接执行
    方式:mvn clean package,详细编译配置参考

    <build>
    	<resources>
    		<resource>
    			<directory>src/main/resources</directory>
    			<includes>
    				<include>static/*.*</include>
    				<include>templates/*.*</include>
    				<include>**/*.xml</include>
    				<include>**/*.yml</include>
    				<include>**/*.properties</include>
    			</includes>
    			<filtering>false</filtering>
    		</resource>
    		<resource>
    			<directory>src/main/java</directory>
    			<includes>
    				<include>**/*.xml</include>
    			</includes>
    			<filtering>false</filtering>
    		</resource>
    	</resources>
    
    	<plugins>
    		<plugin>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-maven-plugin</artifactId>
    		</plugin>
    	</plugins>
    	<finalName>mytest-1.0.0</finalName>
    </build>
    

    war包:放到tomcat服务器下运行

  • 相关阅读:
    07.swoole学习笔记--tcp客户端
    06.swoole学习笔记--异步tcp服务器
    04.swoole学习笔记--webSocket服务器
    bzoj 4516: [Sdoi2016]生成魔咒
    bzoj 3238: [Ahoi2013]差异
    bzoj 4566: [Haoi2016]找相同字符
    bzoj 4199: [Noi2015]品酒大会
    后缀数组之hihocoder 重复旋律1-4
    二分查找
    内置函数--sorted,filter,map
  • 原文地址:https://www.cnblogs.com/wjcx-sqh/p/12708253.html
Copyright © 2020-2023  润新知