回顾Spring框架

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了回顾Spring框架相关的知识,希望对你有一定的参考价值。

Spring框架:

     传统JavaEE解决企业级应用问题时的“重量级”架构体系,使它的开发效率,开发难度和实际的性能都令人失望。Spring是以一个

救世主的身份降临在广大的程序员面前。Spring致力于JavaEE应用的各种解决方案,而不是仅仅专注于某一层的方案也可以说Spring

是一个企业级应用开发的一站式选择。Spring贯穿表现层,业务层,持久层。然而,Spring并不是为了取代他们而出现而是以高度的

开放性与它们无缝整合。

Spring核心:

IOC控制反转(Inversion of Control )也被称为依赖注入(Dependency Injection ,DI)是面向对象编程中的一种设计理念,用来减低

程序代码之间的耦合度

AOP面向切面编程(Aspect Oriented Programming,AOP)是软件编程思想发展到一定阶段的产物是面向对象编程(Object Oriented Programming)

的有益补充。AOP一般适用于有横切逻辑的场合,如访问控制,事务管理,性能检测等。

技术分享

 

IOC和AOP使用扩展

使用多种方式实现依赖注入:

第一步创建出Car类

package cn.happy.day01.entity;

public class Car {
   private String color;
   
    @Override
    public String toString() {
        return "Car [color=" + color + "]";
    }

    public String getColor() {
        return color;
    }
    
    public void setColor(String color) {
        this.color = color;
    }
}

第二步创建出一个实体类Student

package cn.happy.day01.entity;

public class Student {
  private String name;
  private int age;
    
  private Car myCar;
  

    
    public Student() {
       System.out.println("init student");
    }
    
    
    
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", myCar=" + myCar
                + "]";
    }



    
    public Student(String name, int age, Car myCar) {
        System.out.println("i am has args constructor");
        this.name = name;
        this.age = age;
        this.myCar = myCar;
    }



    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Car getMyCar() {
    return myCar;
}
public void setMyCar(Car myCar) {
    this.myCar = myCar;
}
}

接下来就就是编写applicationContext.xml配置

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        ">
        
        <bean id="mmCar" class="cn.happy.day01.entity.Car">
           <property name="color" value="green color"></property>
        </bean>
        
     <!--  <bean id="stu1" class="cn.happy.day01.entity.Student">
          <property name="name" value="微冷的雨"></property>
          <property name="age" value="18"></property>
          <property name="myCar" ref="mmCar"></property>
      </bean> -->
      
       <!--01.构造器注入 -->
       <bean id="stu2" class="cn.happy.day01.entity.Student">
           <constructor-arg index="0" value="微冷的雨"></constructor-arg>  
           <constructor-arg index="1" value="18"></constructor-arg>  
            <constructor-arg index="2"  ref="mmCar"></constructor-arg>  
       </bean>
       
       <!--02.p命名空间注入 -->
      <bean id="stu3" class="cn.happy.day01.entity.Student" p:name="国庆放假买手机" p:age="18" p:myCar-ref="mmCar">
        
       </bean>
       
     
       <!--05.各种类型注入之     集合   之   List-->
       <bean id="list" class="cn.happy.day01.entity.CollectionBean">
          <property name="list">
            <list>
              <value>方言</value>
              <value>李小龙</value>
            </list>
          </property>
       </bean>
       <!--06.各种类型注入之     集合   之   Set-->
       <bean id="set" class="cn.happy.day01.entity.CollectionBean">
              <property name="set">
                <set>
                  <value>方言</value>
                  <value>李小龙</value>
                </set>
              </property>
           </bean>
    
      <!--07.各种类型注入之     集合   之   Map-->
         <bean id="map" class="cn.happy.day01.entity.CollectionBean">
              <property name="map">
                <map>
                   <entry key="fy">
                     <value>方言</value>
                   </entry>
                    <entry key="lxl">
                     <value>李小龙</value>
                   </entry>
                </map>
              </property>
           </bean>
 
     
</beans>

关于上面注入集合的内容创建一个集合的类即可:

package cn.happy.day01.entity;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 集合Bean
 * @author Happy
 *
 */
public class CollectionBean {
   private List<String> list;
   private Set<String> set;
   private  Map<String,String> map;
   private String [] empName;//数组
   private Properties pp;//Properties的使用
   
    public String[] getEmpName() {
    return empName;
}

public void setEmpName(String[] empName) {
    this.empName = empName;
}

    public Properties getPp() {
    return pp;
}

public void setPp(Properties pp) {
    this.pp = pp;
}

    public List<String> getList() {
        return list;
    }
    
    public void setList(List<String> list) {
        this.list = list;
    }

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
  
    
    
   
} 

最后我们编写测试类测试即可

package cn.happy.day01.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.happy.day01.entity.CollectionBean;
import cn.happy.day01.entity.Student;
public class Spring_01Test {
    public static void main(String[] args) {
        testCollectionMap();
    }
    /**
     * 1.1 spring容器创建的时候,会将所有配置的bean对象创建
     */
    /*@Test
    public void testOne(){
        //容器一旦生成,bean都被加载到内存
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student)ctx.getBean("stu1");
        System.out.println(stu);
        
        Student stu = (Student)ctx.getBean("stu1");
        Student stu2 = (Student)ctx.getBean("stu1");
        System.out.println(stu);
        System.out.println(stu2);
        
    }*/
    
    static void testCollectionMap(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        CollectionBean bean=(CollectionBean)ctx.getBean("map");
        System.out.println(bean.getMap());
    }

    
    //every type injection  of Collection  Set
            static void testCollectionSet(){
                ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
                CollectionBean bean=(CollectionBean)ctx.getBean("set");
                System.out.println(bean.getSet());
            }
    
    //every type injection  of Collection  List
        static void testCollectionList(){
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            CollectionBean bean=(CollectionBean)ctx.getBean("list");
            System.out.println(bean.getList());
        }
    
        
                
    //04.域属性的注入
    static void testJavaBean(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu=(Student)ctx.getBean("stu1");
        System.out.println(stu);
    }
    
    
    //03.普通属性注入
    static void testUseful(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        
    
    }
    
    
    //02.P命名空间注入
    static void testPInjection(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student)ctx.getBean("stu3");
        System.out.println(stu);
    }
    //01.1构造注入 
        static void testConstructorInjection2(){
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
            Student stu = (Student)ctx.getBean("stu2");
            System.out.println(stu);
            
        }
    
    //01.构造注入
    static void testConstructorInjection(){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = (Student)ctx.getBean("stu2");
        System.out.println(stu);
        
    }

}

 

以上是关于回顾Spring框架的主要内容,如果未能解决你的问题,请参考以下文章

我的自定义Spring框架 | 回顾Spring框架的基本使用

spring基础回顾

回顾Spring框架

第245天学习打卡(知识点回顾 spring IOC)

SpringCloud升级之路2020.0.x版-4.maven依赖回顾以及项目框架结构

第256天学习打卡(知识点回顾 spring5知识点回顾)