spring基本环境搭建,以及需要用到的一些包

来源:互联网 发布:工程优化设计收费标准 编辑:程序博客网 时间:2024/06/05 15:49

这里写代码片必须要的包:
spring-context-版本.RELEASE.jar (此包用于检测配置文件applicationcontext.xml)
spring-beans-版本.RELEASE.jar (此包用于读取applicationcontext.xml里面的bean)
spring-expression-版本.RELEASE.jar (此包为错误输出,没有的时候无法启动程序)
commons-logging-版本.RELEASE.jar (此包为日志输出包,没有会报错,无法启动)

第一步,先建立目录
自己使用,自己看的懂就好了
第二步,配置applicationcontext.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"         xmlns:aop="http://www.springframework.org/schema/aop"         xmlns:tx="http://www.springframework.org/schema/tx"         xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd          http://www.springframework.org/schema/tx          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd          http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-2.0.xsd ">    <bean id="southMan" class="com.bean.SouthMan"></bean>    <bean id="northMan" class="com.bean.NorthMan"></bean></beans>

这里面的一些引用,目前还没完全清楚,有待琢磨。
第三步,编写方法对应类的方法
1、定义Peson接口

package com.inter;public interface Person {    public void eit();    public void drink();}

2、写实现类

package com.bean;import com.inter.Person;public class NorthMan implements Person{    @Override    public void eit() {        System.out.println("北方人吃面!");    }    @Override    public void drink() {        System.out.println("喜欢喝水。");    }}

3、写测试类test

package com.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.bean.NorthMan;import com.bean.SouthMan;public class Test {    public static void main(String[] args) {        ApplicationContext ap = new FileSystemXmlApplicationContext("src/applicationContext.xml");        NorthMan n = (NorthMan) ap.getBean("northMan");        n.eit();        n.drink();    }}
0 0
原创粉丝点击