Spring(1):搭建Spring3开发环境(maven+spring)

新建maven项目

Spring(1):搭建Spring3开发环境(maven+spring)_第1张图片

整个项目目录结构

Spring(1):搭建Spring3开发环境(maven+spring)_第2张图片

所需jar包

1.spring-core
2.spring-context
3.spring-jdbc
4.spring-beans
5.spring-web
6.spring-expression
7.spring-orm

可以手都能下载,也可以使用maven (我使用的是maven)

pom.xml


<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-coreartifactId>
    <version>3.1.2.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>3.1.2.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>3.1.2.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>3.1.2.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webartifactId>
    <version>3.1.2.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-expressionartifactId>
    <version>3.1.2.RELEASEversion>
dependency>
<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-ormartifactId>
    <version>3.1.2.RELEASEversion>
dependency>

创建spring.xml

新建spring.xml 放到src/main/resources下:
文件内存如下:


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
    
    <context:component-scan base-package="com.lt.service" />
beans>

代码

UserServiceI.java

package com.lt.service;

public interface UserServiceI {
    public void excute();
}

UserServiceImpl.java

package com.lt.service.impl;

import org.springframework.stereotype.Service;

import com.lt.service.UserServiceI;

//使用Spring提供的@Service注解将UserServiceImpl标注为一个Service
@Service("userService")
public class UserServiceImpl implements UserServiceI{

    public void excute() {
        System.out.println("succeed");
    }

}

编写测试类

pom.xml 添加junit 依赖


<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.10version>
    <scope>testscope>
dependency>

UserServiceTest.java

package com.lt.service.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lt.service.UserServiceI;

public class UserServiceTest {
    @Test
    public void test1(){
        // 通过Spring.xml 配置文件创建SpringIOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath:spring.xml"});
        // 获取bean对象
        UserServiceI userService = (UserServiceI) ctx.getBean("userService");
        //do test
        userService.excute();
    }

}

运行结果

Spring(1):搭建Spring3开发环境(maven+spring)_第3张图片

你可能感兴趣的:(spring)