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()");
    }
}

实验截图:

在这里插入图片描述

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...