Spring框架问题简答

1.什么是Spring框架?Spring框架有哪些主要模块?

spring框架是Java开发框架。模块 core,aop,web,orm,Dao

2.使用Spring框架能带来哪些好处?

方便解耦,简化开发:通过Spring提供的IoC容器,我们可以将对象之间的依赖关系交由Spring进行控制,可以更专注于上层的应用。
Spring提供的AOP功能:方便进行面向切面的编程,实现安全、事务、日志等功能管理,提高复用性。
实物支持:通过声明式方式灵活地进行事务的管理,提高开发效率和质量。
Spring对Junit4支持:可以通过注解方便的测试Spring程序。
方便集成各种优秀框架:Redis、kafka、es等。。。。。。。。

3.什么是控制反转?依赖注入?

https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans
空格反转就是把对象创建过程交给了spring容器完成。依赖注入就是spring负责解决个对象的依赖关系,并且自动注入。

4.BeanFactory和ApplicationContext的区别?

https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#context-introduction-ctx-vs-beanfactory

https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-beanfactory
BeanFactory:是Spring里面Bean工厂的接口,只提供了实例化对象和拿对象的功能;只有获取对象的时候才会实例化。
https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#context-introduction
ApplicationContext:继承BeanFactory接口,提供了更多的的功能;国际化、消息机制、AOP等,容器启动就会实例化对象,当然可以配置延迟加载。

5.Spring Bean的生命周期?

添加链接描述
实例化bean对象——》设置对象属性——》执行前置处理——》执行初始化(init)——》执行后置处理——》使用对象——》执行销毁(destroy)

6.Spring Bean的作用域和区别?

https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-factory-scopes
singleton:(Default) Scopes a single bean definition to a single object instance for each Spring IoC container.
prototype:Scopes a single bean definition to any number of object instances.

follow scopes only valid in the context of a web-aware Spring ApplicationContext.

request:Scopes a single bean definition to the lifecycle of a single HTTP request.
session:Scopes a single bean definition to the lifecycle of an HTTP Session.
application:Scopes a single bean definition to the lifecycle of a ServletContext.
websocket:Scopes a single bean definition to the lifecycle of a WebSocket.

7、注入方式

https://docs.spring.io/spring/docs/5.2.0.RELEASE/spring-framework-reference/core.html#beans-factory-autowire
no:(Default) No autowiring. Bean references must be defined by ref elements.
byName:Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired.
byType:Lets a property be autowired if exactly one bean of the property type exists in the container.
constructor:Analogous to byType but applies to constructor arguments.

发布了105 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43792385/article/details/103252367