当前位置: 首页 > news >正文

后端主流框架--Spring02

前言:上篇关于Spring的文章介绍了一些Spring的基本知识,此篇文章主要分享一下如何配置Spring环境,如何注入等。

Spring项目构建

导入Spring相关JAR包

<dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.4.RELEASE</version>
</dependency>

准备Spring配置文件

  1. 在classpath的根目录下新建一个applicationContext.xml配置文件,文件名可以自定义,但是通常使用applicationContext这个名字;

  2. 添加文档声明和约束(这个东西不需要记忆):

    1. 可以参考文档,中英文文档都可以;

      1. spring-framework-4.1.2.RELEASE\docs\spring-framework-reference\pdf

    2. 可以参考资源中的资料;

    3. 可以百度spring的配置文件;

    4. 也可以直接拿以下内容去修改

<?xml version="1.0" encoding="UTF-8"?>
<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="..." class="..."><!-- collaborators and configuration for this bean go here --></bean>
</beans>

编写一个类

public class MyBean {public void hello(){System.out.println("hello spring...");}
}

将编写的类交给Spring容器管理

  1. Spring是一个容器,我们需要把我们的类交给Spring去管理。 因为,我们的测试是创建一个普通的类,然后再通过Spring帮我们把这个类的对象创建出来就算是成功了;

  2. 在配置文件中将这个Java类交给Spring管理。在applicationContext.xml中配置

    <beans ...><bean id="myBean" class="org.com.Spring._01_hello.MyBean"></bean>
    </beans>
  3. 元素和属性讲解:

    bean元素:表示对象配置或注册标签;

    id属性:这个bean对象在Spring容器中的唯一标识,也可以使用name,常用id(唯一特性),获取这个对象的时候就可以通过这个表示来获取;

    class属性:对应对象所属类的完全限定名。注意这里可以是JDK自带的类,也可以是自己新建的类;

Spring容器实例化

  1. Spring容器对象有两种:BeanFactory和ApplicationContext;

  2. ApplicationContext继承自BeanFactory接口,拥有更多的企业级方法,推荐使用该类型;

BeanFactory

BeanFactory是一个接口,可以通过其实现类XmlBeanFactory获取其实例。接口中有一个getBean()方法可以获取Spring容器中配置或注册的Bean对象;

@Test
public void testHelloSpring() throws Exception {/***我们第一步是要启动框架,而启动框架则需要拿到Spring的核心对象	*///第一步:读取资源文件Resource resource = new ClassPathResource("applicationContext.xml");//第二步:拿到核心对象 BeanFactoryBeanFactory factory = new XmlBeanFactory(resource);
}

ApplicationContext

ApplicationContext的中文意思是"应用程序上下文",它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持、资源访问(如URL和文件)、事件传播等方面进行了良好的支持,被推荐为JavaEE应用之首选,可应用在Java APP与Java Web中;

加载工程classpath下的配置文件实例化
String conf = "applicationContext.xml";
ApplicationContext factory = new ClassPathXmlApplicationContext(conf);

获取对象方式

方式一:通过id直接拿到相应的Bean对象

//通过[xml]{.ul}中配置的id拿到对象

MyBean bean = (MyBean)factory.getBean("myBean");

System.out.println(bean);

方式二:通过id与对象的Class对象拿到Bean对象(推荐使用)

//通过id与对象的class拿到Bean对象

MyBean bean = factory.getBean("myBean",MyBean.class);

System.out.println(bean);

ApplicationContext与BeanFactory的区别

联系

ApplicationContext是BeanFactory的子类,拥有更多的功能与方法;

区别

ApplicationContext默认是在读取配置文件的时候就会根据配置创建Bean对象(迫切加载)。而BeanFactory是在使用的时候才进行对象的创建(懒加载/延迟加载)

Spring管理bean的方式

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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"
>    <bean id="myBean" class="com.ss._04di._01xml.MyBean"></bean><bean id="otherBean" class="com.ss._04di._01xml.OtherBean"></bean>
</beans>

注解

配置扫描注解

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd
"
><!--扫描组件:通过指定包,就是找到本包或者子孙包下面加了注解Component及@Controller等注解的类,把它交给spring管理就相当于我们xml当中一个一个配置--><context:component-scan base-package="com.ss._04di._02annotation"></context:component-scan>
</beans>

