Java 如何从类到服务获取对象

Java 如何从类到服务获取对象,java,spring,spring-mvc,spring-boot,aws-sdk,Java,Spring,Spring Mvc,Spring Boot,Aws Sdk,这里我有一个AmazonService类,它包含ec2对象 import org.springframework.stereotype.Component; import com.amazonaws.regions.Regions; import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; @Component public class Amaz

这里我有一个AmazonService类,它包含ec2对象

import org.springframework.stereotype.Component;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder;

@Component
public class AmazonService {

    private AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(Regions.AP_SOUTH_1).build();

    public AmazonEC2 getEc2() {
        return ec2;
    }   
}
我需要从一个服务类获得这个ec2,如下所示

@Service
public class SecurityGroupServiceImpl implements SecurityGroupService {



    @Autowired
    AmazonService amazonService;

    final AmazonEC2 ec2 = amazonService.getEc2();

    @Override
    public DeleteSecurityGroupResult deleteSecurityGroup(SecurityGroupDTO securityGroupDTO) {

        DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest()
                .withGroupId(securityGroupDTO.getGroupID());

        DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);
        System.out.println(response);
        return response;
    }
}
@Controller
public class SecurityGroupController {

    @Autowired
    SecurityGroupService service;

    @RequestMapping(value = "/deleteSecurityGroup", produces = "application/json", 
            consumes = "application/json", method = RequestMethod.POST)
    private @ResponseBody DeleteSecurityGroupResult deleteSecurityGroup(@RequestBody SecurityGroupDTO securityGroupDTO){

        return service.deleteSecurityGroup(securityGroupDTO);
    }

}
我的控制器类如下所示

@Service
public class SecurityGroupServiceImpl implements SecurityGroupService {



    @Autowired
    AmazonService amazonService;

    final AmazonEC2 ec2 = amazonService.getEc2();

    @Override
    public DeleteSecurityGroupResult deleteSecurityGroup(SecurityGroupDTO securityGroupDTO) {

        DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest()
                .withGroupId(securityGroupDTO.getGroupID());

        DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);
        System.out.println(response);
        return response;
    }
}
@Controller
public class SecurityGroupController {

    @Autowired
    SecurityGroupService service;

    @RequestMapping(value = "/deleteSecurityGroup", produces = "application/json", 
            consumes = "application/json", method = RequestMethod.POST)
    private @ResponseBody DeleteSecurityGroupResult deleteSecurityGroup(@RequestBody SecurityGroupDTO securityGroupDTO){

        return service.deleteSecurityGroup(securityGroupDTO);
    }

}
当我试着运行这个时,我得到的错误是

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-28 18:46:21.213 ERROR 7792 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityGroupController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityGroupServiceImpl' defined in file [E:\Project\AWS-SDK-Implimentation\target\classes\com\altimetrik\services\impl\SecurityGroupServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.altimetrik.services.impl.SecurityGroupServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]
    at com.altimetrik.AwsSdkImplimentationApplication.main(AwsSdkImplimentationApplication.java:10) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityGroupServiceImpl' defined in file [E:\Project\AWS-SDK-Implimentation\target\classes\com\altimetrik\services\impl\SecurityGroupServiceImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.altimetrik.services.impl.SecurityGroupServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 19 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.altimetrik.services.impl.SecurityGroupServiceImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 30 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.altimetrik.services.impl.SecurityGroupServiceImpl.<init>(SecurityGroupServiceImpl.java:35) ~[classes/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_121]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_121]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_121]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_121]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE]
    ... 32 common frames omitted
启动ApplicationContext时出错。要显示自动配置报告,请在启用“调试”的情况下重新运行应用程序。 2017-06-28 18:46:21.213错误7792---[main]o.s.boot.SpringApplication:应用程序启动失败 org.springframework.beans.factory.UnsatifiedDependencyException:创建名为“securityGroupController”的bean时出错:通过字段“服务”表示未满足的依赖关系;嵌套异常为org.springframework.beans.factory.BeanCreationException:创建名为“securityGroupServiceImpl”的bean时出错[E:\Project\AWS SDK Implimentation\target\classes\com\altimetrik\services\impl\securityGroupServiceImpl.class]:bean实例化失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[com.altimetrik.services.impl.SecurityGroupServiceImpl]:构造函数引发异常;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor$AutoWiredFeldElement.inject(AutoWiredNotationBeanPostProcessor.java:588)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.annotation.AutowiredNotationBeanPostProcessor.postProcessPropertyValues(AutowiredNotationBeanPostProcessor.java:366)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:761)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 位于org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:303)[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] 在com.altimetrik.awssdkimplicationapplication.main(awssdkimplicationapplication.java:10)[classes/:na] 原因:org.springframework.beans.factory.BeanCreationException:创建名为“securityGroupServiceImpl”的bean时出错[E:\Project\AWS SDK Implimentation\target\classes\com\altimetrik\services\impl\securityGroupServiceImpl.class]:bean实例化失败;嵌套异常为org.springframework.beans.beaninstantationException:未能实例化[com.altimetrik.services.impl.securitygroupservicepl]:构造函数抛出异常;嵌套异常是java.lang.NullPointerException 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1155)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)~[spring-beans-4.3.9.RELEASE.jar:4.3.9.RELEASE] 位于org.springframework.beans.factory.support.De
@Service
public class SecurityGroupServiceImpl implements SecurityGroupService {    
    @Autowired    
    AmazonEC2 ec2;

    @Override
    public DeleteSecurityGroupResult deleteSecurityGroup(SecurityGroupDTO securityGroupDTO) {

        DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest()
                .withGroupId(securityGroupDTO.getGroupID());

        DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);
        System.out.println(response);
        return response;
    }
}