Spring Bean有三种配置方式:

  • 传统的XML配置方式
  • 基于注解的配置
  • 基于类的Java Config

添加spring的maven repository

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-core</artifactId>
  4. <!--这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心 -->
  5. <version>4.1..RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-context</artifactId>
  10. <!--这个jar文件为Spring核心提供了大量扩展 -->
  11. <version>4.1..RELEASE</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-test</artifactId>
  16. <!--对JUNIT等测试框架的简单封装 -->
  17. <version>4.1..RELEASE</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.springframework</groupId>
  21. <artifactId>spring-tx</artifactId>
  22. <!--为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。-->
  23. <version>4.1..RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-beans</artifactId>
  28. <!--这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行(IoC/DI)操作相关的所有类 -->
  29. <version>4.1..RELEASE</version>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>spring-jdbc</artifactId>
  34. <!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类。 -->
  35. <version>4.1..RELEASE</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>junit</groupId>
  39. <artifactId>junit</artifactId>
  40. <version>4.12</version>
  41. </dependency>

一、传统的XML配置方式

  BeanFactory.java

  1. package com.stonegeek.service;
  2.  
  3. /**
  4. * Created by StoneGeek on 2018/5/13.
  5. */
  6. public interface BeanFactory {
  7. public void Beantest();
  8. }

  BeanFactoryImpl.java

  1. package com.stonegeek.service.impl;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4.  
  5. /**
  6. * Created by StoneGeek on 2018/5/13.
  7. */
  8. public class BeanFactroyImpl implements BeanFactory {
  9. @Override
  10. public void Beantest() {
  11. System.out.println("----------------This is a 传统的XML配置的bean!-------------------");
  12. }
  13. }

  applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. ">
  11. <bean id="beanFactroy" class="com.stonegeek.service.impl.BeanFactroyImpl" />
  12.  
  13. </beans>

  TestBean1.java

  1. package com.stonegeek;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. /**
  9. * Created by StoneGeek on 2018/5/13.
  10. */
  11. public class TestBean1 {
  12. @Test
  13. public void test(){
  14. ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
  15. BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactroy");
  16. beanFactory.Beantest(); //----------------This is a 传统的XML配置的bean!-------------------
  17. }
  18. }

二、基于java注解的配置

  如果一个类使用了@Service,那么此类将自动注册成一个bean,不需要再在applicationContext.xml文件定义bean了,类似的还包括@Component、@Repository、@Controller。

  然后需要在applicationContext.xml文件中加一行,作用是自动扫描base-package包下的注解:

  1. <context:component-scan base-package="com.stonegeek" />

  BeanFactoryImpl.java

  1. package com.stonegeek.service.impl;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4. import org.springframework.stereotype.Service;
  5.  
  6. /**
  7. * Created by StoneGeek on 2018/5/13.
  8. */
  9. @Service("beanFactory")
  10. public class BeanFactroyImpl implements BeanFactory {
  11. @Override
  12. public void Beantest() {
  13. System.out.println("----------------This is a 基于Java注解的bean!-------------------");
  14. }
  15. }

  applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. ">
  11. <context:component-scan base-package="com.stonegeek" />
  12.  
  13. </beans>

  TestBean2.java

  1. package com.stonegeek;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. /**
  9. * Created by StoneGeek on 2018/5/13.
  10. */
  11. public class TestBean2 {
  12. @Test
  13. public void test(){
  14. ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
  15. BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactory");
  16. beanFactory.Beantest(); //This is a 基于java注解的bean!
  17. }
  18. }

三、基于类的Java Config

  通过java类定义spring配置元数据,且直接消除xml配置文件

  Spring3.0基于java的配置直接支持下面的注解:

  @Configuration

  @Bean

  @DependsOn

  @Primary

  @Lazy

  @Import

  @ImportResource

  @Value

  BeanFactoryImpl.java

  1. package com.stonegeek.service.impl;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4.  
  5. /**
  6. * Created by StoneGeek on 2018/5/13.
  7. */
  8. public class BeanFactoryImpl implements BeanFactory {
  9. @Override
  10. public void Beantest() {
  11. System.out.println("----------------This is a 基于类的Java Config的bean!-------------------");
  12. }
  13. }

  BeanConfig.java

  1. package com.stonegeek.service.config;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4. import com.stonegeek.service.impl.BeanFactoryImpl;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7.  
  8. /**
  9. * Created by StoneGeek on 2018/5/13.
  10. */
  11. @Configuration
  12. public class BeanConfig {
  13. @Bean
  14. public BeanFactory beanFactory(){
  15. return new BeanFactoryImpl();
  16. }
  17. }

  TestBean3.java

  1. package com.stonegeek;
  2.  
  3. import com.stonegeek.service.BeanFactory;
  4. import com.stonegeek.service.config.BeanConfig;
  5. import org.junit.Test;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  8.  
  9. /**
  10. * Created by StoneGeek on 2018/5/13.
  11. */
  12. public class TestBean3 {
  13. @Test
  14. public void test(){
  15. ApplicationContext applicationContext=new AnnotationConfigApplicationContext(BeanConfig.class);
  16. BeanFactory beanFactorys=applicationContext.getBean(BeanFactory.class);
  17. beanFactorys.Beantest(); //This is a 基于类的Java Config Bean!
  18. }
  19. }

  以上就是spring bean的三种配置方式的简单介绍!!

  

spring Bean的三种配置方式的更多相关文章

  1. spring Bean的三种注入方式

    1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...

  2. spring bean的三种管理方式·

    1.无参构造函数 1.xml文件配置内容 <!--无参构造函数--> <bean id="bean1" class="com.imooc.ioc.dem ...

  3. 【jdbc】【c3p0】c3p0三种配置方式【整理】

    c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...

  4. tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  5. IIS下PHP的三种配置方式比较

    在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...

  6. 【转】tomcat下jndi的三种配置方式

    jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...

  7. Spring三 Bean的三种创建方式

    创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...

  8. Spring IOC以及三种注入方式

    IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...

  9. 【c3p0】 C3P0的三种配置方式以及基本配置项详解

    数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...

随机推荐

  1. springmvc使用JSR-303对复杂对象进行校验

    JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,官方参考实现是Hibernate Validator.此实现与Hibernate ORM 没有任何关系.JSR ...

  2. Docker Swarm从部署到基本操作

    关于Docker Swarm Docker Swarm由两部分组成: Docker集群:将一个或多个Docker节点组织起来,用户就能以集群的方式进行管理: 应用编排:有一套API用来部署和管理容器: ...

  3. Storm 系列(一)—— Storm和流处理简介

    一.Storm 1.1 简介 Storm 是一个开源的分布式实时计算框架,可以以简单.可靠的方式进行大数据流的处理.通常用于实时分析,在线机器学习.持续计算.分布式 RPC.ETL 等场景.Storm ...

  4. [币严区块链]数字货币交易所之以太坊(ETH)钱包对接(四) 使用web3j对接以太坊钱包

    本文给大家介绍了 Web3j Java 版本的框架的基本使用,大家可根据本文的内容进行扩展性的练习,对其他 API 的使用进行尝试. 使用web3j对接以太坊钱包 一.开发准备事项 启动 Geth 此 ...

  5. 一键升级所有pip过期库

    import pipfrom subprocess import callfrom pip._internal.utils.misc import get_installed_distribution ...

  6. Android 网络通信框架Volley(二)

    Volley提供2个静态方法: public static RequestQueue newRequestQueue(Context context) {} public static Request ...

  7. GC垃圾收集算法

    JVM中的垃圾收集算法实现涉及大量的程序细节,而且各个平台的虚拟机操作内存的方法又各不相同,这里介绍几种垃圾收集算法的思想. 1.标记-清除算法 这是最基础的垃圾收集算法,分为“标记”和“清除”两个阶 ...

  8. 基于RMAN搭建DataGuard,使用Broker管理DataGuard

    一.环境准备 1.数据库软件准备 (1).在主节点,安装单机数据库软件并创建数据库. (2).在备库, 安装单机数据库软件, 但是不创建数据库. 2.操作系统配置 在/etc/hosts下面配置主机名 ...

  9. [Linux] linux下vim对于意外退出的文档的再次开启

    转载自博客:https://blog.csdn.net/ljp1919/article/details/48372615 1.对于同一个文件如果上次已经打开,而未关闭的情况下,又打开该文件进行编辑时, ...

  10. 让tomcat使用指定JDK

    一,前言 我们都知道,tomcat启动前需要配置JDK环境变量,如果没有配置JDK的环境变量,那么tomcat启动的时候就会报错,也就是无法启动. 但是在我们的工作或者学习过程中,有的时候会出现tom ...