[学习记录]使用Maven创建spring程序

[spring学习]使用maven创建spring项目

首先创建好maven项目

创建好项目之后,项目结构为这样:

配置pom.xml

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.0.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>

    </dependencies>

此时右键项目,创建spring配置文件

### 创建Java文件

目录结构如下:

HelloWorldDao.java
package com.spring.dao;

public interface HelloWorldDao {
    public void sayHello();
}
HelloWorldDaoImpl.java
package com.spring.dao.impl;

import com.spring.dao.HelloWorldDao;

public class HelloWorldDaoImpl implements HelloWorldDao {
    public void sayHello() {
        System.out.println("Spring say Hello World!");
    }
}
HelloWorldService.java
package com.spring.service;

import com.spring.dao.HelloWorldDao;

public class HelloWorldService {
    HelloWorldDao helloWorldDao;

    public HelloWorldDao getHelloWorldDao() {
        return helloWorldDao;
    }

    public void setHelloWorldDao(HelloWorldDao helloWorldDao) {
        this.helloWorldDao = helloWorldDao;
    }
}
spring-config.xml
    <bean id="HelloWorldDao" class="com.spring.dao.impl.HelloWorldDaoImpl" />
    <bean id="HelloWorldService" class="com.spring.service.HelloWorldService" >
        <property name="helloWorldDao" ref="HelloWorldDao"/>
    </bean>
测试Spring
package com.spring.app;

import com.spring.dao.HelloWorldDao;
import com.spring.service.HelloWorldService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
    @Test
    public void sayHello(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring-conf.xml");
        HelloWorldService service = (HelloWorldService) ac.getBean("HelloWorldService");
        HelloWorldDao dao = service.getHelloWorldDao();
        dao.sayHello();
    }
}

运行结果:

八月 13, 2020 1:26:37 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4ae82894: startup date [Thu Aug 13 01:26:37 CST 2020]; root of context hierarchy
八月 13, 2020 1:26:37 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-conf.xml]
Spring say Hello World!
Disconnected from the target VM, address: '127.0.0.1:65062', transport: 'socket'

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值