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属性里边应写类全名
5. 注册完毕后我们要在测试类中获取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入门程序到此就结束了

最新文章

  1. Linux SendMail发送邮件失败诊断案例(二)
  2. 自己积累的一些Emgu CV代码(主要有图片格式转换,图片裁剪,图片翻转,图片旋转和图片平移等功能)
  3. matlab播放音乐
  4. cocos2d-x之 利用富文本控件解析xhml标签(文字标签,图片标签,换行标签,标签属性)
  5. 304 CORS
  6. 关于CPU亲和性的测试
  7. hdu2712(贪心)
  8. CodeForces 660A Co-prime Array
  9. web前端经典面试题大全及答案
  10. 将常用的Android adb shell 命令行封装为C#静态函数
  11. 爬虫基础--IO多路复用单线程异步非阻塞
  12. 汇编-13.0-int指令
  13. mysql 清空表——truncate 与delete的区别
  14. 共享锁(S锁)和排它锁(X锁)
  15. 安装Tomcat配置环境变量
  16. java并发编程(7)构建自定义同步工具及条件队列
  17. C之面向对象编程20170707
  18. 如何将qlv格式的腾讯视频转换为mp4格式
  19. selenium.common.exceptions.WebDriverException: Message: &#39;geckodriver&#39; executable needs to be in PATH. 错误处理方法
  20. CDH4.5.0下安装lzo

热门文章

  1. hdoj-1212-Big Number【大数取余&amp;amp;简单题】
  2. 我的Android进阶之旅------&amp;gt;怎样将Activity变为半透明的对话框?
  3. light oj 1094 Farthest Nodes in a Tree(树的直径模板)
  4. Linux下PHP开启Oracle支持(oci8)
  5. 求一个数组的最大k个数(java)
  6. django admin显示多对多字段
  7. XML与Plist文件转换
  8. vuejs keep-alive
  9. 使用物化视图解决GoldenGate不能使用中文表名问题
  10. [JSOI2018]潜入行动 树形DP_复杂计数