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

来源:互联网 发布:字符大小写转换 c语言 编辑:程序博客网 时间:2024/06/02 02:28

新建maven项目

这里写图片描述

整个项目目录结构

这里写图片描述

所需jar包

1.spring-core2.spring-context3.spring-jdbc4.spring-beans5.spring-web6.spring-expression7.spring-orm

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

pom.xml

<!-- spring3 --><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-core</artifactId>    <version>3.1.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-context</artifactId>    <version>3.1.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-jdbc</artifactId>    <version>3.1.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-beans</artifactId>    <version>3.1.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-web</artifactId>    <version>3.1.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-expression</artifactId>    <version>3.1.2.RELEASE</version></dependency><dependency>    <groupId>org.springframework</groupId>    <artifactId>spring-orm</artifactId>    <version>3.1.2.RELEASE</version></dependency>

创建spring.xml

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

<?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: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">    <!-- 自动扫描dao和service包(自动注入) -->    <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 依赖

<!-- Junit --><dependency>    <groupId>junit</groupId>    <artifactId>junit</artifactId>    <version>4.10</version>    <scope>test</scope></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();    }}

运行结果

这里写图片描述

原创粉丝点击