khs-sherpa-guice

Guice support for Sherpa.

License

License

Categories

Categories

GUI User Interface Guice Application Layer Libs Dependency Injection KeY Data Data Formats Formal Verification
GroupId

GroupId

com.keyholesoftware
ArtifactId

ArtifactId

khs-sherpa-guice
Last Version

Last Version

1.3.0
Release Date

Release Date

Type

Type

jar
Description

Description

khs-sherpa-guice
Guice support for Sherpa.
Project URL

Project URL

http://keyholesoftware.com
Project Organization

Project Organization

Keyhole Software
Source Code Management

Source Code Management

https://github.com/in-the-keyhole/khs-sherpa-guice

Download khs-sherpa-guice

How to add to project

<!-- https://jarcasting.com/artifacts/com.keyholesoftware/khs-sherpa-guice/ -->
<dependency>
    <groupId>com.keyholesoftware</groupId>
    <artifactId>khs-sherpa-guice</artifactId>
    <version>1.3.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.keyholesoftware/khs-sherpa-guice/
implementation 'com.keyholesoftware:khs-sherpa-guice:1.3.0'
// https://jarcasting.com/artifacts/com.keyholesoftware/khs-sherpa-guice/
implementation ("com.keyholesoftware:khs-sherpa-guice:1.3.0")
'com.keyholesoftware:khs-sherpa-guice:jar:1.3.0'
<dependency org="com.keyholesoftware" name="khs-sherpa-guice" rev="1.3.0">
  <artifact name="khs-sherpa-guice" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.keyholesoftware', module='khs-sherpa-guice', version='1.3.0')
)
libraryDependencies += "com.keyholesoftware" % "khs-sherpa-guice" % "1.3.0"
[com.keyholesoftware/khs-sherpa-guice "1.3.0"]

Dependencies

provided (4)

Group / Artifact Type Version
javax.servlet : javax.servlet-api jar 3.1.0
com.keyholesoftware : khs-sherpa jar 1.3.0
com.google.inject : guice jar 3.0
com.google.inject.extensions : guice-servlet jar 3.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.8.1

Project Modules

There are no modules declared in this project.

khs-sherpa-guice Build Status

Google Guice dependency injection integration for khsSherpa.

Link to khsSherpa project here...[https://github.com/in-the-keyhole/khs-sherpa]

Getting Started

To get this working, follow these steps:

Add

<listener>
    <listener-class>com.khs.guice.SherpaGuiceContextListener</listener-class>
</listener>

to your web.xml after Sherpa's listener but before your application's listener.

In your application's context listener, add SherpaGuiceModule to the end of the list of modules to be loaded by the injector. Then, use something like this to let the SherpaGuice's factory know about the injector. Also shown is how to have Guice handle your servlet mapping without having to put it in web.xml.

After these "glue" pieces are in place, you can just use @Inject in your Sherpa endpoints to inject services or whatever else you need.

public class MyApplicationContextListener extends GuiceServletContextListener implements ServletContextListener {
	private List<Module> modulesLoaded = new LinkedList<Module>();
	ServletContext servletContext;

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		servletContext = sce.getServletContext();
		super.contextInitialized(sce);
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		super.contextDestroyed(sce);
		for (Module module : modulesLoaded) {
			if (module instanceof DestroyableModule) {
				((DestroyableModule) module).destroy();
			}
		}

		servletContext = null;
	}

	@Override
	protected Injector getInjector() {
		// add your application modules here
	
		modulesLoaded.add(new ServletModule() {
			@Override
			protected void configureServlets() {
				serve("/api/*").with(SherpaGuiceServlet.class);
			}
		});
		modulesLoaded.add(new SherpaGuiceModule());

		ApplicationContext applicationContext = (ApplicationContext)servletContext.getAttribute(GenericApplicationContext.SHERPA_APPLICATION_CONTEXT_ATTRIBUTE);
		SherpaGuiceManagedBeanFactory factory = (SherpaGuiceManagedBeanFactory) applicationContext.getManagedBeanFactory();
		Injector injector = Guice.createInjector(modulesLoaded);
		factory.setInjector(injector);
		return injector;
	}
}
com.keyholesoftware

Keyhole Software

Software dev team providing consulting, development & education services with Java, JavaScript & .NET technologies.

Versions

Version
1.3.0