bboss ioc quick start tutorial

    bboss is a very good ioc framework with functions similar to spring ioc and google guice. This article combines a simple case to introduce the usage of bboss ioc, allowing you to quickly understand and get started using bboss ioc.
1. First introduce bboss ioc
maven coordinates into the project:

<dependency>
    <groupId>com.bbossgroups</groupId>
    <artifactId>bboss-core</artifactId>
    <version>5.0.3.5</version>
</dependency>


gradle coordinates:

compile group: 'com.bbossgroups', name: 'bboss-core', version: '5.0.3.5'


2. Write components to implement
org.gradle.IOCExample
package org.gradle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IOCExample {
	private static final Logger logger = LoggerFactory.getLogger(IOCExample.class);
	private String name ;
	private String sex;
	private String homepage;
	public void init(){
		logger.debug("init bean.............");
	}

	public String exampMethod(){
		return new StringBuilder().append("name = ").append(name).append(",")
				.append("sex = ").append(sex).append(",")
				.append("homepage = ").append(homepage).toString();
	}

}


3. Define external properties configuration - config.properties
name=Jack
homepage=http://www.bbossgroups.com


4. Configure bboss ioc

Write bboss ioc configuration file: exampile.xml, put it in the project resources directory
<!--
	bboss ioc configuration example
-->
<properties>
    <!--
    Import the external property file, bboss ioc external property reference document:
    http://yin-bp.iteye.com/blog/2325602
    -->
    <config file="config.properties"/>
    <!--
    name="examplebean" specifies the component name
    class="org.gradle.IOCExample" specifies the component implementation class
    f:name="${name:jack}" The component property name is injected, and the value is configured in the config.properties file. If the name is not configured in the external property file, the default value jack is used
    f:homepage="${homepage}" component property homepage injection, the value is configured in the config.properties file
    f:sex="male" attribute sex injection
    init-method="init" component initialization method
    -->
    <property name="examplebean"
              class="org.gradle.IOCExample"
              f:name="${name:jack}"
              f:homepage="${homepage}"
              f:sex="男"
              init-method="init"
    />

</properties>


5. Test Cases
package org.gradle;

import org.frameworkset.spi.BaseApplicationContext;
import org.frameworkset.spi.DefaultApplicationContext;
import org.junit.Test;

/**
 * Created by 1 on 2017/6/25.
 */
public class TestInvoke {
    @Test
    public void test(){
        //Initialize the ioc container
        BaseApplicationContext context = DefaultApplicationContext.getApplicationContext("example.xml");
        // get the component instance
        IOCExample example = context.getTBeanObject("examplebean",IOCExample.class);
        //call component method
        String message = example.exampMethod();
        System.out.println("message:"+message);
    }
}

6. Build and run
Install and configure the gradle environment before building and running. The gradle installation and configuration reference documentation:
http://yin-bp.iteye.com/blog/2313145Download
example: Download
the decompressed downloaded file, Then switch to cmd, execute the command in the testioc directory:
gradle releaseVersion


and then switch to the directory build/dist, run the command: start.bat to see the effect:










Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326297317&siteId=291194637
Recommended