《Spring上课笔记》----class2

简介: 《Spring上课笔记》----class2

一:Spring配置元数据的三种方式

1.基于Xml的配置

这种方式是完全使用xml文件进行配置的

两个实体类:

CPU:


package spring01com;
import org.springframework.stereotype.Component;
public class InterCpu implements Cpu{
  @Override
  public void run() {
    // TODO Auto-generated method stub
    System.out.println("Cpu启动");
  }
}

Computer:

package spring01com;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
public class Computer {
  Cpu cpu;
   public Cpu getCpu() {
    return cpu;
  }
  public void setCpu(Cpu cpu) {
    this.cpu = cpu;
  }
public void playGame() {
     cpu.run();
     System.out.println("玩游戏");
   }
}

beans.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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     <!--    bean就是java对象由Spring来创建和管理 -->
     <!-- <bean name="hello" class="net.xyz.bean.Hello">
         <property name="name" value="张三"></property>
     </bean> -->
<!--      团队协作通过import方式来实现的 -->
 <bean id="Computer" class="spring01com.Computer">
      <property name="cpu" ref="Cpu"></property>
 </bean>
 <bean id="Cpu"  class="spring01com.InterCpu"></bean> 
</beans>

启动类:

public class Run {
    public static void main(String[] args) {
      ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    //   ApplicationContext ctx=new AnnotationConfigApplicationContext(Run.class);
      Computer pc=(Computer) ctx.getBean("Computer");
      pc.playGame();
  }
}

2.基于注解的配置

实体类

CPU:

@Component
public class InterCpu implements Cpu{
  @Override
  public void run() {
    // TODO Auto-generated method stub
    System.out.println("Cpu启动");
  }
}

Computer:

@Component("Computer")
public class Computer {
    @Resource
  Cpu cpu;  
    public Cpu getCpu() {
    return cpu;
  }
  public void setCpu(Cpu cpu) {
    this.cpu = cpu;
  }
public void playGame() {
     cpu.run();
     System.out.println("玩游戏");
   }
}

beans.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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     <!--    bean就是java对象由Spring来创建和管理 -->
     <!-- <bean name="hello" class="net.xyz.bean.Hello">
         <property name="name" value="张三"></property>
     </bean> -->
   <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="spring01com"></context:component-scan>
</beans>

启动类:

public class Run {
    public static void main(String[] args) {
      ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    //   ApplicationContext ctx=new AnnotationConfigApplicationContext(Run.class);
      Computer pc=(Computer) ctx.getBean("Computer");
      pc.playGame();
  }
}

3.基于Java的配置

不再需要配置文件。完全使用注解配置

实体类:

CPU :

@Component
public class InterCpu implements Cpu{
  @Override
  public void run() {
    // TODO Auto-generated method stub
    System.out.println("Cpu启动");
  }
}

电脑:

@Component("Computer")
public class Computer {
    @Resource
  Cpu cpu;  
    public Cpu getCpu() {
    return cpu;
  }
  public void setCpu(Cpu cpu) {
    this.cpu = cpu;
  }
public void playGame() {
     cpu.run();
     System.out.println("玩游戏");
   }
}

启动类:

@Configuration
@ComponentScan("spring01com")
public class Run {
    public static void main(String[] args) {
      //ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
      ApplicationContext ctx=new AnnotationConfigApplicationContext(Run.class);
      Computer pc=(Computer) ctx.getBean("Computer");
      pc.playGame();
  }
}

二:Bean的实例化

1.通过构造方法实例化

Beans:


public class BeanInstance {
    public BeanInstance() {
    }
    public void show() {
      System.out.println("我是一个实体");
    }
}

配置文件:

<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <bean id="beanInstance" class="beans.BeanInstance"></bean>
</beans>

实体类:

public class Test {
  public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    BeanInstance beanInstance=(BeanInstance) ctx.getBean("beanInstance");
    beanInstance.show();
  }
}

测试类:

public class Test {
  public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    BeanInstance beanInstance=(BeanInstance) ctx.getBean("beanInstance");
    beanInstance.show();
  }
}

2.使用静态工厂方法实例化

实体类:

public class BeanInstance {
  private static BeanInstance beanInstance ;
    public BeanInstance() {
      show();
    }
    public void show() {
      System.out.println("我是一个实体");
    }
    public static  BeanInstance create() {
      if(beanInstance==null) {
          beanInstance=new BeanInstance();
          return beanInstance;
      }else {
        return beanInstance;
      }
    }
}

配置文件:

<?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:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <bean id="beanInstance" class="beans.BeanInstance" factory-method="create"></bean>
</beans>

测试类

public class Test {
  public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
    BeanInstance beanInstance1=(BeanInstance) ctx.getBean("beanInstance");
    BeanInstance beanInstance2=(BeanInstance) ctx.getBean("beanInstance");
    System.out.println(beanInstance1==beanInstance2);
  }
}

