spring Bean配置的三种形式
Spring Bean配置有以下三种形式:
- 传统的xml配置
- Spring 2.5 以后新增注解配置
- Spring3.0以后新增JavaConfig
1. 传统的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"> <bean id="helloWorld" class="com.example.whx.HelloWorld"/> </beans>
2.基于注解的配置
@Component是Spring容器的基本注解,表示容器中的一个Bean组件。使用@Comopnent相当于代替了XML配置中的<bean>元素
HelloWorld.java
package annotationConfig; import org.springframework.stereotype.Component; /**
* 如果属性名称是value,value可以省略。
* 如果不指定value,默认值是类名首先字母变为小写。
* @Component(value="beanId") 就是把当前类实例化。相当于<bean id="beanId">
*/
@Component
public class HelloWorld { public void sayHello() {
System.out.println("Hello World");
}
}
Spring在2.5后提供了一个context的命名空间,它提供了通过扫描类包来加载使用注解定义的bean的方式。
<!-- 开启注解扫描 -->
<context:component-scan base-package="com.example.annotationconfig"/>
Spring2.5 添加了对JSR250注解的支持,有@Resource @PostConstruct @ PreDestroy
自动装配注解
Spring自带的@AutoWired
JSR250的@Resource注解
JSR330的@Inject注解
Spring容器是默认禁用注解装配的。要使用基于注解的自动装配,我们在xml文件中配置:
<context:annotation-config>
使用<context:annotation-config>相当于代替了xml配置的<property>和<constructor-arg>元素
<context:comoponent-scan>除了包含<context:annotatiion-config>的作用外,还能自动扫描和注册base-package下@Component注解的类,将其bean注册到spring容器里,所以配置文件如果有component-scan就不需要annotation-config
3.基于java config
通过java类定义spring配置元数据,且直接消除xml配置文件
Spring3.0基于java的配置直接支持下面的注解:
@Configuration
@Bean
@DependsOn
@Primary
@Lazy
@Import
@ImportResource
@Value
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
测试类,查看配置是否成功:
public class Test {
public static void main(String[] args) {
//加载配置
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.sayHello();
}
}

spring Bean配置的三种形式的更多相关文章
- 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比
[原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...
- Spring boot配置Dubbo三种方式
方式一 使用注解的方式 导入dubbo-starter 在application.properties配置属性 使用@Service暴露服务 使用@Reference引用服务 使用@EnableDub ...
- Spring Bean定义的三种方式
<!--Spring容器启动配置(web.xml文件)--> <context-param> <param-name>contextConfigLocation&l ...
- spring bean实例化的三种方式
一.使用类的无参构造创建 配置文件 java代码 注意若类里面没有无参的构造,则会出现异常 二.使用静态工厂创建 配置文件 java代码 Factory类 测试类 结果 三.使用实例工厂 配置文件 1 ...
- Spring Framework5.0 学习(3)—— spring配置文件的三种形式
Spring Framework 是 IOC (Inversion of Control 控制反转)原则的实践. IoC is also known as dependency injection ...
- @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)
前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...
- spring对事务支持的三种形式
spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...
- 菜鸟学习Spring——SpringIoC容器基于三种配置的对比
一.概述 对于实现Bean信息定义的目标,它提供了基于XML.基于注解及基于java类这三种选项.下面总结一下三种配置方式的差异. 二.Bean不同配置方式比较. 三.Bean不同配置方式的适用场合. ...
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
随机推荐
- SQuirrel-GUI工具安装手册-基于phoenix驱动
背景描述: SQuirrel sql client 官方地址:http://www.squirrelsql.org/index.php?page=screenshots 一个图形界面的管理工具 安装手 ...
- Oracle数据类型(4)
字符类型: CHAR(size):固定长度字符串,最大长度2000 bytes VARCHAR2(size):可变长度的字符串,最大长度4000 bytes,可做索引的最大长度749 NCHAR(si ...
- 用idea编译器写第一个Java程序——步骤
- RENOUNCEMENT
I must not think of thee;and,tired yet syrong,I shun the thought that lurks in all delight--The thou ...
- ButterKnife使用详谈
(1)ButterKnife是什么? 在开发过程中,我们总是会写大量的findViewById和点击事件,像初始view.设置view监听这样简单而重复的操作让人觉得特别麻烦,当然不会偷懒的程序员不是 ...
- git下载别人的代码
1. 打开别人github上的源码地址,点击Clone or download 2. 拷贝链接 3. 通过git clone URL来下载 此外,还可以通过pwd来查看当前目录的路径,一般都是下载到当 ...
- mysql 库与表操作
1. 库操作 1.1. 创建数据库 语法规则:create database 库名; CREATE DATABASE dt55; 在创建库时,希望指定编码语法:create database 库名 c ...
- [转载]java操作word生成水印
应用场景 为了保护版权或辨别文件的真伪,有时需要在生成的Word文件中动态添加水印,PageOffice组件的WaterMark类就封装了给在线编辑的Word文件添加水印这一功能,调用接口非常简单. ...
- C#实现文件与二进制互转并存入数据库
这篇文章主要介绍了C#实现文件与二进制互转并存入数据库,本文直接给出代码实例,代码中包含详细注释,需要的朋友可以参考下 //这个方法是浏览文件对象 private void button1_C ...
- get running task , process and service
public class MyActivityManager extends ExpandableListActivity { private static final String NAME = & ...