Spring基础知识汇总
来源:互联网 发布:qq mac版是什么 编辑:程序博客网 时间:2024/05/20 03:46
1.IOC容器:
Sping 的 BeanFactory 容器:XmlBeanFactory
这个容器从一个 XML 文件中读取配置元数据,由这些元数据来生成一个被配置化的系统或者应用。
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));
Spring ApplicationContext 容器:
FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径
ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
2.Bean作用域:
singleton 在每一个 Spring IoC 容器中只有一个实例
prototype 每次需要bean时都会创建一个新的实例
request 每次http请求会创建一个新的实例。只在Web Spring ApplicationContext的上下文中有效。
session 每次http会话会创建一个新的实例。只在Web Spring ApplicationContext的上下文中有效。
global-session 每次全局http会话会创建一个新的实例。只在 Web Spring ApplicationContext 的上下文中有效。
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
scope="singleton">
</bean>
3.依赖注入类型:
Constructor-based dependency injection
当容器调用带有多个参数的构造函数类时,实现基于构造函数的 DI,每个代表在其他类中的一个依赖关系。
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<constructor-arg ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
Setter-based dependency injection
基于 setter 方法的 DI 是通过在调用无参数的构造函数或无参数的静态工厂方法实例化 bean 之后容器调用 beans 的 setter 方法来实现的。
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
4.自动装配模式:
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor"
autowire="byName">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
5.注解:
@Required
@Required 注解应用于 bean 属性的 setter 方法。
@Autowired
@Autowired 注解可以应用到 bean 属性的 setter 方法,非 setter 方法,构造函数和属性。
当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中视图执行 byType 自动连接。
@Qualifier
通过指定确切的将被连线的 bean,@Autowired 和 @Qualifier 注解可以用来删除混乱。
JSR-250 Annotations
Spring 支持 JSR-250 的基础的注解,其中包括了 @Resource,@PostConstruct 和 @PreDestroy 注解。
Sping 的 BeanFactory 容器:XmlBeanFactory
这个容器从一个 XML 文件中读取配置元数据,由这些元数据来生成一个被配置化的系统或者应用。
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml"));
Spring ApplicationContext 容器:
FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径
ApplicationContext context = new FileSystemXmlApplicationContext("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");
ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。
2.Bean作用域:
singleton 在每一个 Spring IoC 容器中只有一个实例
prototype 每次需要bean时都会创建一个新的实例
request 每次http请求会创建一个新的实例。只在Web Spring ApplicationContext的上下文中有效。
session 每次http会话会创建一个新的实例。只在Web Spring ApplicationContext的上下文中有效。
global-session 每次全局http会话会创建一个新的实例。只在 Web Spring ApplicationContext 的上下文中有效。
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld"
scope="singleton">
</bean>
3.依赖注入类型:
Constructor-based dependency injection
当容器调用带有多个参数的构造函数类时,实现基于构造函数的 DI,每个代表在其他类中的一个依赖关系。
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<constructor-arg ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
Setter-based dependency injection
基于 setter 方法的 DI 是通过在调用无参数的构造函数或无参数的静态工厂方法实例化 bean 之后容器调用 beans 的 setter 方法来实现的。
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor">
<property name="spellChecker" ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
4.自动装配模式:
no 这是默认的设置,它意味着没有自动装配,你应该使用显式的bean引用来连线。你不用为了连线做特殊的事。在依赖注入章节你已经看到这个了。
byName 由属性名自动装配。Spring 容器看到在 XML 配置文件中 bean 的自动装配的属性设置为 byName。然后尝试匹配,并且将它的属性与在配置文件中被定义为相同名称的 beans 的属性进行连接。
byType 由属性数据类型自动装配。Spring 容器看到在 XML 配置文件中 bean 的自动装配的属性设置为 byType。然后如果它的类型匹配配置文件中的一个确切的 bean 名称,它将尝试匹配和连接属性的类型。如果存在不止一个这样的 bean,则一个致命的异常将会被抛出。
constructor 类似于 byType,但该类型适用于构造函数参数类型。如果在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生。
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor"
autowire="byName">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
5.注解:
@Required
@Required 注解应用于 bean 属性的 setter 方法。
@Autowired
@Autowired 注解可以应用到 bean 属性的 setter 方法,非 setter 方法,构造函数和属性。
当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中视图执行 byType 自动连接。
@Qualifier
通过指定确切的将被连线的 bean,@Autowired 和 @Qualifier 注解可以用来删除混乱。
JSR-250 Annotations
Spring 支持 JSR-250 的基础的注解,其中包括了 @Resource,@PostConstruct 和 @PreDestroy 注解。
0 0
- spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring 基础知识汇总
- Spring基础知识汇总
- Spring基础知识汇总
- Spring 基础知识汇总
- Python3抓取糗百、不得姐、kanqu.com
- width与max-width的区别
- REDUCE INSTRUCTION SET COMPUTERS
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- 控制台简单实现mysql数据库学生管理系统
- Spring基础知识汇总
- 组件ScrollView的使用
- 求逆序对数
- Linux中启动Tomcat:bash: ./startup.sh: Permission denied问题
- ORB_SLAM2的编译运行
- 谷歌无人车将开启新的时代——首次载客
- [模版] 高精度
- 剑指Offer面试题18 & Leetcode572
- Unity Mesh生成图形二---绘制轮胎印,耕地区域