IDEA下——Spring入门程序

本文详细介绍如何使用Maven创建Spring项目,包括项目结构搭建、依赖管理、接口及实现类的创建、Spring容器配置及Bean注册,最后通过测试类验证Spring容器的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


  1. 创建一个Maven的项目,我的项目结构如下:
    我的目录
  2. 在pom文件里写下需要导入的依赖:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
       <modelVersion>4.0.0</modelVersion>
       <groupId>com.abc</groupId>
       <artifactId>01-first(Spring)</artifactId>
       <version>1.0-SNAPSHOT</version>
       <build>
           <plugins>
               <plugin>
                   <groupId>org.apache.maven.plugins</groupId>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <configuration>
                       <source>6</source>
                      <target>6</target>
                   </configuration>
               </plugin>
           </plugins>
       </build>
       <properties>
           <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
           <maven.compiler.source>10</maven.compiler.source>
           <maven.compiler.target>10</maven.compiler.target>
           <spring.version>5.1.0.RELEASE</spring.version>
       </properties>
       <dependencies>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-beans</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-core</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-context</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-expression</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <dependency>
               <groupId>org.springframework</groupId>
               <artifactId>spring-jcl</artifactId>
               <version>${spring.version}</version>
           </dependency>
           <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <version>4.12</version>
               <scope>test</scope>
           </dependency>
       </dependencies>
       <build>
           <plugins>
               <plugin>
                   <artifactId>maven-compiler-plugin</artifactId>
                   <configuration>
                       <source>1.10</source>
                       <target>1.10</target>
                   </configuration>
               </plugin>
           </plugins>
       </build>
     </project>
  1. 在Java文件夹下新建package,在package包下新建接口及其实现类
    接口:
    public interface SomeService { void doSome(); }
    实现类:

        public class SomeServiceImpl implements SomeService {
        public SomeServiceImpl() {
            System.out.println("创建SomeServiceImpl对象");
        }
    
        @Override
        public void doSome() {
            System.out.println("执行SomeServiceImpl里的doSome()方法");
        }
        }
  2. 在resources目录下新建applicationContext.xml文件
  • 我们需要在spring容器的配置文件中进行注册该Bean
  • spring使用的配置文件为xml文件,当然需要引入约束文件,一般将spring的配置文件命名为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">    
         <!--注册Servcie其造价于如下代码:   SomeServiceImpl someService = new SomeServiceImpl();
                    其底层是通过反射机制创建的someService对象
            Object someService = Class.forName("com.abc.service.SomeServiceImpl").newInstance();-->
                    <bean id="someService" class="com.abc.service.SomeServiceImpl"/>
     </beans>
  • spring的根元素是benas显然是注册Bean,子标签是Bean
  • 注册:<bean id="someService" class="com.abc.service.SomeServiceImpl"/>
  • id属性为了唯一确定一个对象,class属性里边应写类全名
  1. 注册完毕后我们要在测试类中获取spring容器,而获取Spring容器有两种方式。
    package com.abc.service;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;

    public class SomeServiceImplTTest {
         // spring容器获取的两种方式
         @Test
         public void test01(){
             //在类路径下加载Spring配置文件
             ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
             //在当前目录的根下加载该配置文件,路径应为文件实际存放的目录
                ApplicationContext ac2 = new FileSystemXmlApplicationContext
                       ("D:\\IDEA-workspace\\01-first(Spring)\\src\\main\\resources\\applicationContext.xml");
                System.out.println(ac2);
          }    
        @Test
        public void test02() {
             // 加载Spring配置文件,创建Spring容器对象
             ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
             //调用spring容器的getBean方法获取carImpl,方法参数为bean的id
             SomeService service = (SomeService) ac.getBean("SomeService");
             service.doSome();
            }
        }

Spring入门程序到此就结束了

转载于:https://www.cnblogs.com/EWEADN/p/10017585.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值