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,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...
随机推荐
- ssh的秘钥认证
ssh秘钥认证简述 通常我们会使用x-shell.putty.MobaXterm等支持ssh连接的工具去登录服务器进行管理,而执行ssh命令.scp命令等从一台服务器登录另外一台服务器的时候,通常需要 ...
- 搭建自己的技术博客系列(一)使用 hexo 搭建一个精美的静态博客
1.安装 Git 和 nodejs https://hexo.io/zh-cn/docs/
- Redis的那些最常见面试问题(转)
Redis的那些最常见面试问题 1.什么是redis? Redis 是一个基于内存的高性能key-value数据库. 2.Reids的特点 Redis本质上是一个Key-Value类型 ...
- .netcore 使用阿里云短信
准备工作 阿里云上申请短信服务 创建短信应用.签名.短信模板并申请审核,如果审核不通过,接口是调不通的. 配置专门用来发短信的accessKeyId和 accessKeySecret 开始开发 下载安 ...
- FreeSql (四)实体特性 Fluent Api
FreeSql 提供使用 Fluent Api, 在外部配置实体的数据库特性,Fluent Api 的方法命名与特性名保持一致,如下: fsql.CodeFirst .ConfigEntity< ...
- C#中的根据实体增删改操作
在日常操作中,我们经常会对一些数据进行批量更新, 我在使用EF的时候,没有找到比较好的批量更新的解决方案, 便参考了张占岭前辈的博客,整合了这么一个简略版的使用实体类生成数据库增删改SQL的操作类 在 ...
- C#中using的使用-以FileStream写入文件为例
场景 CS中FileStream的对比以及使用方法: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100396022 关注公众号 ...
- 5.1、顺序队列(java实现)
1.实现代码 public class SeqQueue { private final int MaxSize = 8; private int rear; //队尾指针 private int f ...
- linux 操作系统级别监控 vmstat/dstat 命令
vmstat命令综合了CPU.进程.内存.磁盘IO等信息 命令:vmstat 1 表示vmstat每2秒采集数据,一直采集,直到我结束程序 vmstat 2 1 表示每个两秒采集一次 ...
- 【赶快收藏】Hystrix实战,优雅提升系统的鲁棒性
背景 最近接手了一个系统,其功能都是查询.查询分了两种方式,一种是公司集团提供的查询能力,支持全国各个省份的查询,但是业务高峰期时服务响应比较慢:另外一种是各省的分公司都分别提供了对应的查询能力,但是 ...