3

I am trying to put the data into GemFire by using Spring Data GemFire.

I followed this link

@Region("stockdata")
public class StockInfo {

    @Id 
    public String symbol;

    public String price;

    @PersistenceConstructor
    public StockInfo(String symbol, String price) {
        super();
        this.symbol = symbol;
        this.price = price;
    }

    @Override
    public String toString() {
        return "StockInfo [symbol=" + symbol + ", price=" + price + "]";
    }

    public String getSymbol() {
        return symbol;
    }
    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }

}

StockRepository class:

public interface StockRepository extends CrudRepository<StockInfo, String> {
    StockInfo findByName(String symbol);

}

Main class:

@Configuration
@EnableGemfireRepositories
public class Application implements CommandLineRunner {

    @Bean
    CacheFactoryBean cacheFactoryBean() {
        return new CacheFactoryBean();

    }

    @Bean
    LocalRegionFactoryBean<String, StockInfo> localRegionFactory(final GemFireCache cache) {
        return new LocalRegionFactoryBean<String, StockInfo>() {

            {
                setCache(cache);
                setName("stockdata");
                setClose(false);
            }
        };
    }
    @Autowired
    StockRepository stockRepositry;
    public void run(String... arg0) throws Exception {
        StockInfo fbStock = new StockInfo("FB", "100");
        StockInfo aaplStock = new StockInfo("AAPL", "200");
        StockInfo msftStock = new StockInfo("MSFT", "300");

        System.out.println("Before linking up with Gemfire...");
        for (StockInfo stockInfo : new StockInfo[] {fbStock, aaplStock,msftStock }) {
            System.out.println("\t" + stockInfo);
        }

        stockRepositry.save(fbStock);
        stockRepositry.save(aaplStock);
        stockRepositry.save(msftStock);

        System.out.println("Lookup each Stock by name...");

        for (String symbol : new String[] { fbStock.symbol, aaplStock.symbol,msftStock.symbol }) {
            System.out.println("\t" + stockRepositry.findByName(symbol));
        }

    }

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

Pom.xml:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>
    <properties>
        <java.version>1.7</java.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-gemfire</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency> 
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories> 

The error is below:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[spring-context-4.2.3.RELEASE.jar:4.2.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at com.emc.geode.entity.Application.main(Application.java:62) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    ... 8 common frames omitted
John Blum
  • 7,381
  • 1
  • 20
  • 30
Amaresh
  • 3,231
  • 7
  • 37
  • 60
  • Possible duplicate of [Spring Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean](http://stackoverflow.com/questions/21783391/spring-boot-unable-to-start-embeddedwebapplicationcontext-due-to-missing-embedd) – jny Dec 01 '15 at 14:23
  • 1
    Most likely you are missing `spring-boot-maven-plugin` in your pom – jny Dec 01 '15 at 14:24
  • @jny .it is there.. org.springframework.boot spring-boot-maven-plugin – Amaresh Dec 01 '15 at 14:26
  • How do you build and run it? – jny Dec 01 '15 at 14:28
  • java -jar GeodeRest-0.0.1-SNAPSHOT.jar – Amaresh Dec 01 '15 at 14:29
  • How do you build the jar file? With maven? – jny Dec 01 '15 at 14:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96694/discussion-between-aman-and-jny). – Amaresh Dec 01 '15 at 14:32

2 Answers2

2

You need to add @SpringBootApplication to your main class.

@EnableGemfireRepositories
@SpringBootApplication
public class Application implements CommandLineRunner {

and in your pom add spring-boot-starter-web dependency instead of spring-web

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
reos
  • 8,766
  • 6
  • 28
  • 34
1

To simplify your Pivotal GemFire configuration in your Spring Boot application, you might consider using (starting with the) Spring Boot for Pivotal GemFire (SBDG) project.

SBDG builds on Spring Data for Pivotal GemFire (SDG) along with other Spring projects, obviously Spring Boot, but also Spring Session for Pivotal GemFire (SSDG; here), as well. It applies all the concepts of Spring Boot (e.g. opinionated, "convention over configuration" using auto-configuration, etc) when developing Pivotal GemFire applications with Spring in general, and Spring Boot in particular.

For instance, in your application, SBDG would automatically auto-configure SD[G] Repositories, making the explicit declaration of @EnableGemfireRepositories unnecessary.

There are many other benefits to using SBDG, too.

Food for thought.

John Blum
  • 7,381
  • 1
  • 20
  • 30
  • Actually, I noticed you are using Spring Boot 1.x, which at the time, had the `spring-boot-starter-data-gemfire` module. This starter module no longer exists as of *Spring Boot* `2.0` and was replaced by the SBDG project I referenced in my answer above. – John Blum Apr 16 '19 at 19:05