Java教程

原始Spring Java配置类 实现MVC

本文主要是介绍原始Spring Java配置类 实现MVC,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

是Spring4.x推荐的配置方式 但我觉得一点用都没。大概逻辑如下 :

 

 

package com.bihu.conno.helloDao;

public interface HelloDao {
    void show();
}
HelloDao
package com.bihu.conno.helloDaoImpl;


import com.bihu.conno.helloDao.HelloDao;

public class HelloDaoImpl implements HelloDao {
    public HelloDaoImpl() {
    }

    @Override
    public void show() {
        System.out.println("Dao -- Show");
    }
}
HelloDaoImpl
package com.bihu.conno.helloService;

public interface HelloService {
    void show();
}
HelloService
package com.bihu.conno.HelloServiceImpl;

import com.bihu.conno.helloDao.HelloDao;
import com.bihu.conno.helloService.HelloService;

public class HelloServiceImpl implements HelloService {
    //HelloDao
    HelloDao dao;

    //构造
    public HelloServiceImpl(HelloDao helloDao) {
        this.dao = helloDao;
    }

    @Override
    public void show() {
        dao.show(); //调用Dao的Show
        System.out.println("Service -- Show");
    }
}
HelloServiceImpl
package com.bihu.conno.Controller;

import com.bihu.conno.helloService.HelloService;

public class HelloController {
    //HelloService
    HelloService service;

    //构造
    public HelloController(HelloService service) {
        this.service = service;
    }

    public  void show() {
        service.show();
    }

}
HelloController
package com.bihu.conno;


import com.bihu.conno.Controller.HelloController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Config.class);
        HelloController bean = app.getBean(HelloController.class);
        bean.show();
    }
}
Config配置类
package com.bihu.conno;


import com.bihu.conno.Controller.HelloController;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Config.class);
        HelloController bean = app.getBean(HelloController.class);
        bean.show();
    }
}
Test测试类

 

运行:

 

这篇关于原始Spring Java配置类 实现MVC的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!