码迷,mamicode.com
首页 > 编程语言 > 详细

Spring程序开发步骤

时间:2020-05-05 23:32:48      阅读:73      评论:0      收藏:0      [点我收藏+]

标签: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);
}
}

 

Spring程序开发步骤

标签:beans   repos   text   获得   pos   encoding   system   编写   配置文件   

原文地址:https://www.cnblogs.com/jiangzishun/p/12833417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!