Spring学习知识点大全(三)

简介: 教程来源 https://app-aakcgtuh1ywx.appmiaoda.com 本文系统介绍Spring生态核心实践:涵盖单元测试(MockBean+断言)、集成测试(MockMvc+事务回滚)、SpEL表达式(属性注入、集合操作等)、Spring Boot自动配置原理及YAML配置要点,助力高效构建企业级应用。

七、测试支持

7.1 单元测试

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class UserServiceTest {

    @Autowired
    private UserService userService;

    @MockBean
    private UserDao userDao;

    @Test
    public void testCreateUser() {
        User user = new User("张三", 25);

        when(userDao.insert(any(User.class))).thenReturn(1);

        User result = userService.create(user);

        assertNotNull(result);
        assertEquals("张三", result.getUsername());
        verify(userDao, times(1)).insert(any(User.class));
    }

    @Test(expected = BusinessException.class)
    public void testCreateUserWithInvalidAge() {
        User user = new User("李四", -1);
        userService.create(user);
    }

    @Test
    public void testFindUser() {
        User expected = new User(1L, "王五", 30);
        when(userDao.findById(1L)).thenReturn(expected);

        User actual = userService.findById(1L);

        assertEquals(expected.getUsername(), actual.getUsername());
    }
}

7.2 集成测试

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Transactional
@Rollback
public class UserControllerIntegrationTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void testGetUser() throws Exception {
        mockMvc.perform(get("/api/users/1")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id").value(1))
                .andExpect(jsonPath("$.username").value("张三"))
                .andDo(print());
    }

    @Test
    public void testCreateUser() throws Exception {
        User user = new User("李四", 25);

        mockMvc.perform(post("/api/users")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(user)))
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.id").exists())
                .andExpect(jsonPath("$.username").value("李四"));
    }

    @Test
    public void testValidationError() throws Exception {
        User user = new User("", -1);

        mockMvc.perform(post("/api/users")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(user)))
                .andExpect(status().isBadRequest())
                .andExpect(jsonPath("$.code").value(400));
    }
}

八、Spring 表达式语言(SpEL)

@Component
public class SpELDemo {

    @Value("#{systemProperties['user.name']}")
    private String userName;

    @Value("#{systemProperties['java.version']}")
    private String javaVersion;

    @Value("#{T(Math).random() * 100.0}")
    private double randomNumber;

    @Value("#{userService.findUser(1)?.username ?: 'Unknown'}")
    private String defaultUsername;

    @Value("#{userService.users.?[age > 25]}")
    private List<User> adults;

    @Value("#{userService.users.![username]}")
    private List<String> usernames;

    public void testSpEL() {
        ExpressionParser parser = new SpelExpressionParser();

        // 字面量
        String hello = parser.parseExpression("'Hello World'").getValue(String.class);

        // 数学运算
        int sum = parser.parseExpression("1 + 2 + 3").getValue(Integer.class);

        // 逻辑运算
        boolean result = parser.parseExpression("2 > 1 && 3 < 5").getValue(Boolean.class);

        // 方法调用
        String upper = parser.parseExpression("'hello'.toUpperCase()").getValue(String.class);

        // 属性访问
        Expression exp = parser.parseExpression("user.username");
        User user = new User("张三", 25);
        String username = exp.getValue(user, String.class);

        // 安全导航
        String email = parser.parseExpression("user?.email ?: 'no-email'")
                           .getValue(user, String.class);

        // 集合选择
        List<User> users = Arrays.asList(
            new User("张三", 30),
            new User("李四", 20),
            new User("王五", 35)
        );

        List<User> adults = parser.parseExpression("#root.?[age >= 25]")
                                  .getValue(users, List.class);

        // 集合投影
        List<String> names = parser.parseExpression("#root.![username]")
                                   .getValue(users, List.class);
    }
}

