JavaEE实验六 :bean的装配
实验目的:
通过基于annotation的装配方式装配bean实例,来达到熟练掌握Spring中bean的装配方式的目的。
实验内容:
定义一个学生类Student,在类中定义(sno,sname,major(专业))三个属性。用三层架构模式模拟添加学生的操作。用注解方式完成bean的装配,在应用程序中获取bean对象,并调用方法完成模拟添加学生。
实验代码:
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用 context 命名空间 ,在配置文件中开启相应的注解处理器 -->
<context:component-scan base-package="com.itheima" />
</beans>
Student
package com.itheima.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component("student")
@Scope("singleton")
public class Student {
@Value("1")
private int sno;
@Value("张三")
private String name;
@Value("计算机科学")
private String major;
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", name='" + name + '\'' +
", major='" + major + '\'' +
'}';
}
}
StudentDao
package com.itheima.dao;
public interface StudentDao {
public void save();
}
StudentDaoImpl
package com.itheima.dao;
import com.itheima.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClasspathXmlApplicationContext;
import org.springframework.stereotype.Repository;
@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {
public void save(){
ApplicationContext applicationContext=new
ClasspathXmlApplicationContext("applicationContext.xml");
Student student=(Student) applicationContext.getBean("student");
System.out.println(student);
System.out.println("执行StudentDaoImpl.save()");
}
}
StudentService
package com.itheima.service;
public interface StudentService {
public void save();
}
StudentServiceImpl
package com.itheima.service;
import com.itheima.dao.StudentDao;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
@Resource(name="studentDao")
private StudentDao studentDao;
public void save(){
this.studentDao.save();
System.out.println("执行StudentServiceImpl.save()");
}
}
StudentController
package com.itheima.controller;
import com.itheima.service.StudentService;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller
public class StudentController {
@Resource(name="studentService")
private StudentService studentService;
public void save(){
this.studentService.save();
System.out.println("执行StudentController.save()");
}
}
实验截图:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。