SSM1.1【Spring:Spring概述和快速入门】

Spring概述

 

 

 

 

 

 

 

 

 

Spring快速入门

 

 1、在pom.xml中导入Spring开发的基本包坐标

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.haifei</groupId>
 8     <artifactId>SSM1_spring_ioc</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <!--导入依赖-->
12     <dependencies>
13         <!--导入 Spring 开发的基本包坐标-->
14         <dependency>
15             <groupId>org.springframework</groupId>
16             <artifactId>spring-context</artifactId>
17             <version>5.0.5.RELEASE</version>
18         </dependency>
19         <dependency>
20             <groupId>junit</groupId>
21             <artifactId>junit</artifactId>
22             <version>4.11</version>
23         </dependency>
24     </dependencies>
25 
26 
27     <build>
28         <plugins>
29             <!--编译插件,防止控制台Messages中Warning提示:源值1.5过时-->
30             <plugin>
31                 <groupId>org.apache.maven.plugins</groupId>
32                 <artifactId>maven-compiler-plugin</artifactId>
33                 <version>3.1</version>
34                 <configuration>
35                     <target>1.8</target>
36                     <source>1.8</source>
37                 </configuration>
38             </plugin>
39         </plugins>
40     </build>
41 
42 </project>

2、编写Dao接口及其实现类

1 package com.haifei.dao;
2 
3 public interface UserDao {
4 
5     public void save();
6 }
 1 package com.haifei.dao.impl;
 2 
 3 import com.haifei.dao.UserDao;
 4 
 5 public class UserDaoImpl implements UserDao {
 6 
 7     public void save() {
 8         System.out.println("save running");
 9     }
10 }

3、创建Spring核心配置文件applicationContext.xml

 

 4、在Spring配置文件中配置UserDaoImpl

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
5 
6     <bean id="userDao" class="com.haifei.dao.impl.UserDaoImpl"></bean>
7 
8 </beans>

5、使用Spring的API获得Bean实例

 1 package com.haifei.demo;
 2 
 3 import com.haifei.dao.UserDao;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 public class UserDaoDemo {
 8 
 9     public static void main(String[] args) {
10         ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
11 //        Object userDao = app.getBean("userDao");
12         UserDao userDao = (UserDao)app.getBean("userDao");
13         userDao.save();
14     }
15 }

posted @ 2021-07-14 21:16  yub4by  阅读(51)  评论(0编辑  收藏  举报