❤️Spring--IOC入门
Posted 一只楠喃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了❤️Spring--IOC入门相关的知识,希望对你有一定的参考价值。
Spring入门
1、概述
- IoC:控制反转,将创建对象的权利,由自己(new)反转给spring。
- IoC核心:管理对象的生命周期、对象之间关系(注入)
2、 入门案例:基础案例
- 编写spring IoC入门案例
- 步骤:
- 编写功能实现类,并添加到spring容器:UserDao
- 编写配置类,扫描需要被加载的类
- 测试类
2.1、功能实现类
package com.czxy.demo01.dao;
import org.springframework.stereotype.Component;
@Component(“userDao1”)
public class UserDao {
public void selectAll() {
System.out.println(“userdao selectAll”);
}
}
2.2、配置类
package com.czxy.demo01.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //配置类
@ComponentScan(basePackages = “com.czxy.demo01.dao”) //扫描需要被加载的类所在的包
public class SpringConfiguration {
}
2.3、测试类
import com.czxy.demo01.config.SpringConfiguration;
import com.czxy.demo01.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
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、入门案例:依赖注入
步骤:
- 编写UserDao,并使用@Component
- 编写UserService,并使用@Component
- 修改UserService,提供UserDao成员变量,并使用@Resource完成注入
- 编写测试类,使用@ComponentScan( service、dao)
- 测试类
3.1、UserDao
import org.springframework.stereotype.Component;
@Component(“userDao2”)
public class UserDao {
public void selectAll() {
System.out.println("dao : selectAll ");
}
}
3.2、编写修改UserService
import com.czxy.demo02.dao.UserDao;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component(“userService2”)
public class UserService {
// 将 userDao1 注入给 UserDao
@Resource(name=“userDao2”)
private UserDao userDao;
public void selectAll() {
// 调用dao
userDao.selectAll();
System.out.println("service : selectAll ");
}
}
3.4、测试类,使用@ComponentScan( service、dao)
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = {“com.czxy.demo02.dao”,“com.czxy.demo02.service”})
public class Demo02Configuration {
}
3.5、测试类
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;
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();
}
}
4、入门案例:依赖注入接口
步骤
- 编写UserDao接口
- 编写UserDao实现类:UserDaoImpl
- 编写UserService接口
- 编写UserServiceImpl实现类,并注入UserDao:UserServiceImpl
- 编写配置类
- 编写测试类
4.1、UserDao接口
public interface UserDao {
public void selectAll() ;
}
4.2、UserDaoImpl
@Component(“userDao3”)
public class UserDaoImpl implements UserDao {
@Override
public void selectAll() {
System.out.println("userdao 接口:selectAll " );
}
}
4.3、UserService接口
public interface UserService {
public void selectAll();
}
4.4、UserServiceImpl实现类
@Component(“userService3”)
public class UserServiceImpl implements UserService {
@Resource(name=“userDao3”)
private UserDao userDao;
@Override
public void selectAll() {
userDao.selectAll();;
System.out.println("userService3 : selectAll ");
}
}
4.5、配置类
@Configuration
@ComponentScan(basePackages = {“com.czxy.demo03.dao”,“com.czxy.demo03.service”})
public class Demo03Configuration {
}
4.6、测试类(整合JUnit)
@RunWith(SpringJUnit4ClassRunner.class) //spring 整合 Junit
@ContextConfiguration(classes = Demo03Configuration.class) //加载配置类
public class TestDemo03 {
@Resource(name=“userService3”)
private UserService userService;
@Test
public void testDemo03() {
userService.selectAll();
}
}
以上是关于❤️Spring--IOC入门的主要内容,如果未能解决你的问题,请参考以下文章