栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

03 Spring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

03 Spring

目录

一、什么是依赖注入

简单来说,控制反转是创建对象,依赖注入是为对象的属性赋值。

二、DI_依赖注入三种方式

1.Setter注入

2.构造方法注入

3.自动注入

自动注入有两种配置方式:

autowire的取值如下:

测试自动注入:

三、DI_依赖注入类型

1.在被注入类中编写需要注入的属性名及类型

2.测试方法

3.在xml文件中编写注入代码

(1)注入bean类型:private StudentDao studentDao;

(2)注入基本数据类型:private String name; //字符串类型                                          private int count; //基本数据类型

(3)注入List集合:private List names; // 字符串类型List集合                                 private List students1; // 对象类型List集合

(4)注入Set集合:private Set students2; // 对象类型Set集合

(5)注入Map集合:    private Map names2; //字符串类型Map集合                                     private Map students3;// 对象类型Map集合,student>,string>

(6)注入Properties对象:private Properties properties;//Properties类型


一、什么是依赖注入

 依赖注入(Dependency Injection,简称DI),它是Spring控制反转思想的具体实现。

控制反转将对象的创建交给了 Spring ,但是对象中可能会依赖其他对象。比如service 类中要有 dao 类的属性,我们称 service 依赖于dao。之前需要手动注入属性值,代码如下:
public interface StudentDao {
   Student findById(int id);
 }
public class StudentDaoImpl implements StudentDao{ 
    @Override
    public Student findById(int id) {
        // 模拟根据id查询学生
        return new Student(1,"百战程序员","北京");
   }
 }
public class StudentService {
// service依赖dao,手动注入属性值,即手动维护依赖关系
    private StudentDao studentDao = new StudentDaoImpl();
    public Student findStudentById(int id){
        return studentDao.findById(id);
   }
}
此时,当 StudentService 的想要使用 StudentDao 的另一个实现类如StudentDaoImpl2时,则需要修改 Java 源码,造成代码的可维护性降低。 而使用 Spring 框架后, Spring 管理 Service 对象与 Dao 对象,此时它能够为Service 对象注入依赖的 Dao 属性值。这就是 Spring 的依赖注入。

简单来说,控制反转是创建对象,依赖注入是为对象的属性赋值。

二、DI_依赖注入三种方式

 在之前的开发中,可以通过setter方法或构造方法设置对象的属性值,在Spring框架中,我们可以通过调用setter方法或构造方法给属性赋值。

1.Setter注入

(1)被注入类编写属性的setter方法:(setter注入需要在类中编写setter方法)

public class StudentService {
    private StudentDao studentDao;
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
   }
}

(2)配置文件中,给需要注入属性值的中设置

:配置对象          通过setter方法注入    通过构造器注入


        
                
                
                
         

(3)测试是否注入成功

public class TestContainer {
    @Test
    public void t1(){
//        创建Spring容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//        从容器中获取对象
        StudentService studentService = (StudentService) ac.getBean("studentService");
        System.out.println(studentService);
        System.out.println(studentService.getStudentDao());
        System.out.println(studentService.findStudentById(1));
    }
}

(4)测试结果

标明注入成功! 

2.构造方法注入

(1)被注入类编写有参的构造方法:(构造方法注入需要在类中编写构造方法)

public class StudentService {
    private StudentDao studentDao;
    public StudentService(StudentDao studentDao) {
        this.studentDao = studentDao;
   }
}

(2)给需要注入属性值的中设置

  
        
                
                
                
        

(3)测试是否注入成功、测试结果 如setter注入一样

3.自动注入

 自动注入不需要在 标签中添加其他标签注入属性值,而是自动从容器中找到相应的bean对象设置为属性值。

自动注入有两种配置方式: 1.全局配置:在 中设置 default - autowire 属性可以定义 所有 bean 对象的自动注入策略。(一般不使用) 2.局部配置:在 中设置 autowire 属性可以定义 当前 bean 对象的自动注入策略。(一般使用)

autowire的取值如下: no :不会进行自动注入。 default :全局配置 default 相当于 no ,局部配置 default 表示使用全局配置 byName :在 Spring 容器中查找 id 与属性名相同的 bean ,并进行注入。需要提供 set 方法。 byType :在 Spring 容器中查找类型与属性类型相同的 bean ,并进行注入。需要提供 set 方 法。 constructor :在 Spring 容器中查找 id 与属性名相同的 bean ,并进行注入。需要提供构造方 法。

测试自动注入:

1.为依赖的对象提供setter和构造方法

public class StudentService {
    // 依赖
    private StudentDao studentDao;
    // 构造方法
    public StudentService() {
   }
    
    public StudentService(StudentDao studentDao) {
        this.studentDao = studentDao;
   }
    // setter方法
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
   }
    // 调用依赖的方法
    public Student findStudentById(int id) {
        return studentDao.findById(id);
   }
}

2.配置自动注入:

(1)配置局部自动注入




   

   

(2)配置全局自动注入


三、DI_依赖注入类型

 DI支持注入bean类型、基本数据类型和字符串、List集合、Set集合、Map集合、Properties对象类型等,他们的写法如下:

1.在被注入类中编写需要注入的属性名及类型
public class StudentService {
    private StudentDao studentDao;//bean类型
    private String name; //字符串类型
    private int count; //基本数据类型
    private List names; // 字符串类型List集合
    private List students1; // 对象类型List集合
    private Set students2; // 对象类型Set集合
    private Map names2; //字符串类型Map集合
    private Map students3;// 对象类型Map集合
    private Properties properties;//Properties类型
     // 省略getter/setter/toString
}

2.测试方法
@Test
    public void t3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");
        StudentService studentService = (StudentService)ac.getBean("studentService");
        System.out.println(studentService);
    }

3.在xml文件中编写注入代码

(1)注入bean类型:private StudentDao studentDao;

写法一:


    
        
        

写法二:



     
     
            
     

(2)注入基本数据类型:private String name; //字符串类型
                                          private int count; //基本数据类型


        
        
        
        
            10
        

(3)注入List集合:private List names; // 字符串类型List集合
                                 private List students1; // 对象类型List集合

注入简单数据类型List集合:private List names;



        
        
        
        
            10
        

注入对象类型List集合:private List students1; 



        
            
                
                    
                    
                    
                
                
                    
                    
                    
                
            
        

(4)注入Set集合:private Set students2; // 对象类型Set集合

注入Set集合和List集合大体一样,只是把标签换成标签



        
            
                
                    
                    
                    
                
                
                    
                    
                    
                
            
        

(5)注入Map集合:private Map names2; //字符串类型Map集合
                                 private Map students3;// 对象类型Map集合

注入简单数据类型Map集合:private Map names2;



        
            
                
                
            
        

注入对象数据类型Map集合: private Map students3;



        
            
                
                
            
        



        
        
        



        
        
        

(6)注入Properties对象:private Properties properties;//Properties类型


        
            
                值1
                值2
            
        

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1032366.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号