Handwritten MVC framework - example of using IOC alone

-------Previous: Handwritten MVC framework (two) - code implementation and use examples------


I used the GMVC framework when developing the GMQ framework, and found some inconveniences in the process of using, which were also optimized. Currently, GMQ transmission is based on http transmission. I plan to use netty instead. Because my code is based on GMVC, now I need to peel off the web layer and only use IOC function. So I went through the GMVC framework again and found that it supports this.

IOC usage example

1. Add dependency

gmc project: https://gitee.com/simpleha/gmvc.git


2. Write business code

package com.shuimutong.gmvc_ioc_demo.service;

public interface TestService {
    void speak();
    String convertString(String s);

package com.shuimutong.gmvc_ioc_demo.service.impl;

import com.shuimutong.gmvc.annotation.XAutowired;
import com.shuimutong.gmvc.annotation.XService;
import com.shuimutong.gmvc_ioc_demo.bean.Person;
import com.shuimutong.gmvc_ioc_demo.dao.TestDao;
import com.shuimutong.gmvc_ioc_demo.service.TestService;

public class TestServiceImpl implements TestService {
    private TestDao testDao;
    public void speak() {

    public String convertString(String s) {
        Person p = testDao.findPerson();
        String perString = "addStr:" + s 
                + String.format(",Person(name:%s,age:%d)", p.getName(), p.getAge());
        return perString;


package com.shuimutong.gmvc_ioc_demo.dao;

import com.shuimutong.gmvc_ioc_demo.bean.Person;

public interface TestDao {
    Person findPerson();

package com.shuimutong.gmvc_ioc_demo.dao.impl;

import com.shuimutong.gmvc.annotation.XRepository;
import com.shuimutong.gmvc_ioc_demo.bean.Person;
import com.shuimutong.gmvc_ioc_demo.dao.TestDao;

public class TestDaoImpl implements TestDao {

    public Person findPerson() {
        return new Person();



3. Entry code

public class App {
    public static void main( String[] args ) {
        Map<String, String> packageMap = new HashMap();
        //Scan package directory
        packageMap.put(GmvcSystemConst.BASE_PACKAGE, "com.shuimutong.gmvc_ioc_demo");
        try {
        } catch (Exception e) {
        //Get an instance of a class
        TestService testService = (TestService) InstanceManager.getEntityByClazz(TestServiceImpl.class);
        String msg = testService.convertString("Hello,GMVC-IOC");


4. Start up


IOC is successful.

Keywords: Java Netty git snapshot

Added by groovything on Sun, 12 Apr 2020 17:17:29 +0300