spring实现ioc的步骤

1、新建maven工程,在maven中引入spring-context依赖

2、两种方式基于XML和注解的方式(可分为)

3、使用XML配置文件

  1、新建一个类(Data注解需要引入Lombok依赖,如果不引入就需要自己添加Get和Set方法)

 

  2、在resource下新建一个XXXX.XML文件并添加头部文件 

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">

  3、在XML中配置

  4、在测试类中测试

  5、结果

 二、注解2种(配置类和扫包加注解)

 1、新建一个配置类添加注解

 

   2、读取配置类的方式

第二种注解

  1先在原始类上添加component注解

  2、使用扫包的方式加载

 

 

 

 

 

 

 

 

 

  

posted @ 2023-05-07 10:10  free922_959  阅读(46)  评论(0编辑  收藏  举报