草庐IT

java - 实现 Spring Controller 的单元测试时缺少依赖项 hasSize() 和 hasProperty()

我正尝试在SpringMVCController中实现一个方法的单元测试,如下所示:@TestpublicvoidtestGetProfile(){PersonmockPerson=newPerson();mockPerson.setPersonId(1);mockPerson.setName("MrBrown");mockPerson.setAddress("Somewhere");mockPerson.setTelephone("1234567890");mockPerson.setEmail("brown@brown.com");when(mockPersonService.ge

java - Autowiring 来自 Spring @Configuration 类的字符串?

我希望集中访问我的所有属性值,这样我就可以做一些事情,比如确保所有属性都使用相同的名称、相同的默认值等。我创建了一个类来集中所有这个,但是我不确定需要访问这些值的类应该如何获取它们,因为您不能Autowiring字符串。我的课是这样的:@ConfigurationpublicclassSpringConfig{@Autowired@Value("${identifier:asdf1234}")publicStringidentifier;}我可能在多个类中使用它的地方publicclassFoo{@AutowiredprivateStringtheIdentifier;}publicc

java - 如何在 Spring 中正确关闭 ApplicationContext?

我正在学习SpringCore认证,我对在提供的学习Material中找到的这个问题有一些疑问:Whatisthepreferredwaytocloseanapplicationcontext?我知道如果我有这样的东西:ConfigurableApplicationContextcontext=…//Destroytheapplicationcontext.close();通过在context对象上使用close()方法,ApplicationContext被关闭并且应用程序被销毁。但我认为这不是我必须做的最好方法。阅读官方文档我发现我也可以这样做:context.registerSh

java - Spring MVC 测试结果出现 415 错误

我正在尝试为使用SpringMVC实现的RESTAPI编写集成测试。这是我的REST实现:importorg.myproject.api.input.ProjectInput;importorg.myproject.dao.ProjectsDao;importorg.myproject.model.Project;importorg.myproject.model.Projects;importorg.myproject.util.Exceptions;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.spri

java - 在 Spring Controller 中获取 Locale 的优雅方式

这个问题在这里已经有了答案:FindingLocalefromControllerinSpringMVC(2个答案)关闭4年前。我正在寻找一种比在每个Controller方法开始时显式调用LocaleContextHolder.getLocale()更简洁的方法(在Spring3.2中)来获取当前语言环境。它必须与Java注释兼容,因为我没有使用XML配置。这是我目前正在做的事情。@ControllerpublicclassWifeController{@AutowiredprivateMessageSourcemsgSrc;@RequestMapping(value="/wife/m

java - 对现有 webapp 进行 Spring Boot 健康检查

我有现有的SpringMVCWeb应用程序。现在我只想使用spring-boot-starter-actuator中的健康检查功能。我是springboot的新手,所以不确定是否需要将我的完整项目转换为springboot项目才能进行健康检查。我可以只包含依赖项并以某种方式仅启用所需的功能吗? 最佳答案 我自己想通了。我加入了spring-boot-actuator而不是spring-boot-starter-actuator。而且我不需要使用@SpringBootApplication来初始化应用程序。相反,现在我只导入所需的自动

java - 没有 Spring-boot 的 Eureka 服务发现

我写了一个springboot微服务和一个REST客户端。客户端是另一个模块的一部分,对微服务进行RESTful调用。微服务在Eureka注册表中注册,我希望我的客户端(不是springboot项目)使用Eureka来查询和获取服务端点。我的问题是因为客户端不是Spring-Boot应用程序,所以我不能使用像@SpringBootApplication这样的注释,@EnableDiscoveryClient和DiscoveryClient不会自动连接到应用程序。无论如何手动自动连接DiscoveryClientbean到客户端而不使用注释? 最佳答案

java - 传递的分离实体在 Spring-Data 中持久存在

我的数据库Brand和Product中有两个表,具有下一个简单结构:|品牌|身份证PK||产品|身份证PK|brand_idFK|和该表的实体:@Entity@Table(name="Brand")publicclassBrand{@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateLongid;@Column(name="brand")privateStringbrand;/*gettersandsetters*/}@Entity@Table(name="Product")publicclassProduct{@Id@

java - 在 Spring Boot 中获取对当前 Activity 数据源的引用

我想通过DataSourceInitializer实现数据库数据初始化。我在我的SpringBootmain方法下面有这些方法,但它似乎根本没有执行(我尝试故意删除字符只是为了触发一个错误来确认执行。什么也没发生。):@ConfigurationProperties(prefix="spring.datasource")@BeanpublicDataSourcegetDataSource(){//iwashopingthiswasgoingtopullmycurrentdatasource,as//definedinapplication.propertiesreturnDataSou

java - 类新实例上的 Spring @Autowired

我对Spring不是很熟悉,遇到以下情况:存储库类:@RepositorypublicclassMyRepository{//...}使用存储库类的类:publicclassMyClassextendsAbstractClass{@AutowiredprivateMyRepositorymyRepository;//...}我知道如果我注释我的MyClass与@Component并将其与@Autowired一起使用,然后是@AutowiredMyRepository解决就好了。问题是我需要创建MyClass的新实例与反射。所以MyRepository永远不会解决,并且始终为null。有