IOC(Inversion of Control,控制反转)是Spring框架核心概念之一。它是一种设计原则,用来实现对象的松耦合和依赖管理。在传统的编程中,对象负责创建或查找其依赖对象,而在IOC模式下,这些职责被移交给一个容器,由容器来负责对象的创建和管理。
本文章将介绍几种实现IOC的方式
目录
一.前提准备
二.基于xml的方式
三.基于注解的方式
四.基于配置类的方式
一.前提准备
1)引入Spring的相关依赖:
2)创建示例类(要交给Spring管理的类):
@Data
public class DataConfig {
private String url;
private String driverName;
private String userName;
private String password;
}
注意:@Data注解是由Lombok提供的
Lombok 是一个用于 Java 编程的开源库,旨在通过自动生成常见代码来简化开发过程。其主要功能是减少样板代码(boilerplate code),如 getter、setter、构造函数、equals 和 hashCode 方法等,从而使代码更加简洁和易于维护。
二.基于xml的方式
首先在src/main/resources
目录下创建xml文件,文件名可自定义,但是后缀为.xml
将xml文件用来定义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"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 配置 User 对象 -->
<bean class="com.example.spring_demo.ioc.DataConfig" id="config">
//里面写要定义的属性
//id用来给要管理的对象取名
</bean>
</beans>
我们将要配置的属性写进去:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 配置 User 对象 -->
<bean class="com.example.spring_demo.ioc.DataConfig" id="config">
<property name="driverName" value="Driver"></property>
<property name="url" value="localhost:8080"></property>
<property name="userName" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
最后通过IOC容器ApplicationContext
来管理和获取Bean。
public class Test {
public static void main(String[] args) {
ApplicationContext context1=new ClassPathXmlApplicationContext("springIoc.xml");
System.out.println(context1.getBean("config"));
}
}
结果如下:
23:35:58.074 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c655221
23:35:58.201 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 1 bean definitions from class path resource [springIoc.xml]
23:35:58.228 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config'
DataConfig(url=localhost:8080, driverName=Driver, userName=root, password=root)
Process finished with exit code 0
三.基于注解的方式
使用注解的方式相对简单,只需要给目标类添加 @Component 即可
@Data
@Component("config2")
public class DataConfig {
@Value("localhost:3306")
private String url;
@Value("Driver")
private String driverName;
@Value("root")
private String userName;
@Value("root")
private String password;
}
//@Component注解告知Spring这个类交给ioc容器管理
//"config"是给要管理的对象取的名字
//@Value给管理的对象注入初始值
接着通过IOC容器ApplicationContext
来管理和获取Bean
public class Test {
public static void main(String[] args) {
ApplicationContext context3=new AnnotationConfigApplicationContext("com.example.spring_demo.ioc");
System.out.println(context3.getBean("config2"));
}
}
//"com.example.spring_demo.ioc"是要扫描的类所在的包,Spring会查找该包有无要管理的类
结果如下:
23:44:44.345 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@53e25b76
23:44:44.362 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
23:44:44.448 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
23:44:44.450 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
23:44:44.451 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
23:44:44.452 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
23:44:44.458 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beanConfiguration'
23:44:44.462 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config1'
23:44:44.469 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config2'
DataConfig(url=localhost:3306, driverName=Driver, userName=root, password=root)
四.基于配置类的方式
这种方式和xml的原理一样,只不过是使用java代码的形式来实现的,它定义了一个的专门的配置类来存放我们要管理的类,配置类要加上@Configuration注解
首先创建配置类
@Configuration
public class BeanConfiguration {
//默认id是方法名,id可使用name属性来配置
@Bean(name="config2")
public DataConfig dataConfig(){
DataConfig dataConfig=new DataConfig();
dataConfig.setDriverName("Driver");
dataConfig.setUrl("localhost:3306/spring_demo");
dataConfig.setUserName("root");
dataConfig.setPassword("root");
return dataConfig;
}
}
接着通过IOC容器ApplicationContext
来管理和获取Bean
public class Test {
public static void main(String[] args) {
ApplicationContext context2=new AnnotationConfigApplicationContext("com.example.spring_demo.ioc");
System.out.println(context2.getBean("config1"));
}
}
结果如下:
23:56:29.299 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\code\blog_code\target\classes\com\example\spring_demo\ioc\BeanConfiguration.class]
23:56:29.301 [main] DEBUG org.springframework.context.annotation.ClassPathBeanDefinitionScanner - Identified candidate component class: file [D:\code\blog_code\target\classes\com\example\spring_demo\ioc\DataConfig.class]
23:56:29.313 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@53e25b76
23:56:29.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
23:56:29.413 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
23:56:29.415 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
23:56:29.416 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
23:56:29.417 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
23:56:29.423 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'beanConfiguration'
23:56:29.428 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config1'
23:56:29.435 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'config2'
DataConfig(url=localhost:3306, driverName=Driver, userName=root, password=root)
Process finished with exit code 0
到这里关于SpringIOC实现的三种方式就介绍完了~