Spring 基础实例1

applicationContext.xml

<bean id="bookAction" class="com.xml.spring.action.BookAction">
        <property name="bookService" ref="bookService"></property>
    </bean>
    <bean id="bookService" class="com.xml.spring.service.BookService">
        <property name="bookDao" ref="bookDao"></property>
    </bean>
    <bean id="bookDao" class="com.xml.spring.dao.BookDao"></bean>

    <bean id="parentAction" class="com.xml.spring.action.ParentAction">
        <property name="stu" ref="student" />
        <property name="teh" ref="teacher" />
    </bean>
    <bean id="student" class="com.xml.spring.comom.Student">
        <property name="name" value="tom" />
        <property name="sex" value="men" />
        <property name="age" value="10" />
    </bean>
    <bean id="teacher" class="com.xml.spring.comom.Teacher" parent="student">
        <property name="name" value="jim" />
        <property name="age" value="25" />
        <property name="course" value="computer" />
    </bean>

Action:

@Controller
public class BookAction {

    private BookService bookService;

    public BookService getBookService() {
        return bookService;
    }

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping(value = ("/xml/book"))
    public void test() {
        System.out.println("-----BookAction.test function-------------");
        bookService.test();
    }
}
@Controller
public class ParentAction {

    private Student stu;
    private Teacher teh;

    @RequestMapping(value = "/parent")
    public void parent1() {
        System.out.println("---s:" + stu.getAge() + "----" + stu.getName() + "---" + stu.getSex());
        System.out.println(teh.getAge() + "--" + teh.getCourse() + "--" + teh.getName() + "------" + teh.getSex());
    }

    public Student getStu() {
        return stu;
    }

    public void setStu(Student stu) {
        this.stu = stu;
    }

    public Teacher getTeh() {
        return teh;
    }

    public void setTeh(Teacher teh) {
        this.teh = teh;
    }

}

BEAN:

/*
 * Creation : 26 Nov 2015
 */
package com.xml.spring.comom;

/**
 * The Class Student.
 */
public class Student {

    /** The name. */
    private String name;
    
    /** The sex. */
    private String sex;
    
    /** The age. */
    private int age;

    /**
     * Gets the name.
     *
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name.
     *
     * @param name the new name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Gets the sex.
     *
     * @return the sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * Sets the sex.
     *
     * @param sex the new sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * Gets the age.
     *
     * @return the age
     */
    public int getAge() {
        return age;
    }

    /**
     * Sets the age.
     *
     * @param age the new age
     */
    public void setAge(int age) {
        this.age = age;
    }
}

/*
 * Creation : 26 Nov 2015
 */
package com.xml.spring.comom;

public class Teacher {

    private String name;
    private String sex;
    private int age;
    private String course;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

}

Service:

/*
 * Creation : 24 Nov 2015
 */
package com.xml.spring.service;

import com.xml.spring.dao.BookDao;

public class BookService {

    private BookDao bookDao;

    public BookDao getBookDao() {
        return bookDao;
    }

    public void setBookDao(BookDao bookDao) {
        this.bookDao = bookDao;
    }

    public void test() {
        // TODO Auto-generated method stub
        System.out.println("---------BookService .test function -------");
        bookDao.test();
    }
}


DAO:

/*
 * Creation : 24 Nov 2015
 */
package com.xml.spring.dao;


public class BookDao {


    public void test() {
        // TODO Auto-generated method stub
        System.out.println("--------------Book Dao .test----------");
    }


}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MaxCode-1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值