3.使用实例工厂方法实例化

实体类:

public class BeanInstance2 {
   public void show() {
     System.out.println("我是bean2");
   }
}

工厂类:

public class BeanInstance2Factory {
  public BeanInstance2 create() {
    return new BeanInstance2();
  }
}

配置文件:

 <bean id="beanInstance2Factory" class="beans.BeanInstance2Factory" ></bean>
   <bean id="beanInstance2" factory-bean="beanInstance2Factory" factory-method="create"></bean>

测试类:

  BeanInstance2 beanInstance3=(BeanInstance2) ctx.getBean("beanInstance2");
    beanInstance3.show();

三:Bean的依赖注入

1.构造器注入

CPU类:

public class InterCpu {
   public void run() {
     System.out.println("Cpu 开始运行了");
   }
}

Computer类:

public class Computer {
    private String grand;
    private InterCpu cpu;
  public Computer(String grand, InterCpu cpu) {
    super();
    this.grand = grand;
    this.cpu = cpu;
    System.out.println("cpu是"+grand);
  }
}

bean配置文件

 <bean id="InterCpu" class="di.InterCpu"></bean>
   <bean id="Computer" class="di.Computer">
      <constructor-arg value="联想"></constructor-arg>
      <constructor-arg ref="InterCpu"></constructor-arg>
   </bean>

2.setter注入

computer类:

package di;
public class Computer1 {
  private String grand;
  private String size;
  private InterCpu cpu;
  public String getGrand() {
    return grand;
  }
  public void setGrand(String grand) {
    this.grand = grand;
  }
  public String getSize() {
    return size;
  }
  public void setSize(String size) {
    this.size = size;
  }
  public InterCpu getCpu() {
    return cpu;
  }
  public void setCpu(InterCpu cpu) {
    this.cpu = cpu;
  }
  @Override
  public String toString() {
    return "Computer1 [grand=" + grand + ", size=" + size + ", cpu=" + cpu + "]";
  }
}

bean配置文件:

 <bean id="Computer1" class="di.Computer1">
     <property name="grand" value="thinkpad"></property>
     <property name="size" value="15"></property>
     <property name="cpu" ref="InterCpu"></property>
   </bean>
相关文章
|
2月前
|
Java Spring
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
【编程笔记】在 Spring 项目中使用 RestTemplate 发送网络请求
106 0
|
18天前
|
NoSQL 前端开发 Java
技术笔记:springboot分布式锁组件spring
技术笔记:springboot分布式锁组件spring
17 1
|
18天前
|
Java Linux 程序员
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
技术笔记:Spring生态研习【五】:Springboot中bean的条件注入
|
18天前
|
XML Java 数据安全/隐私保护
技术笔记:Spring中的通知(Advice)和顾问(Advisor)
技术笔记:Spring中的通知(Advice)和顾问(Advisor)
|
2月前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
60 2
|
2月前
|
Java Spring 容器
【Spring系列笔记】IOC与DI
IoC 和 DI 是面向对象编程中的两个相关概念,它们主要用于解决程序中的依赖管理和解耦问题。 控制反转是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入和依赖查找。
44 2
|
2月前
|
缓存 监控 Java
【Spring系列笔记】AOP
面向切面编程就是面向特定方法编程。通过将横切关注点(cross-cutting concerns)从主要业务逻辑中分离出来,提供一种更好的代码模块化和可维护性。 横切关注点指的是在应用程序中横跨多个模块或层的功能,例如日志记录、事务管理、安全性、缓存、异常处理等。
41 0
|
2月前
|
存储 缓存 Java
【Spring系列笔记】依赖注入,循环依赖以及三级缓存
依赖注入: 是指通过外部配置,将依赖关系注入到对象中。依赖注入有四种主要方式:构造器注入、setter方法注入、接口注入以及注解注入。其中注解注入在开发中最为常见,因为其使用便捷以及可维护性强;构造器注入为官方推荐,可注入不可变对象以及解决循环依赖问题。本文基于依赖注入方式引出循环依赖以及三层缓存的底层原理,以及代码的实现方式。
39 0
|
2月前
|
SQL Java 数据库连接
(数据库链接池)spring内容复习7月16日笔记
(数据库链接池)spring内容复习7月16日笔记
20 0
|
2月前
|
安全 Java 数据库连接
啃完这些Spring知识点,我竟吊打了阿里面试官(附面经+笔记)
对于开发同学来说,Spring 框架熟悉又陌生。 熟悉:开发过程中无时无刻不在使用 Spring 的知识点;陌生:对于基本理论知识疏于整理与记忆。导致很多同学面试时对于 Spring 相关的题目知其答案,但表达不够完整准确。