简体   繁体   English

Spring Data JPA:org.springframework.beans.factory.UnsatisfiedDependencyException:

[英]Spring Data JPA: org.springframework.beans.factory.UnsatisfiedDependencyException:

I am trying to add crossOrigin variable in Application.property file. 我正在尝试在Application.property文件中添加crossOrigin变量。 and accessing that variable through controller using @value its working for following two end point's but when I am adding @value to third end point I am getting following error message 并通过控制器使用@value访问该变量,该变量可用于以下两个端点,但是当我将@value添加到第三个端点时,出现以下错误消息

AccountController.java AccountController.java

@RestController
@RequestMapping("/api.spacestudy.com/SpaceStudy/Admin/Account")
public class AccountController {

    @Autowired
    AccountService accService;

    @Value("${crossOrigin}")
    @GetMapping("/loadAcctLocationList")
    public ResponseEntity<List<Account>> findLocation() {
        return  ResponseEntity.ok(accService.findLocation());
    }

}

TestAccountController TestAccountController

@RunWith(SpringRunner.class)
public class TestAccountController {

    private MockMvc mockMvc;

    @Mock
    private AccountService accountService;

    @InjectMocks
    private AccountController accountController;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(accountController).build();
    }

    @Test
    public void findLocationTest() throws Exception {

        Account account = new Account();
        account.setsLocation("Test1");

        List<Account> accountObj = new ArrayList<Account>();
        accountObj.add(account);    

        Mockito.when(accountService.findLocation()).thenReturn(accountObj);

        mockMvc.perform(get("/spacestudy/$ InstituteIdentifier/admin/account/loadAcctLocationList"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$[0].sLocation", is("Test1")))
                .andExpect(jsonPath("$.*",Matchers.hasSize(1)));    

        for(Account result: accountObj) {

            assertEquals("Test1", result.sLocation);

        }

        }

application.properties application.properties

crossOrigin =@CrossOrigin(origins ="http://localhost:4200")
server.port=8086
spring.datasource.url=jdbc:postgresql://localhost/SpaceStudyDB
spring.datasource.username=postgres
spring.datasource.password=postgres

Console 安慰

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-05-15 01:59:17.915 ERROR 5240 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountController': Unsatisfied dependency expressed through method 'btnSaveClick' parameter 0; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave': no matching editors or conversion strategy found
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:667) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at com.spacestudy.SpaceStudyAdminAccountMaintenance.main(SpaceStudyAdminAccountMaintenance.java:10) [classes/:na]
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1093) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 19 common frames omitted
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'com.spacestudy.model.AccountMaintenanceSave': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:307) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:109) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 23 common frames omitted

Can any one tell me what I am doing wrong in above code ? 有人可以告诉我我在上面的代码中做错了什么吗? or tell me any another way to declare variable in application.property file 或告诉我在application.property文件中声明变量的任何其他方法

It looks like what Spring is trying to do here is inject the value of ${crossOrigin} into your method. 看起来Spring想要在这里做的就是将${crossOrigin}的值注入到您的方法中。

  1. In findLocation , it's not breaking because there are no method arguments in which to inject the value findLocation ,它没有中断,因为没有方法参数可在其中注入值
  2. In the btnSearchClick , there's more than one argument and it's not attempting to inject the value btnSearchClick ,有多个参数,并且它没有尝试注入值
  3. There's one argument, and it's trying to inject the value of ${crossOrigin} into AccountMaintenanceSave saveObj 有一个参数,它正在尝试将${crossOrigin}的值注入AccountMaintenanceSave saveObj

You should just inject ${crossOrigin} into a @CrossOrigin at the top of your class? 您应该只在班级顶部将${crossOrigin}注入@CrossOrigin吗? Like so: 像这样:

@RestController
@CrossOrigin(origins = "${crossOrigin}")
@RequestMapping("/api.spacestudy.com/SpaceStudy/Admin/Account")
public class AccountController {

    @Autowired
    AccountService accService;

    ...
}

And access the variable that way. 并以这种方式访问​​变量。 You'll need to remove the @Value annotations from your controller methods. 您需要从控制器方法中删除@Value批注。

You'll also need to just specify the origin name in your application.properties : 您还需要在application.properties指定原始名称:

crossOrigin=http://localhost:4200

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JPA异常org.springframework.beans.factory.UnsatisfiedDependencyException - JPA Exception org.springframework.beans.factory.UnsatisfiedDependencyException 构建项目spring org.springframework.beans.factory.UnsatisfiedDependencyException时出错 - Error building project spring org.springframework.beans.factory.UnsatisfiedDependencyException 单元测试存储库spring org.springframework.beans.factory.UnsatisfiedDependencyException - Unit testing repository spring org.springframework.beans.factory.UnsatisfiedDependencyException Spring Boot:org.springframework.beans.factory.UnsatisfiedDependencyException - Spring Boot:org.springframework.beans.factory.UnsatisfiedDependencyException Spring Boot错误org.springframework.beans.factory.UnsatisfiedDependencyException - Spring Boot Error org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.UnsatisfiedDependencyException-是错误 - org.springframework.beans.factory.UnsatisfiedDependencyException - is the Error 异常org.springframework.beans.factory.UnsatisfiedDependencyException - Exception org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.UnsatisfiedDependencyException 1 - org.springframework.beans.factory.UnsatisfiedDependencyException 1 SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException - SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.UnsatisfiedDependencyException Springboot - org.springframework.beans.factory.UnsatisfiedDependencyException Springboot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM