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(); }}
运行结果
阅读全文
0 0
- Spring(1):搭建Spring3开发环境(maven+spring)
- 利用Maven搭建Spring的开发环境
- 利用Maven搭建Spring开发环境
- 利用Maven搭建Spring开发环境
- eclipse+maven+spring开发环境搭建
- maven 搭建spring环境
- Maven+Spring环境搭建
- Maven搭建SSM(SpringMVC + Spring + Mybaits)开发环境 (上)
- Maven搭建SSM(SpringMVC + Spring + Mybaits)开发环境 (下)
- 使用Maven搭建Spring3开发环境
- 1 搭建Spring开发环境
- 搭建Spring开发环境
- 搭建spring开发环境
- 搭建Spring开发环境
- Spring开发环境搭建
- 使用Maven搭建Spring MVC+Hibernate开发环境
- Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建
- Spring MVC+Maven+Freemarker+Mybatis开发环境搭建
- Django数据库一对一数据关系
- node-mysql
- JavaSE基础入门
- 数字三角形 动态规划
- 关于保障移动电话用户资费方案选择权的通知 信部清〔2006〕630号
- Spring(1):搭建Spring3开发环境(maven+spring)
- 国内最火的 HTML、CSS、JavaScript 开源项目 Top 榜,你知多少?
- MyEclipse 2015启动tomcat服务器时, jar包无法发布到tomcat lib目录下的问题
- python中多进程编程详解
- 黑马程序员总结1-python基础
- 网狐荣耀版手机端登录提示“网络错误:5”解决办法
- webpack安装以及使用
- node-mongodb
- leetcode 3. Longest Substring Without Repeating Characters