【Spring框架】Spring核心知识点剖析(一)之IOC

柔情只为你懂 2022-03-14 14:36 324阅读 0赞

Spring核心知识点剖析(一)之IOC

最近刚开始接触Spring框架部分,写此博客记录学习内容。

Spring的基本概念

Spring:不管学过没学过,大家对Spring都有所了解。Spring是一款开放源码的Java框架,它致力于代码之间的松耦合。S
pring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

Spring的核心知识点是IOC(inverse of control)反转控制,DI(dependency injection)依赖注入和AOP(Aspect Oriented Programming)面向切面编程。

IOC控制反转

何为控制反转?举个例子来说,正常来讲如果你要谈恋爱,你得相中一个合适的女孩子,问她索要联系方式,然后聊天,谈恋爱再到结婚生孩子。控制反转就像是你把你意向的女孩信息列下来交给婚介或者媒婆,让一个中介来帮你找符合你条件的女孩,然后你们再聊天结婚生孩子。

看了上面这个例子,理解IOC相对就要简单了。IOC的思想就是反转资源获取的方向,就是Spring容器主动地将资源(如JavaBean)推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源。

IOC的三种实现方式

1、通过Spring的容器,即创建一个名为applicationContext的xml配置文件,进行配置。
代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="studentdao" class="com.imp.StudentImp">
  6. </bean>
  7. </beans>

其中,beans标签代表此xml是Spring的容器,而其中的bean标签是你需要的资源,id属性即为你为这个资源所起的名字,class属性即此资源所在的全路径(包名+类名)。
注意
(1)按照如上的配置文件的写法,你的资源即JavaBean中,必须有无参构造方法。带参构造方法及对属性的赋值会在之后的DI中讲解。
(2)如果在如上的配置文件中写入两个不同的id对应同一个的bean,则会抛出NoUniqueBeanDefinitionException异常。

对象获取完毕后获取资源的方法如下代码所示:

  1. //创建容器上下文对象
  2. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  3. //获取资源(需要强制类型转换)
  4. StudentDao studendao = (StudentImp) context.getBean("studentdao");
  5. //调用资源的方法
  6. studendao.study();

如上getBean方法的参数即对应xml容器中bean的id值。

2、通过注解的方式配置
代码如下:

  1. //开启注解的扫描(此段代码在applicationContext配置文件中存放)
  2. <context:component-scan base-package="com.bean"/>

其中base-package为你要扫描的包,即你的资源所在的位置。

  1. package com.bean;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class DogBean {
  5. private String name;
  6. private String color;
  7. public DogBean() {
  8. }
  9. public DogBean(String name, String color) {
  10. this.name = name;
  11. this.color = color;
  12. }
  13. @Override
  14. public String toString() {
  15. return "DogBean{" +
  16. "name='" + name + '\'' +
  17. ", color='" + color + '\'' +
  18. '}';
  19. }
  20. public String getName() {
  21. return name;
  22. }
  23. public void setName(String name) {
  24. this.name = name;
  25. }
  26. public String getColor() {
  27. return color;
  28. }
  29. public void setColor(String color) {
  30. this.color = color;
  31. }
  32. }

在导包代码之下,类代码开始之上进行注解即@Component。
然后在注解扫描的作用下,指定包内被@Component注解的类会在Spring容器被创建时完成创建,即如下代码:

  1. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

获取方式同第一种方法,在此不多做赘述。
注意:
(1)使用此种方法扫描,getBean获取时参数为类名(首字母小写),如类名为StudentDao,参数即为”studentDao”。
3、全注释(零配置)
全注释,即不需要配置文件,全部用注解来完成。此时需要创建一个类当作容器,并用上注解:

  1. package config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Import;
  6. import java.util.ArrayList;
  7. @Configuration
  8. public class SpringConfig {
  9. public SpringConfig() {
  10. System.out.println("容器创建成功");
  11. }
  12. }

此时的@Configuration就代替了之前在配置文件中beans,即如下代码的作用:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  4. </beans>

然后在资源中配置注解

  1. package com.bean;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Repository;
  4. @Repository(value = "studentBean")
  5. public class StudentBean {
  6. private int id;
  7. private String name;
  8. private String major;
  9. public StudentBean() {
  10. }
  11. public int getId() {
  12. return id;
  13. }
  14. public void setId(int id) {
  15. this.id = id;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. @Override
  21. public String toString() {
  22. return "StudentBean{" +
  23. "id=" + id +
  24. ", name='" + name + '\'' +
  25. ", major='" + major + '\'' +
  26. '}';
  27. }
  28. public StudentBean(int id, String name, String major) {
  29. this.id = id;
  30. this.name = name;
  31. this.major = major;
  32. }
  33. public void setName(String name) {
  34. this.name = name;
  35. }
  36. public String getMajor() {
  37. return major;
  38. }
  39. public void setMajor(String major) {
  40. this.major = major;
  41. }
  42. }

这里的@Repository(value = “studentBean”)的作用就相当于之前的@Component
(@Repository和@Component的作用相同,@Component多用于业务逻辑层,@Repository多用于持久层),
value就相当于之前bean中的id属性,不写的话默认为类名(首字母小写)。
同时也要开启资源扫描,代码如下:

  1. package config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.Import;
  6. import java.util.ArrayList;
  7. @Configuration
  8. @ComponentScan(basePackages = "com.bean")
  9. public class SpringConfig {
  10. public SpringConfig() {
  11. System.out.println("容器创建成功");
  12. }
  13. }

在@Configuration下面加上@ComponentScan(basePackages = “com.bean”)
这里的@ComponentScan(basePackages = “com.bean”)的作用相当于如下代码:

  1. //开启注解的扫描(此段代码在applicationContext配置文件中存放)
  2. <context:component-scan base-package="com.bean"/>

获取资源的方式同上面两种方法。

发表评论

表情:
评论列表 (有 0 条评论,324人围观)

还没有评论,来说两句吧...

相关阅读