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();
} }
总结:不同配置方式比较
我们来看一下不同配置方式在不同方面的使用

最新文章

  1. Entity Framework 教程——Entity Framework中的实体类型
  2. 和JavaScript家的闭包玩玩捉迷藏
  3. C++11 auto_ptr 的问题
  4. firefox 提示 setTimeout():useless setTimeout call (missing quotes around argument?) 错误
  5. dbca:Exception in thread &quot;main&quot; java.lang.UnsatisfiedLinkError: get
  6. 添加crontab为什么要重定向输出到/dev/null
  7. HDU1003 Max Sum(求最大字段和)
  8. MongoDB五种树形结构表示法
  9. Distributed systems
  10. Android 模拟器上的127.0.0.1 localhost
  11. python 深浅拷贝
  12. Luogu P5290 [十二省联考2019]春节十二响
  13. 利用unittest+ddt进行接口测试(一):简单demo
  14. Linux 三剑客(Awk、Sed、Grep)
  15. 逆FizzBuzz问题求最短序列
  16. ubuntu/deepin 下 Sha 哈 dow 哈 socks 全局配置
  17. 让多个HTML页面 使用 同一段HTML代码
  18. 多线程之BlockingQueue中 take、offer、put、add的一些比较
  19. 4.IIC总线
  20. Jquery中的DOM操作:

热门文章

  1. css盒模型不同浏览器下解释不同 解决办法
  2. Docker 容器内配置Tomcat manager 远程控制
  3. 未来简史之数据主义(Dataism)
  4. MINA学习汇总
  5. JSP内置对象及作用
  6. 报错HTTP Status 500 - HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; nested exception is org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.itcast.entity.
  7. Java 基于JavaMail的邮件发送
  8. python模块及模块安装
  9. Ajax-04 jQuery Ajax 常用操作
  10. GitLab使用总结[转]