Spring入门例程——javaweb整合开发王者归来例子

Spring入门例程——javaweb整合开发王者归来例子

照着书把例子调通,记录一下过程,分享学习经历,也是第一篇经验博客,根据自己学习时遇到的问题,会把spring目录结构贴出来,有以下几个准备事项:

  • 下载spring3.1.1版本
  • eclipse手动导入libraries方法要掌握
    项目全部源码下载地址

http://download.csdn.net/detail/jl381169437/9329563

目录结构

Spring入门例程——javaweb整合开发王者归来例子_第1张图片

spring包下载

http://download.csdn.net/detail/jl381169437/9329577

创建一个Spring的工程,项目名称及包名类名参照图片,该工程会根据Springle配置文件(applicationContext.xml)加载配置文件中的JavaBean,并通过Java的反射机制调用getter、setter方法设置彼此间的依赖,然后从工程中获取Java对象,执行就可以了


web.xml

用于配置spring框架:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>WEB-INF/classes/applicationContext.xmlparam-value>
    context-param>

    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>

web-app>

applicationContext.xml

配置文件,放在src目录下:


<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
            
    <bean id="daoImpl" 
        class="com.helloweenvsfei.spring.example.DaoImpl">
    bean>
    
    <bean id="service" 
        class="com.helloweenvsfei.spring.example.ServiceImpl">
        
        <property name="dao" ref="daoImpl">property>
    bean>
beans>

IDao.java

Dao接口只有一个方法sayHello:

package com.helloweenvsfei.spring.example;

public interface IDao {
    public String sayHello(String name);
}

DaoImpl.java

实现Dao接口,并根据实际输出不同的问候句:

package com.helloweenvsfei.spring.example;

import java.util.Calendar;

public class DaoImpl implements IDao {
    public String sayHello(String name){
        int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

        if(hour < 6) return "凌晨早" + name;
        if(hour < 12) return "早上好" + name;
        if(hour < 13) return "中午好" + name;
        if(hour < 18) return "下午好" + name;
        return "晚上好" + name;
    }
}

IService.java

定义S而vice层接口。只有一个service方法,没有任何DAO层的以来,没有任何冗余的代码:

package com.helloweenvsfei.spring.example;

public interface IService {
    public void service(String name);
}

ServiceImpl.java

Service实现层中,定义了一个IDao类型的对象,以及对应的getter、setter方法:

package com.helloweenvsfei.spring.example;

public class ServiceImpl implements IService {
    private IDao dao;
    public void service(String name){
        System.out.println(dao.sayHello(name));
    }
    public IDao getDao(){
        return dao;
    }
    public void setDao(IDao dao){
        this.dao = dao;
    }
}   

SpringTest.java

package com.helloweenvsfei.spring.example;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class SpringTest {

    public static void main(String[] args) {
        XmlBeanFactory factory = new XmlBeanFactory(
                new ClassPathResource("applicationContext.xml"));
        IService hello = (IService) factory.getBean("service");
        hello.service("Helloween");
        factory.destroySingletons();
    }

}

你可能感兴趣的:(Spring)