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配置的三种形式的更多相关文章

  1. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  2. Spring boot配置Dubbo三种方式

    方式一 使用注解的方式 导入dubbo-starter 在application.properties配置属性 使用@Service暴露服务 使用@Reference引用服务 使用@EnableDub ...

  3. Spring Bean定义的三种方式

    <!--Spring容器启动配置(web.xml文件)--> <context-param> <param-name>contextConfigLocation&l ...

  4. spring bean实例化的三种方式

    一.使用类的无参构造创建 配置文件 java代码 注意若类里面没有无参的构造,则会出现异常 二.使用静态工厂创建 配置文件 java代码 Factory类 测试类 结果 三.使用实例工厂 配置文件 1 ...

  5. Spring Framework5.0 学习(3)—— spring配置文件的三种形式

    Spring Framework  是 IOC (Inversion of Control  控制反转)原则的实践. IoC is also known as dependency injection ...

  6. @Autowired注解和启动自动扫描的三种方式(spring bean配置自动扫描功能的三种方式)

    前言: @Autowired注解代码定义 @Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, Elemen ...

  7. spring对事务支持的三种形式

    spring对事务支持的三种形式: 1.通过spring配置文件进行切面配置 <bean id="***Manager" class="org.springfram ...

  8. 菜鸟学习Spring——SpringIoC容器基于三种配置的对比

    一.概述 对于实现Bean信息定义的目标,它提供了基于XML.基于注解及基于java类这三种选项.下面总结一下三种配置方式的差异. 二.Bean不同配置方式比较. 三.Bean不同配置方式的适用场合. ...

  9. spring配置datasource三种方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...

随机推荐

  1. Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.

    Linux关于yum命令Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx. 问题: Linux ...

  2. Percona-Server-5.7.16 启动错误

    基于:percona-server-5.7.16 启动报错:  [root@monitor mysql]# ./bin/mysqld_safe --defaults-file=/data/config ...

  3. SpringBoot 定义通过字段验证

    第一步:定义ValidationResult类 public class ValidationResult { // 校验结果是否有错 private boolean hasErrors = fals ...

  4. python部署LNMP业务服务环境

  5. pyspider脚本编写指南

    注意,虽然在本文中会涉及调度策略等内容,但实际执行效果取决于具体策略实现. project 脚本分为不同的 project,不同的 project 之间的任务互相独立,建议为不同的站点建立不同的 pr ...

  6. javascript日期格式处理

    一. 服务端返回的日期和时间之间有T Asp.net MVC中 action返回前台的日期类型数据 是带有 T的,如: 2015-07-07T10:15:01. 这样的数据在Chrome浏览器,会自动 ...

  7. jdbc例子

    public class ConnMysql { public static void main(String[] args) throws ClassNotFoundException, SQLEx ...

  8. Redis_01

    http://redis.io/ http://www.yiibai.com/redis/redis_quick_guide.html X

  9. numpy 矩阵归一化

    new_value = (value - min)/(max-min) def normalization(datingDatamat): max_arr = datingDatamat.max(ax ...

  10. 20165332实验一 Java开发环境的熟悉

    实验一 Java开发环境的熟悉 一.Java开发环境的熟悉-1 1.实验要求: 0 参考实验要求: 1 建立"自己学号exp1"的目录 : 2 在"自己学号exp1&qu ...