Sping IoC is Inversion of Control. 控制反转.
Spring DI 是 Dependency Injection 依赖注入.
它是user把创建对象的控制权交给了spring container.
这样做的好处就是可以Decoupling解耦.
控制反转是Spring 的一种设计思想, 使用的途径就是依赖注入
public class Car
{
private Wheel wheel = new Wheel();
}
public class Wheel
{
private float size;
}
publicclassCar{private Wheel wheel;publicCar(Wheel wheel){this.wheel = wheel;}}publicclassWheel{private float size;}
使用IoC/DI来创建对象
public class Car
{
private Wheel wheel;
// 这样做的好处就是能够解耦, 并且方便开发者能够用mock测试
public Car(Wheel wheel)
{
this.wheel = wheel;
}
}
public class Wheel
{
private float size;
}
spring IoC 容器创建对象, 组装,配置, 管理bean的生命周期从创建到销毁.
spring容器使用DI来管理application的组件
How to create a bean
1.Create a metadata object
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
2. Create Bean Configuration File
<?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-3.0.xsd">
<bean id = "helloWorld" class = "com.tutorialspoint.HelloWorld">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
3. Create bean
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
4. Run program
Your Message : Hello World!
ClassPathXmlApplicationContext().This API loads beans configuration file and eventually based on the provided API, it takes care of creating and initializing all the objects
ClassPathXmlApplicationContext在程序运行时会加载bean的xml配置文件,来创建bean和初始化
Bean's lifecycle details
Spring bean factory is responsible for managing the life cycle of beans created through spring container
Spring framework provides following 4 ways for controlling life cycle events of a bean:
InitializingBean
and DisposableBean
callback interfacesinit()
and destroy()
methods in bean configuration file@PostConstruct
and @PreDestroy
annotations使用反射
public static Object newInstance(String className) {
Class<?> cls = null;
Object obj = null;
try {
cls = Class.forName(className);
obj = cls.newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
return obj;
}
Refers
https://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm
https://howtodoinjava.com/spring-core/spring-bean-life-cycle/
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。