码迷,mamicode.com
首页 > 编程语言 > 详细

Spring基础-04(Spring01-04:IOC)

时间:2020-06-07 21:51:17      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:gen   自动   org   rri   源码   abstract   conf   err   自动注入   

Book.java:

package com.atguigu.bean;

/**
 * @Title: Book
 * @Description:
 * @Author:
 * @Version: 1.0
 * @create 2020/6/7 17:05
 */
public class Book {
}

User.java:

package com.atguigu.bean;

/**
 * @Title: User
 * @Description:
 * @Author:
 * @Version: 1.0
 * @create 2020/6/7 17:05
 */
public class User {
}

BaseDao.java:

package com.atguigu.dao;

import org.springframework.stereotype.Repository;

/**
 * 定义了基本的增删改查方法
 */
public abstract class BaseDao<T> {

    public abstract void save();
}

BookDao.java:

package com.atguigu.dao;

import com.atguigu.bean.Book;
import org.springframework.stereotype.Repository;

/**
 * @Title: BookDao
 * @Description:
 * @Author:
 * @Version: 1.0
 * @create 2020/6/7 17:04
 */
@Repository
public class BookDao extends BaseDao<Book> {
    @Override
    public void save() {
        System.out.println("BaseDao...保存图书...");
    }
}

UserDao.java:

package com.atguigu.dao;

import com.atguigu.bean.User;
import org.springframework.stereotype.Repository;

/**
 * @Title: UserDao
 * @Description:
 * @Author:
 * @Version: 1.0
 * @create 2020/6/7 17:04
 */
@Repository
public class UserDao extends BaseDao<User> {
    @Override
    public void save() {
        System.out.println("USerDao...保存用户...");
    }
}

BaseService.java:

package com.atguigu.service;

import com.atguigu.dao.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 此处无需加注解,因为其它服务继承了这个基础服务,相应的内容包括注解也会被继承
 */
public class BaseService<T> {

    @Autowired
    private BaseDao<T> baseDao;

    public void save() {
        System.out.println("自动注入的dao" + baseDao);
        baseDao.save();
    }
}

BookService.java:

package com.atguigu.service;

import com.atguigu.bean.Book;
import com.atguigu.dao.BaseDao;
import com.atguigu.dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title: BookService
 * @Description:
 * @Author:
 * @Version: 1.0
 * @create 2020/6/7 17:03
 */
@Service
public class BookService extends BaseService<Book> {

}

UserService.java:

package com.atguigu.service;

import com.atguigu.bean.User;
import com.atguigu.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title: UserService
 * @Description:
 * @Author:
 * @Version: 1.0
 * @create 2020/6/7 17:03
 */
@Service
public class UserService extends BaseService<User> {

}

applicationContext.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 6 
 7     <!-- 测试泛型依赖注入★ -->
 8     <context:component-scan base-package="com.atguigu"></context:component-scan>
 9 
10 </beans>

IOCTest.java:

 1 package com.atguigu.test;
 2 
 3 import com.atguigu.service.BookService;
 4 import com.atguigu.service.UserService;
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.context.ApplicationContext;
 9 import org.springframework.test.context.ContextConfiguration;
10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11 
12 /**
13  * @Title: IOCTest
14  * @Description:
15  * @Author:
16  * @Version: 1.0
17  * @create 2020/6/7 17:11
18  */
19 @ContextConfiguration(locations = "classpath:applicationContext.xml")
20 @RunWith(SpringJUnit4ClassRunner.class)
21 public class IOCTest {
22 
23     @Autowired
24     private BookService bookService;
25 
26     @Autowired
27     private UserService userService;
28 
29     @Test
30     public void test() {
31         bookService.save();
32         userService.save();
33 
34         //spring中可以使用带泛型的父类类型来确定这个子类的类型
35         //父类的泛型:class com.atguigu.service.BaseService
36         System.out.println(bookService.getClass().getSuperclass());
37 
38         //带泛型的父类类型:com.atguigu.service.BaseService<com.atguigu.bean.Book>
39         System.out.println(bookService.getClass().getGenericSuperclass());
40 
41 
42         /**
43          * ioc总结
44          * ioc是一个容器,帮我们管理所有的组件
45          * 1.依赖注入:@Autowired:自动赋值
46          * 2.某个组件要使用Spring提供的更多(IOC、AOP)必须加入到容器中
47          *
48          * 体会:
49          * 1.容器启动,创建所有单实例bean
50          * 2.@Autowired自动装配的时候,是从容器中找这些符合要求的bean
51          * 3.ioc.getBean("bookServlet")也是从容器中找这些符合要求的bean
52          * 4.容器中包括了所有的bean
53          * 5.调试spring的源码,容器到底是什么?其实就是一个map
54          * 6.这个map中保存所有创建好的bean,并提供外界获取功能...
55          * 7.探索,单实例的bean都保存到哪个map中了【源码-扩展】
56          * 8.调试源码的思路:
57          *      从helloworld开始的,给helloworld每一个关键步骤打上断点,进去看里面都做了什么工作?
58          *      怎么知道哪些方法都是干什么的?
59          *          1)翻译这个方法是干什么的?
60          *          2)放行这个方法,看控制台,看debug的每一个变量的变化
61          *          3)看方法注释
62          * 学到的一点:1)规范注释 2)规范方法名和类名
63          */
64 
65     }
66 }

 关系图解:

技术图片

 

Spring基础-04(Spring01-04:IOC)

标签:gen   自动   org   rri   源码   abstract   conf   err   自动注入   

原文地址:https://www.cnblogs.com/116970u/p/13061849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!