标签:beans repos text 获得 pos encoding system 编写 配置文件
1.导入 Spring 开发的基本包坐标
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies>
2.编写 Dao 接口和实现类
public class userDaoImpl implements userDao { public userDaoImpl() { System.out.println("无参"); } public void init(){ System.out.println("init"); } public void destroy(){ System.out.println("destroy"); } @Override public void save() { System.out.println("save running..."); } }
3.创建 Spring 核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userDao" class="ioc1.it.dao.userDaoImpl"></bean> </beans>
4.在 Spring 配置文件中配置 UserDaoImpl
4.使用 Spring 的 API 获得 Bean 实例
public class test { @Test public void test(){ ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); userDao userDao1 =(userDao) app.getBean("userDao");//getBean是配置文件中的id System.out.println(userDao1); } }
标签:beans repos text 获得 pos encoding system 编写 配置文件
原文地址:https://www.cnblogs.com/jiangzishun/p/12833417.html