mirror of
https://github.com/ethauvin/rife2.git
synced 2025-05-01 02:58:12 -07:00
Updated dependency injection examples
This commit is contained in:
parent
4446d1e811
commit
04cde04733
8 changed files with 53 additions and 13 deletions
|
@ -4,23 +4,32 @@
|
|||
*/
|
||||
package rife;
|
||||
|
||||
import rife.apis.MyService;
|
||||
import rife.apis.ServiceProvider;
|
||||
import rife.engine.*;
|
||||
import rife.services.HelloService;
|
||||
|
||||
public class HelloAll extends Site {
|
||||
public class HelloAll extends Site implements ServiceProvider {
|
||||
Router authentication;
|
||||
Router cmf;
|
||||
HelloDependencyInjection dependencyInjection;
|
||||
Router generation;
|
||||
Router continuations;
|
||||
Router gqm;
|
||||
Router group;
|
||||
Router resources;
|
||||
|
||||
public MyService getMyService() {
|
||||
return dependencyInjection.getMyService();
|
||||
}
|
||||
|
||||
public void setup() {
|
||||
group(authentication = new HelloAuthentication());
|
||||
group("/content", cmf = new HelloContentManagement());
|
||||
group(new HelloContinuations());
|
||||
group(new HelloCounterContinuations());
|
||||
group(new HelloDatabase());
|
||||
group(dependencyInjection = new HelloDependencyInjection(new HelloService()));
|
||||
group(new HelloErrors());
|
||||
group(new HelloForm());
|
||||
group("/generation", generation = new HelloFormGeneration());
|
||||
|
|
|
@ -4,16 +4,17 @@
|
|||
*/
|
||||
package rife;
|
||||
|
||||
import rife.apis.MyService;
|
||||
import rife.apis.ServiceProvider;
|
||||
import rife.engine.*;
|
||||
import rife.engine.annotations.ActiveSite;
|
||||
import rife.services.HelloService;
|
||||
|
||||
public class HelloDependencyInjection extends Site {
|
||||
public class HelloDependencyInjection extends Site implements ServiceProvider {
|
||||
public static class ServiceUser implements Element {
|
||||
final ServiceProvider service_;
|
||||
final MyService service_;
|
||||
|
||||
public ServiceUser(ServiceProvider service) {
|
||||
public ServiceUser(MyService service) {
|
||||
service_ = service;
|
||||
}
|
||||
|
||||
|
@ -22,17 +23,21 @@ public class HelloDependencyInjection extends Site {
|
|||
}
|
||||
}
|
||||
|
||||
public MyService getMyService() {
|
||||
return service_;
|
||||
}
|
||||
|
||||
public static class ServiceUser2 implements Element {
|
||||
@ActiveSite HelloDependencyInjection site_;
|
||||
@ActiveSite ServiceProvider provider_;
|
||||
|
||||
public void process(Context c) {
|
||||
c.print(site_.service_.serviceApi());
|
||||
c.print(provider_.getMyService().serviceApi());
|
||||
}
|
||||
}
|
||||
|
||||
final ServiceProvider service_;
|
||||
final MyService service_;
|
||||
|
||||
public HelloDependencyInjection(ServiceProvider service) {
|
||||
public HelloDependencyInjection(MyService service) {
|
||||
this.service_ = service;
|
||||
}
|
||||
|
||||
|
|
5
app/src/main/java/rife/apis/MyService.java
Normal file
5
app/src/main/java/rife/apis/MyService.java
Normal file
|
@ -0,0 +1,5 @@
|
|||
package rife.apis;
|
||||
|
||||
public interface MyService {
|
||||
String serviceApi();
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
package rife.apis;
|
||||
|
||||
public interface ServiceProvider {
|
||||
String serviceApi();
|
||||
MyService getMyService();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package rife.services;
|
||||
|
||||
import rife.apis.ServiceProvider;
|
||||
import rife.apis.MyService;
|
||||
|
||||
public class HelloService implements ServiceProvider {
|
||||
public class HelloService implements MyService {
|
||||
@Override
|
||||
public String serviceApi() {
|
||||
return "Hello World";
|
||||
|
|
|
@ -40,6 +40,12 @@
|
|||
<li>When you're done, delete the database through <a href="{{v route:/}}remove">{{v route:/}}remove</a></li>
|
||||
</ul>
|
||||
|
||||
<h2>HelloDependencyInjection</h2>
|
||||
<ul>
|
||||
<li>Open your browser at <a href="{{v route:/}}service1">{{v route:/}}service1</a></li>
|
||||
<li>Or <a href="{{v route:/}}service2">{{v route:/}}service2</a></li>
|
||||
</ul>
|
||||
|
||||
<h2>HelloErrors</h2>
|
||||
<ul>
|
||||
<li>Trigger and exception at <a href="{{v route:/}}error">{{v route:/}}error</a></li>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
package rife;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import rife.apis.ServiceProvider;
|
||||
import rife.apis.MyService;
|
||||
import rife.services.HelloService;
|
||||
import rife.test.MockConversation;
|
||||
|
||||
|
@ -71,7 +71,7 @@ class HelloTest {
|
|||
assertEquals("Hello World", m.doRequest("/service1").getText());
|
||||
assertEquals("Hello World", m.doRequest("/service2").getText());
|
||||
|
||||
m = new MockConversation(new HelloDependencyInjection(new ServiceProvider() {
|
||||
m = new MockConversation(new HelloDependencyInjection(new MyService() {
|
||||
public String serviceApi() {
|
||||
return "Test Service";
|
||||
}
|
||||
|
|
|
@ -54,6 +54,10 @@ tasks {
|
|||
project.logger.lifecycle(" Then add names by going to http://localhost:8080/add")
|
||||
project.logger.lifecycle(" When you're done, delete the database through http://localhost:8080/remove")
|
||||
project.logger.lifecycle("")
|
||||
project.logger.lifecycle(" HelloDependencyInjection:")
|
||||
project.logger.lifecycle(" Open your browser at http://localhost:8080/service1")
|
||||
project.logger.lifecycle(" Or http://localhost:8080/service2")
|
||||
project.logger.lifecycle("")
|
||||
project.logger.lifecycle(" HelloErrors:")
|
||||
project.logger.lifecycle(" Trigger and exception at http://localhost:8080/error")
|
||||
project.logger.lifecycle(" Try any other URL that doesn't exit, ie. http://localhost:8080/treasure")
|
||||
|
@ -179,6 +183,17 @@ tasks {
|
|||
project.logger.lifecycle("")
|
||||
}
|
||||
|
||||
register<JavaExec>("runHelloDependencyInjection") {
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
mainClass.set("rife.HelloDependencyInjection")
|
||||
project.logger.lifecycle("")
|
||||
project.logger.lifecycle("================================================================================")
|
||||
project.logger.lifecycle(" To try this example, open your browser at http://localhost:8080/service1")
|
||||
project.logger.lifecycle(" Or http://localhost:8080/service2")
|
||||
project.logger.lifecycle("================================================================================")
|
||||
project.logger.lifecycle("")
|
||||
}
|
||||
|
||||
register<JavaExec>("runHelloErrors") {
|
||||
classpath = sourceSets["main"].runtimeClasspath
|
||||
mainClass.set("rife.HelloErrors")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue