[BAEL-1218] Guide to Spring EJB Integration (#3266)

* Evaluation article about types of bean injection in spring

* BAEL-1319 Quick Guide on Data.sql and Schema.sql Files in Spring

* Revert "Evaluation article about types of bean injection in spring"

This reverts commit eb071171673e0b8fa2b7ecffdad86f596e5fb114.

* BAEL-1218: adding spring-ejb and configuring ejb start and deploy

* BAEL-1218: adding spring

* BAEL-1218: wrapping all together

* BAEL-1218: new spring-ejb module

* BAEL-1218: tests and improvements

* BAEL-1218: removing moved files from old article

* BAEL-1218: code review requested changes

* BAEL-1218: test methods nomenclature correction

* BAEL-1418: removing tabs

* BAEL-1218: removing tabs

* BAEL-1218: correcting boot parent module path
This commit is contained in:
Allan Vital 2018-01-07 21:59:53 -02:00 committed by KevinGilmore
parent 09cbc1c6ee
commit cf32e14a9b
15 changed files with 463 additions and 0 deletions

View File

@ -161,6 +161,7 @@
<module>spring-cloud</module>
<module>spring-core</module>
<module>spring-cucumber</module>
<module>spring-ejb</module>
<module>spring-aop</module>
<module>persistence-modules/spring-data-cassandra</module>
<module>spring-data-couchbase-2</module>

View File

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>ejb-for-spring</artifactId>
<version>1.0.1</version>
</parent>
<artifactId>ejb-remote-for-spring</artifactId>
<packaging>ejb</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- mvn clean package cargo:run -Pwildfly-standalone-->
<profile>
<id>wildfly-standalone</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version>
<configuration>
<container>
<containerId>wildfly10x</containerId>
<zipUrlInstaller>
<url>http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip</url>
</zipUrlInstaller>
</container>
<configuration>
<properties>
<cargo.hostname>127.0.0.1</cargo.hostname>
<cargo.jboss.configuration>standalone-full</cargo.jboss.configuration>
<cargo.jboss.management-http.port>9990</cargo.jboss.management-http.port>
<cargo.servlet.users>testUser:admin1234!</cargo.servlet.users>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<javaee-api.version>7.0</javaee-api.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,11 @@
package com.baeldung.ejb.tutorial;
import javax.ejb.Remote;
@Remote
public interface HelloStatefulWorld {
int howManyTimes();
String getHelloWorld();
}

View File

@ -0,0 +1,19 @@
package com.baeldung.ejb.tutorial;
import javax.ejb.Stateful;
@Stateful(name = "HelloStatefulWorld")
public class HelloStatefulWorldBean implements HelloStatefulWorld {
private int howManyTimes = 0;
public int howManyTimes() {
return howManyTimes;
}
public String getHelloWorld() {
howManyTimes++;
return "Hello Stateful World!";
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.ejb.tutorial;
import javax.ejb.Remote;
@Remote
public interface HelloStatelessWorld {
String getHelloWorld();
}

View File

@ -0,0 +1,12 @@
package com.baeldung.ejb.tutorial;
import javax.ejb.Stateless;
@Stateless(name = "HelloStatelessWorld")
public class HelloStatelessWorldBean implements HelloStatelessWorld {
public String getHelloWorld() {
return "Hello Stateless World!";
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
version="3.2">
<module-name>ejb-remote-for-spring</module-name>
</ejb-jar>

View File

@ -0,0 +1,32 @@
package com.baeldung.ejb.tutorial;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
public class HelloStatefulWorldTestUnitTest {
private HelloStatefulWorldBean statefulBean;
@Before
public void setup() {
statefulBean = new HelloStatefulWorldBean();
}
@Test
public void whenGetHelloWorld_thenHelloStatefulWorldIsReturned() {
String helloWorld = statefulBean.getHelloWorld();
assertThat(helloWorld).isEqualTo("Hello Stateful World!");
}
@Test
public void whenGetHelloWorldIsCalledTwice_thenCounterIs2() {
statefulBean.getHelloWorld();
statefulBean.getHelloWorld();
assertThat(statefulBean.howManyTimes()).isEqualTo(2);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.ejb.tutorial;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
public class HelloStatelessWorldTestUnitTest {
private HelloStatelessWorldBean statelessBean;
@Before
public void setup() {
statelessBean = new HelloStatelessWorldBean();
}
@Test
public void whenGetHelloWorld_thenHelloStatelessWorldIsReturned() {
String helloWorld = statelessBean.getHelloWorld();
assertThat(helloWorld).isEqualTo("Hello Stateless World!");
}
}

77
spring-ejb/pom.xml Executable file
View File

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>ejb-for-spring</artifactId>
<version>1.0.1</version>
<packaging>pom</packaging>
<name>ejb</name>
<description>Spring EJB Tutorial</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<repositories>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Maven Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>ejb-remote-for-spring</artifactId>
<version>1.0.1</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>10.1.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.4</version>
<configuration>
<ejbVersion>3.2</ejbVersion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>ejb-remote-for-spring</module>
<module>spring-ejb-client</module>
</modules>
</project>

View File

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-ejb-client</artifactId>
<packaging>jar</packaging>
<name>spring-ejb-client</name>
<description>Spring EJB Client</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-5</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>10.1.0.Final</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>ejb-remote-for-spring</artifactId>
<version>1.0.1</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@ -0,0 +1,50 @@
package com.baeldung.springejbclient;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.baeldung.ejb.tutorial.HelloStatefulWorld;
import com.baeldung.ejb.tutorial.HelloStatelessWorld;
@SpringBootApplication
public class SpringEjbClientApplication {
@Bean
public Context context() throws NamingException {
Properties jndiProps = new Properties();
jndiProps.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
jndiProps.put("jboss.naming.client.ejb.context", true);
jndiProps.put("java.naming.provider.url", "http-remoting://localhost:8080");
return new InitialContext(jndiProps);
}
@Bean
public HelloStatelessWorld helloStatelessWorld(Context context) throws NamingException {
return (HelloStatelessWorld) context.lookup(this.getFullName(HelloStatelessWorld.class));
}
@Bean
public HelloStatefulWorld helloStatefulWorld(Context context) throws NamingException {
return (HelloStatefulWorld) context.lookup(this.getFullName(HelloStatefulWorld.class));
}
@SuppressWarnings("rawtypes")
private String getFullName(Class classType) {
String moduleName = "ejb-remote-for-spring/";
String beanName = classType.getSimpleName();
String viewClassName = classType.getName();
return moduleName + beanName + "!" + viewClassName;
}
public static void main(String[] args) {
SpringApplication.run(SpringEjbClientApplication.class, args);
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.springejbclient.endpoint;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.ejb.tutorial.HelloStatefulWorld;
import com.baeldung.ejb.tutorial.HelloStatelessWorld;
@RestController
public class HomeEndpoint {
private HelloStatelessWorld helloStatelessWorld;
private HelloStatefulWorld helloStatefulWorld;
public HomeEndpoint(HelloStatelessWorld helloStatelessWorld, HelloStatefulWorld helloStatefulWorld) {
this.helloStatelessWorld = helloStatelessWorld;
this.helloStatefulWorld = helloStatefulWorld;
}
@GetMapping("/stateless")
public String getStateless() {
return helloStatelessWorld.getHelloWorld();
}
@GetMapping("/stateful")
public String getStateful() {
return helloStatefulWorld.getHelloWorld() + " called " + helloStatefulWorld.howManyTimes() + " times";
}
}

View File

@ -0,0 +1,3 @@
server.port=8081
#logging.level.root=DEBUG

View File

@ -0,0 +1,11 @@
package com.baeldung.springejbclient;
import org.junit.Test;
public class SpringEjbClientApplicationIntegrationTest {
@Test
public void contextLoads() {
}
}