Spring基础依赖
pom依赖
<!-- Spring 基础包括 :
Spring-core/Spring-beans/Spring-app/Spring-expression
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
核心配置文件(beans.xml/application.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
UserService 的注册,通过bean标签进行注册
1.<bean>标签
1.1 id: 当前bean标签的标识符
1.2 class 指定注册当前的类(不是接口)
-->
<bean id="studentService" class="com.bboy.service.impl.StudentServiceImpl">
<!-- 属性注入
1. name 表示在当前类中的属性名
例如: studentDao 是在StudentServiceImpl 类中的属性名
2. ref 表示当前属性在spring 核心配置文件中的bean = 》id为 studentDao
-->
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="teacherService" class="com.bboy.service.impl.TeacherServiceImpl">
<property name="teacherDao" ref="teacherDao"/>
</bean>
<bean id="studentDao" class="com.bboy.dao.impl.StudentDapImpl"/>
<bean id="teacherDao" class="com.bboy.dao.impl.TeacherDaoImpl"/>
</beans>
测试 调用核心配置文件里的bean对象
package com.bboy.demo;
import com.bboy.service.StudentService;
import com.bboy.service.TeacherService;
import com.bboy.service.impl.StudentServiceImpl;
import com.bboy.service.impl.TeacherServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/20 0020 10:30:48
*/
public class Demo {
public static void main(String[] args) {
//- : 使用StudentService
/* StudentService service = new StudentServiceImpl();
service.listStudents();*/
//-1. 获取Spring容器(通过配置文件获取Spring容器)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
//-2. 从容器中获取去bean对象
StudentService studentService =(StudentService) applicationContext.getBean("studentService");
studentService.listStudents();
TeacherService teacherService =(TeacherService) applicationContext.getBean("teacherService");
teacherService.listTeachers();
}
}
依赖注入
- 通过service调用dao
- 属性注入: 将StudentDao注入到StudentService
i.在StudentService实现类中设置StudentDao的属性并生成其get与set方法
package com.bboy.service.impl;
import com.bboy.dao.StudentDao;
import com.bboy.pojo.Student;
import com.bboy.service.StudentService;
import java.util.List;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/20 0020 10:27:51
*/
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao){
this.studentDao=studentDao;
}
public StudentDao getStudentDao() {
return studentDao;
}
@Override
public void listStudents() {
System.out.println("==============>>StudentServiceImpl");
}
}
ii. 在spring核心的配置文件中将StudentDao注入到StudentService中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
UserService 的注册,通过bean标签进行注册
1.<bean>标签
1.1 id: 当前bean标签的标识符
1.2 class 指定注册当前的类(不是接口)
-->
<bean id="studentService" class="com.bboy.service.impl.StudentServiceImpl">
<!-- 属性注入
1. name 表示在当前类中的属性名
例如: studentDao 是在StudentServiceImpl 类中的属性名
2. ref 表示当前属性在spring 核心配置文件中的bean = 》id为 studentDao
-->
<property name="studentDao" ref="studentDao"/>
</bean>
<bean id="teacherService" class="com.bboy.service.impl.TeacherServiceImpl">
<property name="teacherDao" ref="teacherDao"/>
</bean>
<bean id="studentDao" class="com.bboy.dao.impl.StudentDapImpl"/>
<bean id="teacherDao" class="com.bboy.dao.impl.TeacherDaoImpl"/>
</beans>
标签:11,20,service,Spring,bboy,com,StudentDao,import,StudentService
From: https://www.cnblogs.com/Qinyyds/p/17843555.html