需求:了解快速入门步骤,并完成入门测试
步骤:
1.导入坐标
在创建的Maven项目中,pom.xml文件中导入Spring坐标
代码:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.19</version> </dependency> </dependencies>
示例图:
2.创建Bean
创建接口以及实现类,再重写实现类用于测试
接口代码:
public interface UserDao { public void save(); }
实现类代码:
package Wss.dao.impl; import Wss.dao.UserDao; public class UserDaoImpl implements UserDao { @Override public void save() { System.out.println("save running..."); } }
3.创建applicationContext.xml配置文件
创建xml配置文件,配置Bean
<bean id="userDao" class="Wss.dao.impl.UserDaoImpl"> </bean>
4.创建ApplicationContext对象getBean
package Wss.Demo; import Wss.dao.UserDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserDemo { public static void main(String[] args) { ApplicationContext app =new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) app.getBean("userDao"); userDao.save(); } }
运行结果: