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 ...
随机推荐
- jQuery带小图标的Tab切换焦点图
在线演示 本地下载
- spring与memcached整合[转]
1, 开始肯定是下载需要的文件了,这里就下载附件里的文件就好,我也是在网上down的,放这好找.然后我们安装一下Memcache服务器,找到解压的memcached-1.2.1-win32,启动cmd ...
- Parameter Binding in ASP.NET Web API
https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding ...
- Bellman-Ford算法优化
2017-07-27 16:02:48 writer:pprp 在BEllman-Ford算法中,其最外层的循环的迭代次数为n-1,如果不存在负权回路,需要迭代的次数是远远小于n-1; 如果在某一次迭 ...
- 服务器状态监控之snmp&ipmi
一.ipmi 1.简介 IPMI(Intelligent Platform Management Interface)即智能平台管理接口是使硬件管理具备"智能化"的新一代通用接口标 ...
- Google推荐——Glide使用详解(图片加载框架)
零.前言 本文所使用的Glide版本为3.7.0 一.简介 Glide,一个被google所推荐的图片加载库,作者是bumptech.这个库被广泛运用在google的开源项目中,包括2014年的goo ...
- 关于Android中根据ID名动态获取资源的两个方法
在开发中, 我们习惯了类似下面这种方式去实现引用资源: context.getResources().getDrawable(R.drawable.flower); 但是,当我们提前知道这个资源的id ...
- 报错HTTP Status 500 - HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; nested exception is org.hibernate.HibernateException: HHH000142: Javassist Enhancement failed: cn.itcast.entity.
报错 type Exception report message HHH000142: Javassist Enhancement failed: cn.itcast.entity.Customer; ...
- Strust2遇到的问题
前端发一次请求,后台执行execute方法多次,最后发现Acion类继承ActionSupport类,且覆盖了execute,当用户数量一上来就会出现执行多次的BUG,所以千万要注意不能给此方法加An ...
- spring3: AOP 之代理机制
Spring AOP通过代理模式实现,目前支持两种代理:JDK动态代理.CGLIB代理来创建AOP代理,Spring建议优先使用JDK动态代理. JDK动态代理:使用java.lang.reflect ...