【03】Spring IOC 配置

  • 项目包结构
    在这里插入图片描述

1. XML 配置 IoC

		<bean id="teaDao" class="com.hxzy.dao.impl.TeacherDaoImpl"/>
		<bean id="teaService" class="com.hxzy.service.impl.TeacherServiceImpl">
			<property name="teacherDao" ref="teaDao"/>
		</bean>
		<bean id="teaController" class="com.hxzy.controller.TeacherController">
			<property name="teacherService" ref="teaService"/>
		</bean>

2. 注解配置 IoC

  • 注解配置之前的准备工作
    • Spring配置文件中添加context命名空间
    • 开启注解自动扫描
	<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:p="http://www.springframework.org/schema/p"
		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">
	
		<!-- 扫描注解包 , 扫描代码的先后决定着Spring加载bean的先后顺序 -->
		<context:component-scan base-package="com.hxzy"/>
	</beans>
  1. @Component

    Component :组件
    可用于标记任意的类,如实体类;

	@Component(value = "tea")
	@Scope(value = "singleton")
	public class Teacher implements Serializable {
		@Value("李老师")
		private String teaName;
		@Value("60")
		private Integer teaAge;
			
		public Teacher() {
			// TODO Auto-generated constructor stub
		}
		public String getTeaName() {
			return teaName;
		}
		public void setTeaName(String teaName) {
			this.teaName = teaName;
		}
		public Integer getTeaAge() {
			return teaAge;
		}
		public void setTeaAge(Integer teaAge) {
			this.teaAge = teaAge;
		}	
	}
  1. @Controller

    Controller : 控制器
    明确使用该注解标记 Web层的控制器;

	import javax.annotation.Resource;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Controller;
	import com.hxzy.service.TeacherService;
	
	@Controller // 控制器,teacherController -- 首字母小写
	public class TeacherController {
	
	//	@Autowired  --  自动装配 , 根据Spring容器注册的bean的类型进行装配
		@Resource(name = "teacherServiceImpl") // 根据id加载已经注册好的bean
		private TeacherService teacherService;
	
		public void setTeacherService(TeacherService teacherService) {
			this.teacherService = teacherService;
		}
	
		public void dianMing() {
			System.out.println("TeacherController ... dianMing()");
			teacherService.dianMing();
		}
	
	}
  1. @Service
  2. Service : 业务
    明确使用该注解标记 业务层;

	import javax.annotation.Resource;
	import org.springframework.beans.factory.annotation.Autowired;
	import org.springframework.stereotype.Service;
	import com.hxzy.dao.TeacherDao;
	import com.hxzy.service.TeacherService;
	
	@Service
	public class TeacherServiceImpl implements TeacherService{
		
	//	@Autowired --  自动装配 , 根据Spring容器注册的bean的类型进行装配
		@Resource(name = "teacherDaoImpl")
		private TeacherDao teacherDao;
		
		public void setTeacherDao(TeacherDao teacherDao) {
			this.teacherDao = teacherDao;
		}
		
		@Override
		public void dianMing() {
			System.out.println("TeacherServiceImpl ... dianMing()");
			teacherDao.dianMing();
		}
	}
  1. @Repository
  2. Repository: 持久层
    明确使用该注解标记 DAO 的实现类 ;

	import org.springframework.stereotype.Repository;
	import com.hxzy.dao.TeacherDao;
	
	@Repository // @Repository 持久层直接,标记dao,默认创建的对象名是类的首字母变小写
	public class TeacherDaoImpl implements TeacherDao{
		@Override
		public void dianMing() {
			System.out.println("TeacherDaoImpl ... dianMing()");
		}
  • 测试
	public class TestClass05 {
	
		@Test
		public void testStudy() {
			ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
			TeacherController controller = (TeacherController) context.getBean("teacherController");
			controller.dianMing();
			TeacherController teaController = (TeacherController) context.getBean("teaController");
			teaController.dianMing();
		}
	}  

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Spectre_win/article/details/89553313