Chapter1、搭建与测试spring的环境
1、 将必要的jar包导入到新建的project中(可以去官网下载,也可以google或者百度)至少必须使用的有spring.jar 和commons-logging.jar
2、 在calsspath路径下创建配置文件beans.xml(模板到处有)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="" class=""></bean>
</beans>
3、 实例化spring容器:
实例化Spring容器的两种方式:
(1)在类路径下寻找配置文件来实例化容器
ApplicationContext ctx = new ClassPathXmlApplicationXContext(new String[]{“beans.xml”})
(2)在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx = new FileSystemXmlApplication(new String[]{“d:\\beans.xml”});
Spring 的配置文件可以指定多个,可以通过String数组传入,也可以是一个String
public class Test {
public static void main(String[] args) {
//IOC容器实例化
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
}
}
运行测试,结果如下:
Spring环境搭建成功!
4、创建业务bean接口 :
package com.wxy.service;
public interface PeopleService {
public abstract void save();
}
5、实现业务bean接口:
package com.wxy.service.impl;
import com.wxy.service.PeopleService;
public class PeopleServiceBean implements PeopleService {
/* (non-Javadoc)
* @see com.wxy.service.impl.PeopleService#save()
*/
public void save() {
System.out.println("--> the method is called save()!");
}
}
6、 在beans.xml中配置业务bean,将bean交给spring容器管理,spring创建和维护该bean,用户使用时,只需要获取就可以了,不用自己创建,实现IoC
依赖倒置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="peopleService" class="com.wxy.service.impl.PeopleServiceBean"></bean>
</beans>
7、 使用业务bean:
public class Test {
public static void main(String[] args) {
//IOC容器实例化
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
//获取业务bean
PeopleServiceBean peopleService = (PeopleServiceBean) ctx.getBean("peopleService");
peopleService.save();
}
}
调用结果:
- 大小: 34.9 KB
- 大小: 63.6 KB
分享到:
相关推荐
这个是搭建与测试Spring的开发环境和编码剖析Spring 管理Bean的原理的代码,适合刚接触spring的人学习,如何搭建spring开发环境,以及了解spring是如何管理bean的。这个是由传智播客的老师讲的。
本文章讲解了搭建与测试spring3开发环境的整个流程。可以供大家参考。
简单搭建一个spring+springMVC+Mybatis的项目。不完整,只是练习配置!
采用最新Spring 4.3.2 和mybatis最新jar包,jdk1.8,eclipse javaEE搭建
Struts2+Spring+hibernate中对action的单元测试环境搭建[总结].pdf
①部署Spring Boot开发环境 ②熟悉Idea工具的使用 ③创建Spring Boot程序 ④单元测试 ⑤配置热部署
基于SpringCloud、SpringSecurity、Oauth2、Nacos打造的开放式微服务平台. 利于企业微服务开发、更安全、更便捷的对接APP或前端应用,SpringCloud环境搭建及配置,个人已经搭建测试成功,分享给搭建
包含Spring+mybatis搭建文档,和spring3.06+mybatis3.06jar包和测试列子
Maven搭建spring mvc环境,应用spring3.2.2 jar包, 代码中包含有测试例子、配置文件等,非常好的一个实例。 希望大家喜欢。
Spring环境搭建、Spring中Bean的配置(Bean注入的2种方式,注入细节(字面值、引用其他bean、内部bean、集合属性、util scheme 定义集合、p命名空间))
从完整包中 抽出最少的包然后 编写一个增删改功能进行框架测试
NULL 博文链接:https://xrb2008.iteye.com/blog/650632
Spring + SpringMVC +Mybats环境搭建,有兴趣专研企业javaWeb的最新技术;希望能够帮助到你。不再是struts1.2或struts2来控制页面转发,也不是用Hibernate来做持久化;谢谢!
02_搭建与测试Spring的开发环境 03_编码剖析Spring管理Bean的原理 04_Spring的三种实例化Bean的方式 05_配置Spring管理的bean的作用域 06_Spring管理的Bean的生命周期 07_编码剖析Spring依赖注入的原理 08_...
基于struts,spring,hibernate,oracle环境下jquery easyui环境搭建。实现了使用jquery easyui中的datagrid显示用户列表。测试网址: http://localhost:9090/jquery-easyui-1.2.6BasedonStruts/layout.jsp
Spring cloud 实战电商网站项目( 模块) 目的: 综合应用 spring cloud进行微服务架构开发。 开发环境 操作系统 : windows Java环境 : JDK1.8(不能使用高版本) 开发工具 : Idea 2020 数据库: mysql 5.5以上 ...
介绍如何通过docker,nginx,erueka,appllo 搭建基于springboot的SpringCloud架构操作步骤。帮助你开始部署测试环境和开发环境的springboot项目环境。
本书由浅入深地介绍了当今流行的Java Web框架Spring MVC的方方面面,从基础的环境搭建到微服务设计与架构,再到持久化、REST API构建、认证与测试……涵盖了Spring MVC诸多重要且常用的特性。值得一提的是,本书针对...
本基础框架采用 spring mvc+c3p0+jpa的配置方式,里面配置了spring事务管理,异常处理,登陆拦截器 等,全部自己动手搭建,经过测试通过。如果你苦于一个万能的java ee框架搭建,你可以参考。个人喜欢专研技术,但不...