您的位置:首页 > 编程语言 > Java开发

Spring 基础实例1

2016-01-05 11:07 696 查看
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----------");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: