您的位置:首页 > 健康 > 美食 > Java开发注意事项

Java开发注意事项

2025/2/27 0:07:03 来源:https://blog.csdn.net/weixin_47199727/article/details/139511144  浏览:    关键词:Java开发注意事项

注意:测试类中使用@Autowired注解注入Bean,不要使用@RequiredArgsConstructor注解注入Bean

正确示范:

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.annotation.Autowired;@SpringBootTest
class ElasticSearchApplicationTests {@Autowired    //注意点private ItemService itemService;}

错误示范:

import lombok.RequiredArgsConstructor;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
@RequiredArgsConstructor    //注意点
class ElasticSearchApplicationTests {private final ItemService itemService;}

注意:如果启动多种环境配置的服务,在测试类中测试接口要指明其请求的服务配置文件的后半段名称,如application-dev.yml只需写dev。

正确示范:

import org.springframework.beans.factory.annotation.Autowired;
import com.example.elasticsearch.domain.po.Item;@SpringBootTest(properties = "spring.profiles.active=dev") //注意点
class ElasticSearchApplicationTests {@Autowiredprivate ItemService itemService;@Testvoid testAddDocument() throws IOException {// 根据id查询商品数据Item item = itemService.getById(317578L);System.out.println(item);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com