通过实例解析Spring Ioc项目实现过程

作者:yaominghui 时间:2023-11-24 10:12:33 

0. Ioc

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html

主要是实现一个控制反转,耦合性大大降低。

1. 建maven项目

建立一个空的maven项目,然后pom.xml添加spring-context的依赖:


   <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>5.2.7.RELEASE</version>
   </dependency>

2. 创建pojo java对象


package com.aca;

public class Hello {
 private String str;

public void setStr(String str) {
   this.str = str;
 }

public String getStr() {
   return str;
 }

public Hello(String str){
   this.str = str;
 }

@Override
 public String toString() {
   return "Hello{" +
       "str='" + str + '\'' +
       '}';
 }
}

3. 创建bean xml配置元数据

配置文件放在resources下。
这里以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/beans
   https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="Hello" class="com.aca.Hello">
   <constructor-arg type="java.lang.String" value="fffff"/>
 </bean>

</beans>

如果有多个resource或者目录不一致,就需要import一下:


<beans>
 <import resource="services.xml"/>
 <import resource="resources/messageSource.xml"/>
 <import resource="/resources/themeSource.xml"/>

<bean id="bean1" class="..."/>
 <bean id="bean2" class="..."/>
</beans>

里面可以调用构造函数来初始化一下bean。

4.创建spring 上下文

这里用ClassPathXmlApplicationContext 方法。


  ApplicationContext context = new ClassPathXmlApplicationContext("hbean.xml");

// retrieve configured instance
   Hello hello = context.getBean("Hello", Hello.class);
//    hello.setStr("abc");
   System.out.println(hello);

直接可以用这个bean,由xml注入。

5. Error:java: 错误: 不支持发行版本 5

将file- project structure 中的jdk版本选成跟本地一直,比如我这个jdk14

通过实例解析Spring Ioc项目实现过程

将build -> java complier中的两个版本选择成跟本地一致,这里是14

通过实例解析Spring Ioc项目实现过程

这两步做好以后不会报错,maven里面不需要选择版本。

6. 如果报xml的问题

xml declaration should precede all document

那是因为xml 第一行是空格了,必须<?xml 做为第一行。

来源:https://www.cnblogs.com/gqdw/p/13130949.html

标签:spring,Ioc,项目
0
投稿

猜你喜欢

  • Android自定义控件ViewGroup实现标签云(四)

    2021-11-30 14:58:36
  • Java实现高校教务系统

    2022-05-16 04:24:17
  • 如何实现Spring Event(异步事件)

    2023-08-23 05:06:47
  • spring中向一个单例bean中注入非单例bean的方法详解

    2022-07-19 13:14:18
  • java将一个目录下的所有文件复制n次

    2023-03-04 00:13:12
  • springboot为异步任务规划自定义线程池的实现

    2022-12-05 01:13:02
  • SpringCloud使用Zookeeper作为配置中心的示例

    2023-08-03 12:30:51
  • Java中Date和Calendar常用方法

    2023-11-12 07:35:32
  • Kotlin基础学习之循环和异常

    2023-05-26 00:42:12
  • Spring Boot教程之利用ActiveMQ实现延迟消息

    2023-11-23 18:25:09
  • C#文件断点续传实现方法

    2023-09-07 18:35:05
  • IntelliJ IDEA基于SpringBoot如何搭建SSM开发环境的步骤详解

    2022-11-24 12:10:39
  • Spring Boot + Mybatis 实现动态数据源案例分析

    2023-05-15 12:13:47
  • 详解Java对象的强、软、弱和虚引用+ReferenceQueue

    2021-11-30 16:23:01
  • Android仿微信选择图片和拍照功能

    2023-08-18 05:22:50
  • android计算器代码示例分享

    2023-10-14 14:06:58
  • Netty分布式FastThreadLocal的set方法实现逻辑剖析

    2021-08-22 04:51:54
  • 详解Java中的泛型

    2021-12-02 14:57:34
  • Java多线程编程小实例模拟停车场系统

    2022-07-07 06:36:29
  • Java中的字符串常量池详细介绍

    2023-03-08 09:16:41
  • asp之家 软件编程 m.aspxhome.com