2017-08-08 5 views
0

私は@Serviceを作成し、依存関係注入機能を備えたSelenium WebDriverを提供します。その後、Mainクラスで@Autowieredを指定したNullPointerException

import java.io.File;  
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.chrome.ChromeDriver; 
    import org.springframework.stereotype.Service;  

    @Service 
    public class WebDriverService { 

     public WebDriver webDriverGetter(){ 
      File file = new File(SeleniumApplication.class.getClassLoader().getResource("driver/chromedriver.exe").getFile()); 
      String driverPath=file.getAbsolutePath(); 
      System.out.println("Webdriver is in path: "+driverPath); 
      System.setProperty("webdriver.chrome.driver",driverPath); 
      WebDriver driver= new ChromeDriver(); 
      return driver; 
     } 

} 

と以下のようにメインクラスでサービスを呼び出します:ここではコードです

public class SeleniumApplication { 

    @Autowired 
    static WebDriverService driver; 

    public static void main(String[] args) { 

     driver.webDriverGetter().get("https://www.google.com/");   
    } 
} 

をしかし、それはして文句を言う:私はC hromeDriver.exe内に設定している

Exception in thread "main" java.lang.NullPointerException 

経路

src\main\resources\driver\chromedriver.exe 

pom.xmlに私は春を使用するには

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</artifactId> 
</dependency> 
+0

そして、どの春の文脈を全くロードせずにメインの方法でオートワイヤリングすると魔法を期待していますか?あなたは現在、主な方法を持っています。このメインメソッドは、変数 'driver'にアクセスします。これは決して設定しないので、nullです。あなたは '@ Autowired'で何か何かをすることは絶対にありません。あなたが実際に春を始めるわけではないからです。あなたが使用しているSpringに応じて、Springを最初に初期化する必要があります。 –

+0

あなたのコメントからヒントを得ましたが、回答を書くことはできますか? – Salman

+0

あなたがスプリングブートを使用しているかどうかを教えてくれるわけではないので、使用しているスプリングに応じて「スタートスプリング」以外に何かを答えるのは難しいです。 –

答えて

0

を持っている、あなたは、その春ブーツの文脈では

は、この手段など、あなたの豆を作成し、春のコンテキストを、初期化を意味し、何とか春を開始する必要がありますあなたはMyApplication@SpringBootApplication(多くの場合、それはmainメソッドが含まれている同じクラスだ)と注釈を付けたクラスであることを... ...

public static void main(String[] args) { 
    SpringApplication.run(MyApplication.class, args); 
} 

をこのようなあなたの主な方法があります。これは、春の起動コンテキストを開始します。

メインメソッドが静的であるため、isは非静的メンバーへのアクセスがなく、afaikスプリングは静的変数を結び付けません。たとえば、@PostConstructメソッドのように、Bean内部で別の方法でアクセスする必要があります。あなたが実際にあなたがあなたのメインクラスが実際にこれを持っている必要がありますところでmainメソッドの中でそれを行うカントautowired豆 を使用するためにcommandLineRunnerを実装する必要が

ConfigurableApplicationContext context = SpringApplication.run(MyApplication.class, args); 
    WebDriverService service = context.getBean(WebDriverService.class); 
+0

私はあなたの答えから多くを学んだ。ありがとう。 – Salman

+1

はじめに、https://start.spring.io/でSpring Initializrを使用することをお勧めします。これにより、基本的なプロジェクトを作成できます。開始するのが少し楽になります。 –

0
@SpringBootApplication  
public class SeleniumApplication implements CommandLineRunner { 

     @Autowired 
     WebDriverService driver; 

     public static void main(String[] args) { 
      SpringApplication.run(SeleniumApplication .class, args); 
     } 

     @Override 
     public void run(String... strings) throws Exception { 
      driver.webDriverGetter().get("https://www.google.com/");  
     } 

    } 

...上記の方法からのApplicationContextを取得します@SpringBootApplication annotion。

関連する問題