3. Spring
3.1 概述
- IoC:控制反转,将创建对象的权利,由自己(new)反转给spring。
- IoC核心:管理对象的生命周期、对象之间关系(注入)
3.2 入门案例:基础案例
-
目标:编写spring IoC入门案例
-
步骤:
- 编写功能实现类,并添加到spring容器:UserDao
- 编写配置类,扫描需要被加载的类
- 测试类
-
步骤:
-
功能实现类,并添加到spring容器:UserDao
package com.czxy.demo01.dao; import org.springframework.stereotype.Component; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Component("userDao1") public class UserDao { public void selectAll() { System.out.println("userdao selectAll"); } }
-
编写配置类,扫描需要被加载的类
package com.czxy.demo01.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Configuration //配置类 @ComponentScan(basePackages = "com.czxy.demo01.dao") //扫描需要被加载的类所在的包 public class SpringConfiguration { }
-
测试类
package com.czxy.demo01; import com.czxy.demo01.config.SpringConfiguration; import com.czxy.demo01.dao.UserDao; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author 桐叔 * @email liangtong@itcast.cn */ public class TestDemo01 { public static void main(String[] args) { // 1 通过处理注解,获得工厂 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfiguration.class); // 2 通过工厂,获得mapper UserDao userDao =(UserDao) applicationContext.getBean("userDao1"); // 3 调用 userDao.selectAll(); } }
-
3.3 入门案例:依赖注入
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZbsIPl8C-1633347949587)(assets/image-20211004111630293.png)]
-
步骤:
- 编写UserDao,并使用@Component
- 编写UserService,并使用@Component
- 修改UserService,提供UserDao成员变量,并使用@Resource完成注入
- 编写测试类,使用@ComponentScan( service、dao)
- 测试类
-
步骤:
-
编写UserDao,并使用@Component
package com.czxy.demo02.dao; import org.springframework.stereotype.Component; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Component("userDao2") public class UserDao { public void selectAll() { System.out.println("dao : selectAll "); } }
-
编写UserService,并使用@Component
-
修改UserService,提供UserDao成员变量,并使用@Resource完成注入
package com.czxy.demo02.service; import com.czxy.demo02.dao.UserDao; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Component("userService2") public class UserService { // 将 userDao1 注入给 UserDao @Resource(name="userDao2") private UserDao userDao; public void selectAll() { // 调用dao userDao.selectAll(); System.out.println("service : selectAll "); } }
-
编写测试类,使用@ComponentScan( service、dao)
package com.czxy.demo02.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Configuration @ComponentScan(basePackages = {"com.czxy.demo02.dao","com.czxy.demo02.service"}) public class Demo02Configuration { }
-
测试类
package com.czxy.demo02; import com.czxy.demo02.config.Demo02Configuration; import com.czxy.demo02.dao.UserDao; import com.czxy.demo02.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * @author 桐叔 * @email liangtong@itcast.cn */ public class TestDemo02 { public static void main(String[] args) { // 1 通过处理注解,获得工厂 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Demo02Configuration.class); // 2 通过工厂,获得mapper UserService userService = applicationContext.getBean("userService2", UserService.class); // 3 调用 userService.selectAll(); } }
3.4 入门案例:依赖注入接口
-
步骤
- 编写UserDao接口
- 编写UserDao实现类:UserDaoImpl
- 编写UserService接口
- 编写UserServiceImpl实现类,并注入UserDao:UserServiceImpl
- 编写配置类
- 编写测试类
-
步骤
-
编写UserDao接口
package com.czxy.demo03.dao; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface UserDao { public void selectAll() ; }
-
编写UserDao实现类:UserDaoImpl
package com.czxy.demo03.dao.impl; import com.czxy.demo03.dao.UserDao; import org.springframework.stereotype.Component; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Component("userDao3") public class UserDaoImpl implements UserDao { @Override public void selectAll() { System.out.println("userdao 接口:selectAll " ); } }
-
编写UserService接口
package com.czxy.demo03.service; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface UserService { public void selectAll(); }
-
编写UserServiceImpl实现类,并注入UserDao:UserServiceImpl
package com.czxy.demo03.service.impl; import com.czxy.demo03.dao.UserDao; import com.czxy.demo03.service.UserService; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Component("userService3") public class UserServiceImpl implements UserService { @Resource(name="userDao3") private UserDao userDao; @Override public void selectAll() { userDao.selectAll();; System.out.println("userService3 : selectAll "); } }
-
编写配置类
package com.czxy.demo03.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Configuration @ComponentScan(basePackages = {"com.czxy.demo03.dao","com.czxy.demo03.service"}) public class Demo03Configuration { }
-
编写测试类,使用整合JUnit进行测试
package com.czxy.demo03; import com.czxy.demo03.config.Demo03Configuration; import com.czxy.demo03.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; /** * @author 桐叔 * @email liangtong@itcast.cn */ @RunWith(SpringJUnit4ClassRunner.class) //spring 整合 Junit @ContextConfiguration(classes = Demo03Configuration.class) //加载配置类 public class TestDemo03 { @Resource(name="userService3") private UserService userService; @Test public void testDemo03() { userService.selectAll(); } }
总结:Mybatis学完以后,紧接着就学习最为重要的框架,Spring框架是企业使用最多的框架,没有之一。Spring是一站式框架,称之为一站式框架的原因是Spring可以整合其他框架。所以学会并且精通是非常之关键的。将课程中的案例敲完之后,又试着将dao变成Mybtis查询数据,成功了,但是感觉之前Mybatis略显生疏,还是要加强一下,好记性不如烂笔头。
又离工作又近了一天
-