加注解

@Component @Controller @ Service @Repository等注解,后面三个是前一个子注解。其实我们任意用一个都可以,但是为了代码可读性,最好分门别类的使用 。

Controller就用 @Controller 注解

Service就用@Service注解

dao/mapper用@Repository

Spring依赖注入

  1. IoC是一种思想,它的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的;

  2. Spring中的对象都是由Spring进行统一管理,但是在对象中还存在属性,这些属性实际上引用的也是别的对象,那么这些对象也是由Spring来管理的;

  3. 在实际使用时,我们需要给Spring中对象的属性字段赋值,这称为依赖注入DI(Dependency Injection);

  4. 依赖注入又分为xml注入和注解注入;

XML注入

Bean定义:

public class MyBean{private OtherBean otherBean;public void hello(){otherBean.hello();}public void setOtherBean(OtherBean otherbean){this.OtherBean = OtherBean
}
}public class OtherBean{public void hello(){System.out.println("otherbean hello");}
}

xml定义

//xml配置:
<bean id="otherBean" class="org.com.bean.OtherBean"></bean>
<bean id="myBean" class="org.com.bean.MyBean"><property name="otherBean" ref="otherBean"></property>
</bean

测试

//测试:main方法测试
//加载工程classpath下的配置文件实例化
String conf = "applicationContext.xml";
ApplicationContext factory = new ClassPathXmlApplicationContext(conf);

注解注入

使用@Autowired

@Component
public class ServiceBean {@Autowiredprivate DaoBean1 daoBean;public void sayHello(){System.out.println("hello spring!!!!");daoBean.hello();}
}
@Component
public class DaoBean1 {public void hello(){System.out.println("hello DaoBean1!!!!!");}
}
@Component
public class DaoBean {public void hello(){System.out.println("hello DaoBean!!!!!");}
}

使用@Resource

public class MyBean{@Resource //默认按照名字匹配【名字对了,类型也必须一致】,然后按照类型匹配//@Resource(name="otherBean1")//指定Bean的名称private OtherBean otherBean;public void hello(){otherBean.hello();}
}public class OtherBean{public void hello(){System.out.println("otherbean hello");}
}

写在最后

Spring的依赖注入,翻转控制,面向切面编程是其最主要的特点与优势,后续博主也会继续分享相关的文章。博主小,中,大厂均有面试经历,每日分享JAVA全栈知识,希望能与大家共同进步。

http://www.lryc.cn/news/373531.html

相关文章:

  • [数据集][目标检测]减速带检测数据集VOC+YOLO格式5400张1类别
  • 分析Linux操作指令及使用场景与频率分析 持续更新
  • Redis 字符串(String)
  • 第一篇:容器化的未来:从Docker的革命到云原生架构
  • 【2024最新华为OD-C/D卷试题汇总】[支持在线评测] URL拼接(100分) - 三语言AC题解(Python/Java/Cpp)
  • 反射,枚举以及lambda表达式
  • DNS域名解析----分离解析、多域名解析、父域与子域
  • Spring底层架构核心概念解析
  • C++ 44 之 指针运算符的重载
  • onlyoffice在线预览加载优化
  • 依赖自动装配
  • mysql和redis的双写一致性问题
  • Qwen2——阿里巴巴最新的多语言模型挑战 Llama 3 等 SOTA
  • 等级考试3-2021年3月题
  • Web前端开发PPT:深入探索与实战应用
  • liunx常见指令
  • vscode设置成中文界面
  • python命名空间详解
  • 【日常记录】【vue】vite-plugin-inspect 插件的使用
  • mini web框架示例
  • 基于C#开发web网页管理系统模板流程-主界面统计功能完善
  • chromedriver114以后版本下载地址汇总chromedriver所有版本下载地址汇总国内源下载
  • x86计算机的启动初期流程 Linux 启动流程
  • P450Rdb: CYP450数据库--地表最强系列--文献精读24
  • ubuntu 22.04下载和安装
  • Fegin如何传参form-data文件
  • 解决 Visual C++ 17.5 __cplusplus 始终为 199711L 的问题
  • Mac M3 Pro安装Hadoop-3.3.6
  • mac下Xcode在iphone真机上测试运行iOS软件
  • Mysql(一):深入理解Mysql索引底层数据结构与算法