Spring 学习笔记2 Spring开发环境搭建

来源:互联网 发布:炫舞按键精灵源码 编辑:程序博客网 时间:2024/06/06 01:33




新建一个Java工程:

工程需要依赖包:


需要的几个类:

package com.itest;public interface PersonService {public abstract void save();}

需要的类实现:

package com.itest.impl;import com.itest.PersonService;public class PersonServiceBean implements PersonService {/* (non-Javadoc) * @see com.itest.impl.PersonService#save() */@Overridepublic void save(){    //实现软件过程之间的解耦,要使用接口编程,对应这个方法抽取出一个接口    // 点击bean 右键 -》refactor -> extrac interface//move 可将类进行移动    System.out.println("我是save()方法");    }}

需要的配置文件:

<?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"      xmlns:aop="http://www.springframework.org/schema/aop"      xmlns:tx="http://www.springframework.org/schema/tx"      xmlns:ehcache="http://www.springmodules.org/schema/ehcache"      xsi:schemaLocation="http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-2.5.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-2.5.xsd             http://www.springframework.org/schema/aop              http://www.springframework.org/schema/aop/spring-aop-2.5.xsd             http://www.springframework.org/schema/tx              http://www.springframework.org/schema/tx/spring-tx-2.5.xsd             http://www.springmodules.org/schema/ehcache             http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">      <!-- 手动增加schema 进行提示windows ->preference->myeclipse->file and editors->xml-> xmlcatalog             点击 add 之后 添加spring-beans-2.5.xsd -->     <!-- id 属性是xml  本省特性的不能 包含特殊字符的  但是name  可以包含特殊字符的-->    <bean id="personService" class="com.itest.impl.PersonServiceBean"></bean></beans>     


测试类:

package junit.test;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itest.PersonService;public class SpringTest {@Testpublic void test() {//fail("Not yet implemented");//寻找spring 配置路径一般有两种方式,一个是类路径,一个是文件系统路径        //一般采用 类路径的方法  并且可以传入多个spring 配置文件 new String[]//ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml"});ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");    // 依赖对象的传感是由 spring 来进行维护的 返回的object类型需要类型转换PersonService personService=  (PersonService) ctx.getBean("personService");System.out.println("hhh");    personService.save();}}


测试结果:



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 健身房老板跑路教练怎么办 健身房老板跑路了怎么办 淘宝断货了该怎么办 淘金猫网购频道账号换了怎么办 京东商城信用卡无法付款怎么办 京东第三方退货拒收怎么办 淘宝凑单收货地址不一样怎么办 淘宝凑单地址不一样怎么办 任性付还不了款怎么办 京东购物卷删了怎么办 荣耀手环3进水了怎么办 手机疏油层没了怎么办 快递到了处理中心不走了怎么办 京东取消了退款怎么办 我的订单删除了怎么办 近邻宝箱子误关怎么办 把收件人电话写错了怎么办 吧收件人电话写错了怎么办 速递易收件人电话写错怎么办 书包上的皮掉了怎么办 美亚很多商品不直邮中国怎么办 我想买刘涛用的化妆品贵妇膏怎么办 自提柜号码忘记了怎么办 京东商城误点确认收货怎么办 京东购物到货后怎么办 京东地址错了怎么办 没买运费险退货怎么办 没有买运费险退货怎么办 买的运费险换货怎么办 顾客买衣服说贵怎么办 卖衣服顾客说贵怎么办 汽车前保险杠刮蹭怎么办 洗手盆下水管堵了怎么办 装修好的卫生间漏水怎么办 马桶水箱盖碎了怎么办 马桶陶瓷盖碎了怎么办 电脑左右分屏了怎么办 在东东助手里安装软件怎么办 王者荣耀进入安全系统怎么办 京东限时达超时怎么办 京东京准达晚点怎么办