佳林L

博客园 首页 新随笔 联系 订阅 管理

Spring

● Spring是轻量级开源的 JavaEE 框架。

● Spring可以解决企业应用开发的复杂性。

● Spring有两个核心部分:IOC与Aop。

○ IOC(Inversion of Control):控制反转,把创建对象的过程交给Spring进行管理。

○ Aop(Aspect Oriented Programming):面向切面,不修改源代码进行功能增强。

● Spring的特点:

○ 方便解耦,简化开发。

○ Aop编程支持。

○ 方便和其他框架进行整合。

○ 方便进行事务操作。


什么是IOC?

○ 控制反转,把对象创建和对象之间的操作交给Spring进行管理。

○ 使用IOC的目的:为了降低耦合度。

IOC底层实现原理:XML解析,工厂模式,反射。

IOC(接口):

○ IOC思想基于IOC容器,IOC容器底层就是工厂对象。

○ Spring提供IOC容器实现两种方式:两个接口

①:BeanFactory:IOC容器的基本实现,是Spring内部的使用接口,负责创建bean的实例。不提供开发人员进行使用,*加载 配置文件时候不会创建对象,在获取对象的时候才去创建对象。

②:ApplicationContext:BeanFactory的子接口,更多的是负责容器功能的实现。提供更多强大的功能,一般由开发人员使用,*加载配置文件的时候就直接创建配置文件中的对象。


IOC操作Bean管理:

什么是Bean管理?

○ Spring创建对象。

○ Spring注入属性。

Bean管理有两种方式:

○ 基于XML配置文件方式实现。

○ 基于注解方式实现。

对象创建与属性注入(DI):

            <!-- 使用set方法注入属性 -->
       <bean id="user" class="com.djl.spring.User">
           <property name="name" value="董佳林"></property>
           <!--
           name:就是类中的属性
           value:向属性注入的值
           -->
           <property name="age" value="20"></property>
       </bean>

           <!-- 使用有参构造注入属性 -->
       <bean id="User" class="com.djl.spring.User">
           <constructor-arg name="age" value="21"></constructor-arg>
           <constructor-arg name="name" value="董佳林"></constructor-arg>
       </bean>
</beans>

注入内部bean:

 <!-- 内部bean-->
   <bean name="persen" class="com.djl.service.Persen">
       <property name="studentDaoImp">
           <bean name="studentDaoImp" class="com.djl.dao.Imp.StudentDaoImp"></bean>
       </property>
   </bean>

注入外部bean:

<!-- 注入外部bean-->
   <bean name="persen" class="com.djl.service.Persen">
       <property name="studentDaoImp" ref="studentDaoImp"></property>
   </bean>

   <bean name="studentDaoImp" class="com.djl.dao.Imp.StudentDaoImp">
       <property name="name" value="董佳林"></property>
   </bean>

IOC操作Bean管理(xml注入集合属性):

○ 注入数组,集合类型属性:

   <bean name="persen" class="com.djl.service.Persen">
           <!-- 注入数组 -->
           <property name="strings">
               <array>
                   <value>张三</value>
                   <value>李四</value>
                   <value>王五</value>
               </array>
           </property>
           <!-- 注入list集合 -->
           <property name="list">
               <list>
                   <value>董佳林</value>
                   <value>孙玉琴</value>
               </list>
           </property>
           <!-- 注入map集合 -->
           <property name="map">
               <map>
                   <entry key="d" value="jl"></entry>
                   <entry key="s" value="yq"></entry>
               </map>
           </property>
       </bean>

在集合里面设置对象类型值:

    <!-- 在集合里面设置对象类型属性-->
   <bean name="persen" class="com.djl.service.Persen">
       <property name="stulist">
           <list>
               <ref bean="student"></ref>
               <ref bean="student1"></ref>
           </list>
       </property>
   </bean>

   <bean name="student" class="com.djl.service.Student">
       <property name="name" value="董佳林"></property>
   </bean>

   <bean name="student1" class="com.djl.service.Student">
       <property name="name" value="孙玉琴"></property>
   </bean>

把集合注入部分提取出来:

