spring Bean的三种配置方式
Spring Bean有三种配置方式:
- 传统的XML配置方式
- 基于注解的配置
- 基于类的Java Config
添加spring的maven repository
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <!--这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心 -->
- <version>4.1..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <!--这个jar文件为Spring核心提供了大量扩展 -->
- <version>4.1..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <!--对JUNIT等测试框架的简单封装 -->
- <version>4.1..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <!--为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。-->
- <version>4.1..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <!--这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行(IoC/DI)操作相关的所有类 -->
- <version>4.1..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类。 -->
- <version>4.1..RELEASE</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
一、传统的XML配置方式
BeanFactory.java
- package com.stonegeek.service;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- public interface BeanFactory {
- public void Beantest();
- }
BeanFactoryImpl.java
- package com.stonegeek.service.impl;
- import com.stonegeek.service.BeanFactory;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- public class BeanFactroyImpl implements BeanFactory {
- @Override
- public void Beantest() {
- System.out.println("----------------This is a 传统的XML配置的bean!-------------------");
- }
- }
applicationContext.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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- ">
- <bean id="beanFactroy" class="com.stonegeek.service.impl.BeanFactroyImpl" />
- </beans>
TestBean1.java
- package com.stonegeek;
- import com.stonegeek.service.BeanFactory;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- public class TestBean1 {
- @Test
- public void test(){
- ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
- BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactroy");
- beanFactory.Beantest(); //----------------This is a 传统的XML配置的bean!-------------------
- }
- }
二、基于java注解的配置
如果一个类使用了@Service,那么此类将自动注册成一个bean,不需要再在applicationContext.xml文件定义bean了,类似的还包括@Component、@Repository、@Controller。
然后需要在applicationContext.xml文件中加一行,作用是自动扫描base-package包下的注解:
- <context:component-scan base-package="com.stonegeek" />
BeanFactoryImpl.java
- package com.stonegeek.service.impl;
- import com.stonegeek.service.BeanFactory;
- import org.springframework.stereotype.Service;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- @Service("beanFactory")
- public class BeanFactroyImpl implements BeanFactory {
- @Override
- public void Beantest() {
- System.out.println("----------------This is a 基于Java注解的bean!-------------------");
- }
- }
applicationContext.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"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- ">
- <context:component-scan base-package="com.stonegeek" />
- </beans>
TestBean2.java
- package com.stonegeek;
- import com.stonegeek.service.BeanFactory;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- public class TestBean2 {
- @Test
- public void test(){
- ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
- BeanFactory beanFactory=(BeanFactory) ctx.getBean("beanFactory");
- beanFactory.Beantest(); //This is a 基于java注解的bean!
- }
- }
三、基于类的Java Config
通过java类定义spring配置元数据,且直接消除xml配置文件
Spring3.0基于java的配置直接支持下面的注解:
@Configuration
@Bean
@DependsOn
@Primary
@Lazy
@Import
@ImportResource
@Value
BeanFactoryImpl.java
- package com.stonegeek.service.impl;
- import com.stonegeek.service.BeanFactory;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- public class BeanFactoryImpl implements BeanFactory {
- @Override
- public void Beantest() {
- System.out.println("----------------This is a 基于类的Java Config的bean!-------------------");
- }
- }
BeanConfig.java
- package com.stonegeek.service.config;
- import com.stonegeek.service.BeanFactory;
- import com.stonegeek.service.impl.BeanFactoryImpl;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- @Configuration
- public class BeanConfig {
- @Bean
- public BeanFactory beanFactory(){
- return new BeanFactoryImpl();
- }
- }
TestBean3.java
- package com.stonegeek;
- import com.stonegeek.service.BeanFactory;
- import com.stonegeek.service.config.BeanConfig;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- /**
- * Created by StoneGeek on 2018/5/13.
- */
- public class TestBean3 {
- @Test
- public void test(){
- ApplicationContext applicationContext=new AnnotationConfigApplicationContext(BeanConfig.class);
- BeanFactory beanFactorys=applicationContext.getBean(BeanFactory.class);
- beanFactorys.Beantest(); //This is a 基于类的Java Config Bean!
- }
- }
以上就是spring bean的三种配置方式的简单介绍!!
spring Bean的三种配置方式的更多相关文章
- spring Bean的三种注入方式
1.构造函数注入: 构造函数的注入方式分为很多种 (1)普通构造函数,空参数的构造函数 <bean id="exampleBean" class="examples ...
- spring bean的三种管理方式·
1.无参构造函数 1.xml文件配置内容 <!--无参构造函数--> <bean id="bean1" class="com.imooc.ioc.dem ...
- 【jdbc】【c3p0】c3p0三种配置方式【整理】
c3p0三种配置方式 c3p0的配置方式分为三种,分别是1.setters一个个地设置各个配置项2.类路径下提供一个c3p0.properties文件3.类路径下提供一个c3p0-config.xml ...
- tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- IIS下PHP的三种配置方式比较
在Windows IIS 6.0下配置PHP,通常有CGI.ISAPI和FastCGI三种配置方式,这三种模式都可以在IIS 6.0下成功运行,下面我就讲一下这三种方式配置的区别和性能上的差异. 1. ...
- 【转】tomcat下jndi的三种配置方式
jndi(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API.命名服务将名称和对象联系起来,使得我们可以用 ...
- Spring三 Bean的三种创建方式
创建Bean的三种方式在大多数情况下,Spring容器直接通过new关键字调用构造器来创建Bean实例,而class属性指定Bean实例的实现类,但这不是实例化Bean的唯一方法.实际上,Spring ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
- 【c3p0】 C3P0的三种配置方式以及基本配置项详解
数据库连接池C3P0框架是个非常优异的开源jar,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...
随机推荐
- springmvc使用JSR-303对复杂对象进行校验
JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,官方参考实现是Hibernate Validator.此实现与Hibernate ORM 没有任何关系.JSR ...
- Docker Swarm从部署到基本操作
关于Docker Swarm Docker Swarm由两部分组成: Docker集群:将一个或多个Docker节点组织起来,用户就能以集群的方式进行管理: 应用编排:有一套API用来部署和管理容器: ...
- Storm 系列(一)—— Storm和流处理简介
一.Storm 1.1 简介 Storm 是一个开源的分布式实时计算框架,可以以简单.可靠的方式进行大数据流的处理.通常用于实时分析,在线机器学习.持续计算.分布式 RPC.ETL 等场景.Storm ...
- [币严区块链]数字货币交易所之以太坊(ETH)钱包对接(四) 使用web3j对接以太坊钱包
本文给大家介绍了 Web3j Java 版本的框架的基本使用,大家可根据本文的内容进行扩展性的练习,对其他 API 的使用进行尝试. 使用web3j对接以太坊钱包 一.开发准备事项 启动 Geth 此 ...
- 一键升级所有pip过期库
import pipfrom subprocess import callfrom pip._internal.utils.misc import get_installed_distribution ...
- Android 网络通信框架Volley(二)
Volley提供2个静态方法: public static RequestQueue newRequestQueue(Context context) {} public static Request ...
- GC垃圾收集算法
JVM中的垃圾收集算法实现涉及大量的程序细节,而且各个平台的虚拟机操作内存的方法又各不相同,这里介绍几种垃圾收集算法的思想. 1.标记-清除算法 这是最基础的垃圾收集算法,分为“标记”和“清除”两个阶 ...
- 基于RMAN搭建DataGuard,使用Broker管理DataGuard
一.环境准备 1.数据库软件准备 (1).在主节点,安装单机数据库软件并创建数据库. (2).在备库, 安装单机数据库软件, 但是不创建数据库. 2.操作系统配置 在/etc/hosts下面配置主机名 ...
- [Linux] linux下vim对于意外退出的文档的再次开启
转载自博客:https://blog.csdn.net/ljp1919/article/details/48372615 1.对于同一个文件如果上次已经打开,而未关闭的情况下,又打开该文件进行编辑时, ...
- 让tomcat使用指定JDK
一,前言 我们都知道,tomcat启动前需要配置JDK环境变量,如果没有配置JDK的环境变量,那么tomcat启动的时候就会报错,也就是无法启动. 但是在我们的工作或者学习过程中,有的时候会出现tom ...