第一个Spring程序
1.开发spring程序的步骤
- 导入 Spring 开发的基本包坐标
- 编写 Dao 接口和实现类
- 创建 Spring 核心配置文件
- 在 Spring 配置文件中配置 UserDaoImpl
- 使用 Spring 的 API 获得 Bean 实例
2.第一个spring程序
1.创建maven项目,在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>org.example</groupId>
<artifactId>Case1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--spring-context坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--log4j坐标-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
</project>
2.在resources/目录下创建slf4j.properties文件,并且设置slf4j的日志输出格式,打印级别为ERROR
#开放一个info级别的日志记录器,名字叫做stdout
log4j.rootCategory=DEBUG,stdout
#日志记录器要在控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#org.springframework包底下输出的是ERROR级别
log4j.logger.org.springframework=ERROR
3.在dao包创建UserDao接口和UserDaoImpl接口实现类,在service包下创建UserService类
package com.itspring.a.a1.dao;
public interface UserDao {
void save();
}
-----------------------------------
package com.itspring.a.a1.dao;
public class UserDaoImpl implements UserDao{
public void save(){
System.out.println("save...");
}
}
----------------------------------
package com.itspring.a.a1.service;
import com.itspring.a.a1.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserService {
private UserDao userDao;
public void execute() {
ApplicationContext ac =
new ClassPathXmlApplicationContext(
"applicationContext.xml");
userDao = (UserDao) ac.getBean("userDao");
userDao.save();
}
}
3.在resources目录下创建applicationContext.xml的spring配置文件,并且添加一个Bean
<?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="userDao" class="com.itspring.a.a1.dao.UserDaoImpl"></bean>
</beans>
4.在test包下创建测试类
package com.itspring.a.a1.test;
import com.itspring.a.a1.service.UserService;
import org.junit.Test;
public class Test_a1 {
@Test
public void test1() {
UserService service = new UserService();
service.execute();
}
}
-----------------------------
$控制台输出:
十月 08, 2021 9:31:49 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3419866c: startup date [Fri Oct 08 21:31:49 CST 2021]; root of context hierarchy
十月 08, 2021 9:31:49 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
save...
Process finished with exit code 0