①:在Spring配置文件中引入名称空间 util 。

<?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:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                                                                     http://www.springframework.org/schema/util
                                                                     http://www.springframework.org/schema/util/spring-util.xsd">

②:使用 util 标签完成list集合注入提取。

     <!-- 提取list集合-->
   <util:list id="studentlist">
       <value>董佳林</value>
       <value>孙玉琴</value>
   </util:list>
   
   <!--创建对象-->
   <bean id="persen" class="com.djl.service.Persen">
       <property name="list" ref="studentlist"></property>
   </bean>

IOC 操作Bean管理:(FactoryBean)

○ 在Spring中有两种类型Bean,一种是普通Bean,另外一种工厂Bean(FactoryBean)。

○ 普通Bean:在配置文件中定义什么类型就返回什么类型。

○ 工厂Bean:在配置文件定义bean类型可以和返回类型不一样。

IOC 操作Bean管理:(Bean的作用域)

○ Spring默认Bean是单例对象。

如何设置单实例还是多实例?

○ 在配置文件bean标签里面有属性(scope)用于设置单实例还是多实例。

○ scope属性:

第一个值(默认值):singleton,表示是单实例对象。

第二个值:prototype,表示是多实例对象。

<bean name="student1" class="com.djl.service.Student" scope="prototype"></bean>

IOC 操作Bean管理:(Bean生命周期)

Bean的生命周期:

○ 通过构造器创建Bean的实例(无参构造)。

○ 为Bean的属性设置值和对其他Bean的引用(调用set方法)。

○ 调用Bean的初始化方法(需要进行配置)。

○ 使用Bean。

○ 当容器关闭的时候,调用Bean的销毁的方法(需要进行配置销毁的方法)。

● 如使用Bean的后置处理器,Bean的生命周期有七步:

条件:此类要实现 BeanPostProcessor 接口。

Bean的生命周期:

○ 通过构造器创建Bean的实例(无参构造)。

○ 为Bean的属性设置值和对其他Bean的引用(调用set方法)。

○ 把Bean实例传递Bean的后置处理器的方法 postProcessBeforeInitialization 。

○ 调用Bean的初始化方法(需要进行配置)。

○ 把Bean实例传递Bean后置处理器的方法 postProcessAfterInitialization 。

○ 使用Bean。

○ 当容器关闭的时候,调用Bean的销毁的方法(需要进行配置销毁的方法)。


IOC操作Bean(自动装配):

○ 根据指定属性名称或者属性类型,Spring自动将匹配的属性值进行注入。​

    <bean id="persen" class="com.djl.service.Persen" autowire="byName"></bean>
   <bean id="persen" class="com.djl.service.Persen" autowire="byType"></bean>

IOC容器Bean管理(注解方式):

○ @Component 所有组件

○ @Service 一般用在业务层

○ @Controller 一般用在web层

○ @Repository 一般用于dao层

基于注解实现对象:

① 导入Aop架包。

② 开启组件扫描。

     <!--
       component-scan:组件扫描
    -->
   <context:component-scan base-package="com.djl.spring"></context:component-scan>

③ 创建一个类打上注解。

使用properties进行属性赋值:

 <context:property-placeholder location="classpath:property.properties"></context:property-placeholder> 
 
   <bean id="cat" class="com.djl.bean.Cat">
       <property name="name" value="${u_username}"></property>
   </bean>

基于注解方式实现属性注入:

○ @AutoWired:根据属性类型进行自动装配。

○ @Qualifier:根据属性的名称进行注入。搭配@AutoWired一起使用。

○ @Resource:可以根据类型注入,也可以根据名称注入。

○ @value:注入普通属性。

完全注解开发:

①:创建配置类。

@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.djl.bean"}) //代表xml中 组件扫描
public class Config {

}

②:编写测试类。

public class test {
   public static void main(String[] args) {
       
    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
       Student student = context.getBean("student", Student.class);
       student.getname();
       System.out.println(student);
  }
}

 

posted on 2020-09-18 13:42  佳林L  阅读(245)  评论(0编辑  收藏  举报