Spring框架浅入

SpringFramework概述及IOC

一:Spring概述、Spring体系介绍

Spring的核心是控制反转(IoC)和面向切面(AOP)

spring是开源的、轻量级的、一站式的框架,以 IoC(Inverse Of Control: 反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核

二:Spring安装配置

在工程目录下的pom.xml中配置

<dependency>
     <groupId>org.springframeworkgroupId>
     <artifactId>spring-contextartifactId>
     <version>5.2.0.RELEASEversion>
  dependency>

可在网上查找,粘贴过来

三:IOC

IOC容器初始化

ApplicationContext实现类

ApplicationContext是一个接口,有以下几个不同的实现类:

ClassPathXmlApplicationContext

AnnotationConfigApplicationContext

FileSystemXmlApplicationContext
XmlWebApplicationContext
ClassPathXmlApplicationContext
使用方法:
  1. 创建实体类

    package com.lanou3g.bean;
    
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    @Setter
    public class Man {
         
        private Food name;
        private String hobby;
        private int age;
        private String sex;
        private String food;
    
        public void eat(){
         
            System.out.println("我叫" + name + ",我今年" + age + "岁了,我喜欢吃:" + food);
        }
    
        public void play(){
         
            System.out.println("我叫" + name + ",我今年" + age + "岁了,我喜欢玩:" + hobby);
        }
    
    }
    
    
  2. 配置Spring xml文件

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="man" class="com.lanou3g.bean.Man">
            <property name="age" value="18">property>
            <property name="food" value="橘子">property>
            <property name="hobby" value="吃鸡">property>
    
            <property name="name" ref="apple">property>
            <property name="sex" value="">property>
        bean>
        <bean id="apple" class="com.lanou3g.bean.Food">
            <property name="name" value="张飞">property>
        bean>
    beans>
    
  3. 使用

    public class App {
         
        public static void main( String[] args ) {
         
            useSpringIOC();
    
        }
        public static void useSpringIOC(){
         
            //加载Spring上下文配置文件
            ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
            //获取bean
            Man man = (Man) cxt.getBean("man");
            Man man1 = cxt.getBean("man",Man.class);
            Man man2 = cxt.getBean(Man.class);
    
            man.eat();
            man.play();
            System.out.println("man:" + man);
            System.out.println("man1:" + man1);
            System.out.println("man2:" + man2);
        }
    }
    

    如果是两个xml文件以上,可以使用字符串数组

    ApplicationContext cxt = new ClassPathXmlApplicationContext(new String[]{“applicationContext.xml”,“applicationContext1.xml”});

    或者使用通配符

    ApplicationContext cxt = new ClassPathXmlApplicationContext(“classpath:/*.xml”);

AnnotationConfigApplicationContext
使用方法:
  1. 创建实体类

    package com.lanou3g.bean;
    
    import lombok.Getter;
    import lombok.Setter;
    
    @Getter
    @Setter
    public class Student {
         
        private String name;
        private int age;
    }
    
    
  2. 创建@Configuration配置类

    package com.lanou3g;
    
    import com.lanou3g.bean.Student;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration //表明是bean的配置类
    public class AppConfig {
         
        @Bean
        public Student student1(){
          //方法名相当于id
            Student student = new Student();
            student.setName("张三");
            student.setAge(18);
            return student;
        }
    
        @Bean
        public Student student2(){
         
            Student student = new Student();
            student.setName("赵云");
            student.setAge(20);
            return student;
        }
    }
    
    

    @configuration可理解为用Spring的时候xml里面的标签

    @Bean可理解为用Spring的时候xml里面的标签

  3. 使用

    public class App {
         
        public static void main( String[] args ) {
         
           // useSpringIOC();
            useSpringIOC2();
    
        }
    
    public static void useSpringIOC2(){
         
         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("AppConfig.class");
            Student student1 = (Student) ctx.getBean("student1");
            System.out.println(student1.getName());
            System.out.println(student1.getAge());
    
            Student student2 = (Student) ctx.getBean("student2");
            System.out.println(student2.getName());
            System.out.println(student2.getAge());
        }
    }
    
FileSystemXmlApplicationContext
XmlWebApplicationContext

这两种跟上面的类似

Application初始化路径

路径前缀
//	前缀classpath:表示的是项目的classpath下相对路径   
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
路径中的通配符
//	使用通配符加载所有符合要求的文件  
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");

通过Xml方式配置管理bean

schema约束地址不能用https,否则每次都要从Spring加载,无网不能运行(新版本已解决这个问题)

使用步骤:

  • 创建实体类
  package com.lanou3g.bean;
  
  import lombok.Getter;
  import lombok.Setter;
  
  @Getter
  @Setter
  public class Man {
   
      private String hobby;
      private int age;
      private String sex;
      private String food;
  
      public void eat(){
   
          System.out.println("我叫" + name + ",我今年" + age + "岁了,我喜欢吃:" + food);
      }
  
      public void play(){
   
          System.out.println("我叫" + name + ",我今年" + age + "岁了,我喜欢玩:" + hobby);
      }
  }
  
  
  • 配置xml

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
      <bean id="man" class="com.lanou3g.bean.Man">
          <property name="age" value="18">property>
          <property name="food" value="橘子">property>
          <property name="hobby" value="吃鸡">property>
          <property name="name" ref="apple">property>
          <property name="sex" value="">property>
      bean>   
  beans>    

通过注解方式管理bean

两种方式:
第一种:
  1. 创建实体类
 package com.lanou3g.bean;
 
 import lombok.Getter;
 import lombok.Setter;
 import org.springframework.stereotype.Component;
 
 @Component //把普通实体类实例化到spring容器中,相当于配置文件中的
 @Getter
 @Setter
 public class Student {
   
     private String name;
     private int age;
 }
 
  1. 创建配置类
 package com.lanou3g;
 
 import com.lanou3g.bean.Student;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 
 @Configuration //表明该类是Spring的一个配置类,该类中会包含应用上下文创建bean的具体细节
 @ComponentScan(basePackages = "com.lanou3g")//开启注解扫描支持,同时指定扫描包根路径
 public class MyConfiguration {
   
 
 }
 
 
  1. 使用
 package com.lanou3g;
 
 import com.lanou3g.bean.Student;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
 public class App {
   
     public static void main( String[] args ) {
   
         //加载Spring上下文配置文件
         ApplicationContext cxt = new AnnotationConfigApplicationContext(MyConfiguration.class);
         testIOCBean(cxt);
     }
 
     public static void testIOCBean(ApplicationContext cxt){
   
         //获取bean
         Student stu = cxt.getBean(Student.class);
         System.out.println(stu.getName());
         System.out.println(stu.getAge());
     }
 }
 
第二种:
  1. 创建实体类
 package com.lanou3g.bean;
 
 import lombok.Getter;
 import lombok.Setter;
 
 @Getter
 @Setter
 public class Student {
   
     private String name;
     private int age;
 }
 
  1. 创建配置类
 package com.lanou3g;
 
 import com.lanou3g.bean.Student;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 //加上@Configuration注解后,这个类就相当于变成了一个Spring上下文配置文件
 @Configuration
 public class AppConfig {
   
     @Bean
     public Student student1(){
   //方法名相当于id
         Student student = new Student();
         student.setName("张三");
         student.setAge(18);
         return student;
     }
 
     @Bean
     public Student student2(){
   
         Student student = new Student();
         student.setName("赵云");
         student.setAge(20);
         return student;
     }
 }
 

@configuration可理解为用Spring的时候xml里面的标签

@Bean可理解为用Spring的时候xml里面的标签

  1. 使用
 public class App {
   
     public static void main( String[] args ) {
   
        // useSpringIOC();
         useSpringIOC2();
 
     }
 
 public static void useSpringIOC2(){
   
         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext("AppConfig.class");    
         Student student1 = (Student) ctx.getBean("student1");
         System.out.println(student1.getName());
         System.out.println(student1.getAge());
 
         Student student2 = (Student) ctx.getBean("student2");
         System.out.println(student2.getName());
         System.out.println(student2.getAge());

你可能感兴趣的:(spring,java)