Spring框架的知识点

版权声明:该博客版权归小囧子 https://blog.csdn.net/qq_38733836/article/details/88576227

**

最近在学习java后端技术。整理下Spring框架的知识点

**
Spring介绍
Spring是一个基于IOC和AOP的结构J2EE系统的框架
IOC 反转控制 是Spring的基础,Inversion Of Control
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

Spring框架概述

框架优点###
轻量级的容器框架没有侵入性
使用IoC容器更加容易组合对象直接间关系,面向接口编程,降低耦合
Aop可以更加容易的进行功能扩展,遵循ocp开发原则
创建对象默认是单例的,不需要再使用单例模式进行处理
实例说明###
在不使用spring框架之前,我们的service层中要使用dao层的对象,不得不在service层中new一个对象。如下:
//dao层对象

public class UserDao{  
   public void insert(User user){}  
}  
   
//service层对象  
public class UserService{  
   public void insert(User user){  
       UserDao userdao = new UserDao();  
       userdao.insert(user);  
   }  
}  

使用框架后:service层要用dao层对象需要配置到xml配置文件中,至于对象是怎么创建的,关系是怎么组合的都交给了spring框架去实现。
//dao层对象

public class UserDao{  
    public void insert(User user){}  
}  
   
//service层对象  
public class UserService{  
   private UserDao userdao;  
   
   public UserDao getUserdao() {  
      return userdao;  
   }  
   public void setUserdao(UserDao userdao) {  
      this.userdao= userdao;  
   }  
   
   public void insert(User user){  
      userdao.insert(user);  
   }  
   
}  

注入对象###
Product类中有Clothes对象的setter getter

package com.pandaXiong.pojo;
 
public class Product {
 
    private int id;
    private String name;
    private Clothes clothes;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Clothes getClothes() {
        return clothes;
    }
    public void setClothes(Clothes clothes) {
        this. clothes = clothes;
    }
}

在创建Product的时候注入一个Clothes对象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx 
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context      
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <bean name="c" class="com.pandaXiong.pojo.Clothes">
        <property name="name" value="clothes1" />
    </bean>
    <bean name="p" class="com.pandaXiong.pojo.Product">
        <property name="name" value="product1" />
        <property name="clothes" ref="c" />
    </bean>
 
</beans>

Spring注解
xml配置每一个bean实在太麻烦,我们可以只用注解来配置bean
@Component(“pruduct”) //为Product类加上@Component注解,即表明此类是bean
@Scope(“prototype”) //bean的作用域默认为singleton
public class Pruduct {
private int id;
private String name = “pruduct1”;
@Autowired //自动装配 @Resource(name=“category”)效果一样
private Category category;
//在属性前加上@Autowired 这种方式外,也可以在setCategory方法前加上@Autowired,这样来达到相同的效果

}

Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。
在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。 Spring2.5为我们引入了组件自动扫描机制,他可以在类路径底下寻找标注了@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
@Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Service服务层组件,用于标注业务层组件,表示定义一个bean,自动根据bean的类名实例化一个首写字母为小写的bean,
@Controller用于标注控制层组件(如struts中的action)
@Repository持久层组件,用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

猜你喜欢

转载自blog.csdn.net/qq_38733836/article/details/88576227