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();
相关推荐
HanhahnaH2 分钟前
Spring集合注入Bean
java·spring
.生产的驴2 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
时间之城2 小时前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
程序员总部3 小时前
如何在IDEA中高效使用Test注解进行单元测试?
java·单元测试·intellij-idea
方圆想当图灵3 小时前
深入理解 AOP:使用 AspectJ 实现对 Maven 依赖中 Jar 包类的织入
后端·maven
李菠菜3 小时前
浅谈Maven依赖传递中的optional和provided
后端·maven
m0Java门徒4 小时前
面向对象编程核心:封装、继承、多态与 static 关键字深度解析
java·运维·开发语言·intellij-idea·idea
ApeAssistant4 小时前
Spring + 设计模式 (十四) 行为型 - 观察者模式
spring·设计模式
代码小侦探5 小时前
Java中以Maven方式引入Oracle JDBC Driver依赖的详解
java·oracle·maven
ApeAssistant5 小时前
Spring + 设计模式 (十三) 行为型 - 策略模式
spring·设计模式