// 在 XML 中使用 SpEL
<bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource">
    <property name="jdbcUrl" value="#{systemProperties['db.url']}"/>
    <property name="username" value="#{systemProperties['db.username']}"/>
    <property name="password" value="#{systemProperties['db.password']}"/>
</bean>

九、Spring 与 Spring Boot

9.1 Spring Boot 简介
Spring Boot 是 Spring 框架的扩展,通过自动配置和起步依赖简化了 Spring 应用的开发。

核心特性:

自动配置:根据类路径中的依赖自动配置 Spring

起步依赖:简化 Maven/Gradle 依赖管理

嵌入式服务器:内置 Tomcat、Jetty 等

生产就绪:提供健康检查、指标监控等

无代码生成和 XML 配置

9.2 Spring Boot 应用

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

// application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: 123456
    hikari:
      maximum-pool-size: 10
      connection-timeout: 30000

  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL8Dialect

  redis:
    host: localhost
    port: 6379
    timeout: 2000ms

  cache:
    type: redis

  servlet:
    multipart:
      max-file-size: 10MB
      max-request-size: 100MB

server:
  port: 8080
  servlet:
    context-path: /api
    session:
      timeout: 1800s

logging:
  level:
    com.example: DEBUG
    org.springframework.web: INFO
  file:
    name: logs/application.log

management:
  endpoints:
    web:
      exposure:
        include: health,info,metrics,prometheus
  endpoint:
    health:
      show-details: always

9.3 Spring Boot 自动配置

// 自定义自动配置
@Configuration
@ConditionalOnClass(RedisTemplate.class)
@ConditionalOnProperty(name = "cache.enabled", havingValue = "true")
@EnableConfigurationProperties(CacheProperties.class)
public class CustomCacheAutoConfiguration {

    @Autowired
    private CacheProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
            .entryTtl(Duration.ofMinutes(properties.getTtl()))
            .serializeValuesWith(
                RedisSerializationContext.SerializationPair.fromSerializer(
                    new GenericJackson2JsonRedisSerializer()
                )
            );

        return RedisCacheManager.builder(factory)
            .cacheDefaults(config)
            .build();
    }
}

// 配置属性类
@ConfigurationProperties(prefix = "cache")
public class CacheProperties {
    private long ttl = 60;
    private int maxSize = 1000;

    // getter/setter
}

Spring 的世界广阔而精彩,愿本文成为你 Spring 学习之路上的重要指南。持续学习,深入实践,你一定能成为 Spring 专家!
来源:
https://app-aakcgtuh1ywx.appmiaoda.com

相关文章
|
5天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
10731 63
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
5天前
|
人工智能 IDE API
2026年国内 Codex 安装教程和使用教程:GPT-5.4 完整指南
Codex已进化为AI编程智能体,不仅能补全代码,更能理解项目、自动重构、执行任务。本文详解国内安装、GPT-5.4接入、cc-switch中转配置及实战开发流程,助你从零掌握“描述需求→AI实现”的新一代工程范式。(239字)
3111 126
|
1天前
|
人工智能 自然语言处理 供应链
【最新】阿里云ClawHub Skill扫描:3万个AI Agent技能中的安全度量
阿里云扫描3万+AI Skill,发现AI检测引擎可识别80%+威胁,远高于传统引擎。
1199 1
|
11天前
|
人工智能 JavaScript API
解放双手!OpenClaw Agent Browser全攻略(阿里云+本地部署+免费API+网页自动化场景落地)
“让AI聊聊天、写代码不难,难的是让它自己打开网页、填表单、查数据”——2026年,无数OpenClaw用户被这个痛点困扰。参考文章直击核心:当AI只能“纸上谈兵”,无法实际操控浏览器,就永远成不了真正的“数字员工”。而Agent Browser技能的出现,彻底打破了这一壁垒——它给OpenClaw装上“上网的手和眼睛”,让AI能像真人一样打开网页、点击按钮、填写表单、提取数据,24小时不间断完成网页自动化任务。
2563 6
|
25天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
24388 122