zoukankan      html  css  js  c++  java
  • 吴裕雄天生自然SPRINGSpring java配置

    package dao;
    
    //此处没有使用@Repository声明Bean
    public class TestDao {
        public void save() {
            System.out.println("TestDao save");
        }
    
    }
    package service;
    
    import dao.TestDao;
    
    //此处没有使用@Service声明Bean
    public class TestService {
        // 此处没有使用@Autowired注入testDao
        TestDao testDao;
    
        public void setTestDao(TestDao testDao) {
            this.testDao = testDao;
        }
    
        public void save() {
            testDao.save();
        }
    
    }
    package controller;
    
    import service.TestService;
    
    //此处没有使用@Controller声明Bean
    public class TestController {
        // 此处没有使用@Autowired注入testService
        TestService testService;
    
        public void setTestService(TestService testService) {
            this.testService = testService;
        }
    
        public void save() {
            testService.save();
        }
    
    }
    package javaConfig;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import dao.TestDao;
    import service.TestService;
    import controller.TestController;
    
    //一个配置类,相当于一个Spring配置的XML文件;
    //此处没有使用包扫描,是因为所有Bean都在此类中定义了。
    @Configuration
    public class JavaConfig {
        @Bean
        public TestDao getTestDao() {
            return new TestDao();
        }
    
        @Bean
        public TestService getTestService() {
            TestService ts = new TestService();
            // 使用set方法注入testDao
            ts.setTestDao(getTestDao());
            return ts;
        }
    
        @Bean
        public TestController getTestController() {
            TestController tc = new TestController();
            // 使用set方法注入testService
            tc.setTestService(getTestService());
            return tc;
        }
    
    }
    package javaConfig;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import controller.TestController;
    
    public class TestConfig {
        public static void main(String[] args) {
            // 初始化Spring容器ApplicationContext
            AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(JavaConfig.class);
            TestController tc = appCon.getBean(TestController.class);
            tc.save();
            appCon.close();
        }
    
    }

     

  • 相关阅读:
    LeetCode刷题记录2020-10-07之动态规划入门!!!线性DP(二)
    LeetCode刷题记录2020-10-06之动态规划入门!!!线性DP
    LeetCode刷题记录2020-10-05之Double Pointer!!!
    Python核心编程之属性查找过程!
    Python核心编程之元类!
    Python配置文件的导入方式和源码分析!
    大数据架构入门之二:埋点数据流程
    day46 css第二part
    day44天 HTTP协议 和前端html协议
    day39 视图 触发器 事务 存储过程 函数 流程控制 索引与慢查询优化
  • 原文地址:https://www.cnblogs.com/tszr/p/15310225.html
Copyright © 2011-2022 走看看