GithubHelp home page GithubHelp logo

danjee / hedgehog Goto Github PK

View Code? Open in Web Editor NEW
9.0 2.0 4.0 705 KB

Sting your regular non container-managed java classes with Spring, Guice and Weld CDI beans

License: Apache License 2.0

Java 100.00%

hedgehog's Introduction

Hedgehog

Gitter Hex.pm Stories in Ready Travis CI Build Status Coverage Status Maven Central

Hedgehog is a Spring, Guice and Weld CDI bean injector in non-managed business java classes.

Inspired from Wicket injectors

Use it if you want to inject some managed components in legacy classes or classes that you simple do not want to have it managed like Swing or JavaFX panels.

Usage sample:

Spring

Use the maven dependency:

<dependency>
	<groupId>ro.fortsoft</groupId>
	<artifactId>hedgehog-spring</artifactId>
	<version>0.0.3</version>
</dependency>

The configuration class, with a sample bean that gets initialized here:

@Configuration
@ComponentScan(basePackages = "ro.fortsoft.beans")
public class AppConfig {

	@Bean
	public Child getChild(){
		return new Child();
	}

}

The bean definition:

@Component
public class Child {
	@Override
	public String toString() {
		return "I'm a child";
	}
}

A business panel (non managed bean), this can be any of your legacy classes that you do not want to add them to Spring management:

public class Panel {

	@Sting
	private Child child;
	
	public Panel(){
		Stinger.get().sting(this);
	}
	
	public void test() {
		System.out.println(child);
	}
}

Main class could look like this:

public class App implements StingAwareApplication {

	private MetaDataEntry<Stinger>[] metaData;

	public static void main(String[] args) {
		AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
		SpringComponentStinger stinger = new SpringComponentStinger(context);
		App app = new App();
		stinger.bind(app);
		Panel panel = new Panel();
		panel.test();
	}

	public Stinger getMetaData(MetaDataKey<Stinger> key) {
        return key.get(metaData);
    }

    public void setMetaData(final MetaDataKey<Stinger> key, final Stinger object) {
        metaData = key.set(metaData, object);
    }
}

Guice

Use the maven dependency:

<dependency>
	<groupId>ro.fortsoft</groupId>
	<artifactId>hedgehog-guice</artifactId>
	<version>0.0.3</version>
</dependency>

The Guice module class:

public class GuiceModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(ContactService.class).to(InMemoryContactService.class).asEagerSingleton();
	}
}

The beans (interface and implementation):

public interface ContactService {
	
	String add(String word);
	
	void modify(String word);
	
	void delete(String word);
}
public class InMemoryContactService implements ContactService {

	public String add(String word) {
		// TODO Auto-generated method stub
		return null;
	}

	public void modify(String word) {
		// TODO Auto-generated method stub
		
	}

	public void delete(String word) {
		// TODO Auto-generated method stub
		
	}
}

A business panel (non managed bean). This could be any of your java classes that you do not want Guice to be aware of them

public class Panel {

	@Sting
	private ContactService contactService;
	
	public Panel(){
		Stinger.get().sting(this);
	}
	
	public void test(){
		System.out.println(contactService);
	}
}

Main class

public class App implements StingAwareApplication {
	
	private MetaDataEntry<Stinger>[] metaData;
	
	public static void main(String[] args) {
		 Injector injector = Guice.createInjector(new GuiceModule());
		 GuiceComponentsStinger stinger = new GuiceComponentsStinger(injector);
		 App app = new App();
		 stinger.bind(app);
		 Panel panel = new Panel();
		 panel.test();
	}

	public Stinger getMetaData(MetaDataKey<Stinger> key) {
        return key.get(metaData);
    }

    public void setMetaData(final MetaDataKey<Stinger> key, final Stinger object) {
        metaData = key.set(metaData, object);
    }
}

Weld

Use the maven dependency:

<dependency>
	<groupId>ro.fortsoft</groupId>
	<artifactId>hedgehog-weld</artifactId>
	<version>0.0.3</version>
</dependency>

The service class to be injected

public class Child {
	@Override
	public String toString() {
		return "I'm a child";
	}
}

A business panel (non managed bean)

public class Panel {

	@Sting
	private Child child;
	
	public Panel(){
		Stinger.get().sting(this);
	}
	
	public void test() {
		System.out.println(child);
	}
}

Main class, this should print the I'm a child because the injection succeeded:

public class Main implements StingAwareApplication {

	private MetaDataEntry<Stinger>[] metaData;

	public static void main(String[] args) {
		Weld weld = new Weld();
		WeldContainer container = weld.initialize();

		WeldComponentStinger stinger = new WeldComponentStinger(container);
		Main mainApp = new Main();
		stinger.bind(mainApp);
		Panel panel = new Panel();
		panel.test();
	}

	public Stinger getMetaData(MetaDataKey<Stinger> key) {
        return key.get(metaData);
    }

    public void setMetaData(final MetaDataKey<Stinger> key, final Stinger object) {
        metaData = key.set(metaData, object);
    }
}

Versioning

Hedgehog will be maintained under the Semantic Versioning guidelines as much as possible.

Releases will be numbered with the follow format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

  • Breaking backward compatibility bumps the major
  • New additions without breaking backward compatibility bumps the minor
  • Bug fixes and misc changes bump the patch

For more information on SemVer, please visit http://semver.org.

Issues

Any issue or improvement idea is welcome, also pull-requests are happily accepted.

hedgehog's People

Contributors

danjee avatar waffle-iron avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

hedgehog's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.