Spring环境搭建和示例工程

来源:互联网 发布:吃鸡为什么不优化 编辑:程序博客网 时间:2024/06/06 04:04

/**

 * spring官网:https://spring.io/

下载地址http://projects.spring.io/spring-framework/

Spring Framework下的Reference

2.3. Usage scenarios

点击Distribution Zip Files

Distribution Zip Files


真实下载地址 http://repo.spring.io/release/org/springframework/spring

点击一个版本

然后点击最上层的第一个:spring-framework-4.3.3.RELEASE-dist.zip 解压


 * 第一步:新建一个普通java工程new-other-java Project

第二步:建立lib文件夹,引入spring-framework的jar文件全部拷贝到工程中

javadoc和sources 结尾的不用拷贝

最主要的有:spring-aop、spring-beans、spring-context、spring-core

第三步:添加到buildPath:右键点击工程名称-Properties-Java Build Path-Add JARs… 

下载Apache Commons Logging

http://commons.apache.org/proper/commons-logging/download_logging.cgi


commons-logging-1.2.jar也同上添加进去/

 */



IHelloMessage.java接口文件:

package com.jike.spring.chapter01;


public interface IHelloMessage{

publicString sayHello();

}


HelloChina.java文件:

package com.jike.spring.chapter01;

public classHelloChina implementsIHelloMessage {

@Override

publicString sayHello(){

// TODO Auto-generated method stub

return"大家好!";

}

}




HelloWorld.java文件:

package com.jike.spring.chapter01;

public classHelloWorld implementsIHelloMessage {

@Override

publicString sayHello(){

// TODO Auto-generated method stub

return"Hello World!";

}

}




Person.java文件:

package com.jike.spring.chapter01;


public class Person{

private IHelloMessage helloMessage;

publicIHelloMessage getHelloMessage(){

return helloMessage;

}

publicvoid setHelloMessage(IHelloMessagehelloMessage){

this.helloMessage= helloMessage;

}

publicString sayHello(){

return this.helloMessage.sayHello();

}

}



helloMessage.xml文件:

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE beansPUBLIC "-//SPRING/DTD BEAN/EN" 

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="helloWorld" class="com.jike.spring.chapter01.HelloWorld"></bean>

<bean id="helloChina" class="com.jike.spring.chapter01.HelloChina"></bean>

<bean id="person" class="com.jike.spring.chapter01.Person">

    <propertyname="helloMessage" ref="helloChina"/>

</bean>

</beans>



Main.java文件:

package com.jike.spring.chapter01;

import org.springframework.beans.factory.BeanFactory;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;

public class Main{

//2.Spring 入门示例----03创建示例工程

public staticvoid main(String[]args) {

//加载配置文件 以及启动IOC容器  调用人物类 向用户输出问候信息

Resource r = new FileSystemResource("helloMessage.xml");

BeanFactory f= newXmlBeanFactory(r);

Person person= (Person)f.getBean("person");

String s = person.sayHello();

System.out.print("The person is currently saying:"+ s);

}

}

//Main.java右键RunAs--java Application


0 0