The Bring is the Inversion of Control (IoC) framework.
The Bring takes care of Dependency Injection (DI) into the Bean and manages full Bean lifecycle.
Description for annotations and ApplicationContext:
@Configuration:
This annotation is a marker that applicable to class and by this annotation the Bring is looking for beans configurations.
@Bean:
This annotation is usually applicable to POJO that cannot be marked as @Component and in cases when we need to provide custom initialization for the POJO object.
NOTE: CLASS HAVE TO CONTAIN EMPTY CONSTRUCTORImagine that you have external library, and you want to pass to the Bring all the control of lifecycle of this POJO:
Step 1 -> Create the configuration class (class that is marked as @Configuration)
Step 2 -> Create the method that returns required POJO and mark this method as @Bean
If you need to pass some unique name -> @Bean("bean_name")
@Component:
This annotation is applicable to class. The Bring will create a bean and will manage all lifecycle of this bean.
NOTE: CLASS HAVE TO CONTAIN EMPTY CONSTRUCTORAnnotate class as @Component, in case presented on the picture below, the name for the bean will be "com.bobocode.svydovets.beans.scanner.quoter.books.HarryPotterQuoter"
You can pass your custom name for bean. This is helpful in cases when tou have beans that implement one interface, this works in conjunction with @Inject annotation. In the case on the picture below the bean name will be "hp""
@Inject:
This annotation makes Dependency Injection (DI) into bean.
This annotation applicable in classes that marked as @Component or if we create a bean (via @Bean annotation) for the class where we use it. You can mark by this annotation a field that is a bean or an interface that have realization that is marked as @Component or @Bean and the Bring-Bean-Container contains it, then the Bring will inject it. If Bring-Bean-Container doesn't have it -> the NoSuchBeanDefinitionException will be thrown. If an interface contains more than one realization, then the NoUniqueBeanDefinitionException will be thrown.
@PostConstruct:
This annotation is a bean lifecycle callback applied to the method that performs additional initialization
ApplicationContext:
Description for the ApplicationContext:
The ApplicationContext is the main interface in the Bring. It has realizations for some configurations of bean lifecycle regulations and control. Nowadays, the Bring contains only AnnotationConfigurationApplicationContext but we are on the way of evolving and will add other ConfigurationApplicationContext (for instance XmlConfigurationApplicationContext).
Also, the ApplicationContext provides the methods that provide required beans:
- getBean(Class beanType) - provides bean by the bean type
- getBean(String beanName) - provides bean by the bean name
- getBean(String beanName, Class beanType) - provides bean by the bean name and by the bean type
- getBeans(Class<?> beanType) - provides beans by the bean type
BeanPostProcessor:
This interface defines callback methods that you can implement to provide you own bean instantiation logic to customize beans someway, etc. PostConstruct methods will be applied to all beans during creation. You can define one or more postprocessors they will work sequentially.
How to add dependency to Gradle project:
- Add maven repository to your build.gradle. You need your github username and github packages read token.
repositories {
mavenCentral()
maven {
url = uri("https://maven.pkg.github.com/maingroon/svydovets-bring")
credentials {
username = 'your_github_email'
password = 'bring_token'
}
}
}
- Replace your_github_emailt on your email from the GitHub account
- Contact Bring team and ask a token for downloading dependency
- Replace bring_token on provided from Bring team
- Add dependency:
implementation 'com.svydovets:svydovets-bring-framework:0.0.1-SNAPSHOT'
How to add dependency to maven project:
- Create in your .m2 (Windows example of this folder C:\Users\username.m2) folder setting.xml file. If the file is already exists go to point 2.
- Add to the file:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2</url>
</repository>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/maingroon/svydovets-bring</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>your_github_email</username>
<password>bring_token</password>
</server>
</servers>
</settings>
- Replace your_github_email in this file on your email from the GitHub account
- Contact Bring team and ask a token for downloading dependency
- Replace bring_token on provided from Bring team
- Add dependency:
<dependency>
<groupId>com.svydovets</groupId>
<artifactId>svydovets-bring-framework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Here you can clone preconfigured maven Web project
Here you can clone preconfigured maven project with 'main' method as start point for application