idea 新建spring maven项目、ioc和依赖注入

文章目录

一、新建Spring-Maven项目

  • 在pom.xml文件中添加插件管理依赖

      <build>
          <plugins>
              <plugin>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <version>3.1</version>
                  <configuration>
                      <source>1.8</source>
                      <target>1.8</target>
                  </configuration>
              </plugin>
          </plugins>
      </build>
    
  • 在pom.xml文件中添加Spring-Context依赖

二、在Spring-context使用IOC和依赖注入

  • 在resource里面新建一个spring的xml文件
    • applicationContext.xml ,如图

    • 新建接口类

          package org.study;
      
          public interface ISomeService {
              void doSome();
          }
      
    • 新建接口实现类

          package org.study;
      
          public class SomeService implements  ISomeService{
              /**
               * 构造函数
               */
              public SomeService()
              {
                  System.out.println("SomeService 构造函数!");
              }
          
              /**
               *
               */
              @Override
              public void doSome() {
                  System.out.println("doSome 函数!");
              }
          } 
      
    • Main函数中执行

          package org.example;
      
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;
          import org.study.ISomeService; 
          
          public class Main {
          
              public static void main(String[] args) {
                  //加载配置文件
                  ApplicationContext context =   new ClassPathXmlApplicationContext("applicationContext.xml");
                  //第一种方式
                  ISomeService iSomeService = (ISomeService)context.getBean("SomeService");
                  iSomeService.doSome();
                  //第二种方式 要求:获取对应的实例,该实例必须实现对应的接口
                  ISomeService iSomeService1 =  context.getBean("SomeService",ISomeService.class);
                  iSomeService.doSome(); 
              }
          }
      
    • 在当前的服务中调用其他服务

      • 第一种方式

        • 使用setter的方式
          • 接口类

                  package org.study;
            
                  public interface ISomeService1 {
                      void doSome();
                  } 
            
          • 实现类

                  package org.study;
            
                  public class SomeService1 implements  ISomeService1{
                      private ISomeService iSomeService;
                  
                      public void setiSomeService(ISomeService iSomeService) {
                          this.iSomeService = iSomeService;
                      } 
                      /**
                       * 构造函数
                       */
                      public SomeService1()
                      {
                          System.out.println("SomeService1 构造函数!");
                      } 
                      /**
                       *
                       */
                      @Override
                      public void doSome() {
                  
                          iSomeService.doSome();
                  
                      }
                  } 
            
          • applicationContext.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
                                         http://www.springframework.org/schema/beans/spring-beans.xsd">
              
                <bean  id ="SomeService" class="org.study.SomeService"></bean>
              
              
                <bean id="SomeService1" class="org.study.SomeService1" >
                  <!--使用 setter-->
               <property  name="iSomeService" ref="SomeService"></property>  
                </bean>
              
              </beans>
              <!--
              name : 提供setter方法的属性名称
              ref:容器中已经有的bean名称
                -->
            
      • 第二种方式

        • 使用构造函数方式
          • 接口类

                  package org.study;
            
                  public interface ISomeService1 {
                      void doSome();
                  } 
            
          • 实现类

                  package org.study;
            
                  public class SomeService1 implements  ISomeService1{
                      private ISomeService iSomeService;
                   
                  
                   public  SomeService1 (ISomeService iSomeService){
                       this.iSomeService = iSomeService;
                   }
                      /**
                       * 构造函数
                       */
                      public SomeService1()
                      {
                          System.out.println("SomeService1 构造函数!");
                      }
                  
                      /**
                       *
                       */
                      @Override
                      public void doSome() {
                  
                          iSomeService.doSome();
                  
                      }
                  }
            
          • applicationContext.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
                                         http://www.springframework.org/schema/beans/spring-beans.xsd">
              
                <bean  id ="SomeService" class="org.study.SomeService"></bean> 
              
                <bean id="SomeService1" class="org.study.SomeService1" >
                   <constructor-arg name="iSomeService" ref="SomeService"></constructor-arg>
                </bean>
              
              </beans>
              <!--
              name :  属性名称
              ref:容器中已经有的bean名称
                -->
            
      • main函数执行

           ISomeService1 iSomeService1 = context.getBean("SomeService1",ISomeService1.class);
          iSomeService1.doSome();
        
相关推荐
飞翔的佩奇1 小时前
Java项目: 基于SpringBoot+mybatis+maven校园资料分享平台(含源码+数据库+答辩PPT+毕业论文)
java·spring boot·spring·毕业设计·maven·mybatis·校园资料分享平台
终末圆3 小时前
MyBatis 增删改查【后端 17】
java·服务器·数据库·b树·mysql·spring·mybatis
计算机学姐4 小时前
基于微信小程序的高校实验室管理系统的设计与实现
java·vue.js·spring boot·mysql·微信小程序·小程序·intellij-idea
Yudiannann6 小时前
【苍穹外卖】总结
java·开发语言·spring
越过难题7 小时前
springBoot整合easyexcel实现导入、导出功能
java·spring boot·spring
计算机毕设残哥12 小时前
【动漫资源管理系统】Java SpringBoot助力,搭建一个高清动漫在线观看网站
java·开发语言·spring boot·spring·计算机毕设·计算机毕业设计
AskHarries15 小时前
Spring Boot集成Akka Cluster实现在分布式节点中执行任务
java·spring boot·maven·akka
荆州克莱15 小时前
书生大模型全链路开源体系
spring boot·spring·spring cloud·css3·技术
冰淇淋烤布蕾15 小时前
git 你要如何打开这个文件
git·vscode·intellij-idea
熙客16 小时前
Spring IOC的应用
java·后端·spring