Spring实现IOC的几种方式
- 基于xml实现Spring的IOC
- 基于注解实现Spring的IOC
- 基于JavaConfig实现的Spring的IOC
- 基于SpringBoot实现Spring的IOC
基于xml实现Spring的IOC
引入spring核心依赖
<!--spring核心容器-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
</dependency>
UserService依赖了UserDao
- userdao
public class UserDao {
public void getUser(){
System.out.println("获取了用户");
}
}
- userService
userDao需要被注入所以必须加set方法
public class UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUser(){
userDao.getUser();
}
}
- 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">
<!--让spring帮我们去new对象 并且对象之间的依赖也让spring帮我们组织-->
<bean name="userService" class="com.spring.service.UserService">
<!--依赖注入-->
<property name="userDao" ref="userDao"></property>
</bean>
<bean name="userDao" class="com.spring.dao.UserDao"></bean>
</beans>
- 测试
@org.junit.Test
public void test(){
//要依赖spring注入,就需要从spring容器中获取UserService
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService) context.getBean("userService");
userService.getUser();
}
基于注解实现Spring的IOC
@Component:被标注的类会交给spring进行管理,把这个类注册成一个bean
- 配置文件开启注解
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--告诉spring注解所在的包在哪儿-->
<context:component-scan base-package="com.spring"/>
</beans>
- 在各个类上加上@Component注解
@Component
public class UserDao implements IUserDao{
public void getUser() {
System.out.println("获取了用户");
}
}
@Component
public class UserService implements IUSerService{
//此时可以用@Autowired 自动注入userDao
@Autowired
private UserDao userDao;
public void getUser() {
userDao.getUser();
}
}
- 测试
@org.junit.Test
public void test(){
//要依赖spring注入,就需要从spring容器中获取UserService
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
UserService userService = (UserService) context.getBean("userService");
userService.getUser();
}
基于JavaConfig实现的Spring的IOC
还是需要在各个需要被注册成bean的类行加@Component注解
新建配置类
@Configuration //=xml的配置类
@ComponentScan //=<context:component-scan
public class SpringConfig {
}
- 测试类
@org.junit.Test
public void test(){
//要依赖spring注入,就需要从spring容器中获取UserService
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService) context.getBean("userService");
userService.getUser();
}
基于SpringBoot实现Spring的IOC
- 引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 配置启动类
@SpringBootApplication
public class DemoApplication {
}
- 测试
@org.junit.Test
public void test(){
//要依赖spring注入,就需要从spring容器中获取UserService
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DemoApplication.class);
UserService userService = (UserService) context.getBean("userService");
userService.getUser();
}