2017-06-28 7 views
0

の保守のクラスからオブジェクトを取得するために私持ってEC2オブジェクトが含まれている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; 
    } 
} 

@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); 
    } 

} 

は私が

0123、などのエラーを取得しています
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 

ここで私は様々なサービスクラスでec2オブジェクトを使用していますので、毎回これをすべてのサービスクラスで初期化したくありません。代わりに別のクラスにec2を作成し、それを他のすべてのサービスクラスで使用する必要があります。しかし、私はこれを試して上記のエラーが発生しています。誰かがこの問題の修正に私を助けます。

+0

SecurityGroupServiceImpl.javaの行35とは何ですか?ここでNullPointerExceptionが発生し、完全なSpring初期化が失敗します。 –

答えて

2

をフィールドから@Autowireを削除し、コンストラクタに移動して(ここではコンストラクタセクションhttps://www.tutorialspoint.com/spring/spring_autowired_annotation.htmで読む@Autowired)コンストラクタから最後のフィールドを初期化します第三者からの豆)、実際のSpring BeanとしてAmazonEC2があります。

@Configuration 
public class AmazonServiceConfiguration { 
    @Bean 
    public AmazonEC2 ec2() { 
    return AmazonEC2ClientBuilder.standard() 
     .withRegion(Regions.AP_SOUTH_1).build(); 
    } 
} 

そして、あなたのサービスの実装:ここ

@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; 
    } 
} 
0

SecurityGroupServiceImplが作成されるとき、amazonServiceはありません。後でautowiredされるので、何かを取得しようとするとNPEが生成されます。

代わりに、フィールドがfinalではありません。もちろん、@PostConstruct

@PostConstruct 
public void init() throws Exception { 
    ec2 = amazonService.getEc2(); 
} 

でINITを移動します。

代わりにインスタンス化するための完璧な(のJava設定を利用しない理由を、さらに良い

1

あなたが実際にAmazonServiceクラスによって時に作成されていないSecurityGroupServiceImplクラスからec2を設定してみてください。したがって、ec2を取得しようとするクラス