Skip to content

cscristianmoreno/simple-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple IoC + Web Annotations

This project include dependencie injection and controllers annotations that consume a servlet

How to work?

Requeriments

<dependencies>
    <dependency>
      <groupId>io.github.cscristianmoreno</groupId>
      <artifactId>web</artifactId>
      <version>1.0</version>
    </dependency>
</dependencies>
/** Annotation to launch tomcat server */
@Server
public class App {
    public static void main(String[] args) throws Exception {
        /** Initialized the IoC */
        IOCInitializer.main(args);
    }
}

How to work the initilization

The implementation ServletContainerInitializer is the one in charge onStartup method.

This interface:

@HandlesTypes(Controller.class)

load all classes that matches with @Controller annotation, next, i use a Java Reflection to access a class property.

Change package scan

For default package scan is com/../.. If you wish change to another package name, you need use

PackageScanUtil.setScan("org");
IOCInitializer.main(args);

Controller example

@Controller("/users")
public class UserController {
    @GET("/value/{id}/test/{name}")
    private String message(@Variable int id, @Variable String name) {
        return "Hi! " + name + " " + "#" + Integer.toString(id)
    }

    @POST("/save")
    public Users save(@Body Users users) {
        return users;
    }
}

IoC example

@Component
public class MyComponent {
    
    public String getMessage() {
        return "MyComponent has been returned this message";
    }
}
@Injectable
public class MyInjectable {
    
    @Inject
    private MyComponent myComponent;

    public String getMessage() {
        return myComponent.getMessage();
    }
}

Or else

@Component
public class SimpleComponent {

    private final MyComponent myInjectable;

    @Constructor
    public UserController(final MyInjectable myInjectable) {
        this.myInjectable = myInjectable;
    }

    ...
}

Servlet Annotations

  • @GET("/../{...}/.../{...}")
  • @POST("/../{...}/.../{...}")
  • @PUT("/../{...}/.../{...}")
  • @PATCH("/../{...}/.../{...}")
  • @DELETE("/../{...}/.../{...}")

Method Params Annotations

  • @Body
  • @Variable
  • @Param(..)
  • @Header(..)

IoC Annotations

@Injectable

Create a new class instance.

The classes with this annotation can use @Inject in field or constructor 

This annotation is required for use in  @Inject fields.

* Can inject dependencie

@Inject

Get a instance to @Injectable class.

This annotation required @Component or @Injectable in the same class to can use.

@Inject -> (@Component | @Injectable)

@Component

Create a new classe instance.

This annotation is required to inject dependencie

* Can inject dependencie

@Constructor (Optional)

A semanthic annotation to indicate that fields will injected through constructor

@Server (Required in main class)

This annotation run Apache Tomcat

About

A simple framework that include dependency injection and web controller annotations

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages