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,高性能的管理着数据源,这里只讨论程序本身负责数据源,不讨论容器管理. ---------------------------------------- ...
随机推荐
- zabbix监控进程和端口存活脚本
自定义脚本监控端口和进程,脚本process_port_check.sh 内容: [root@mysql02 data]# cat test.sh #!/bin/bash ############## ...
- SpringBoot系列__01HelloWorld
接触SpringBoot很久了,但是一直没有很深入的研究一下源码,最近重启了博客,顺便开始深入研究一下技术. 1.简介 参照官方文档的说法,SpringBoot的设计理念就是为了简化Java程序员搭建 ...
- IDEA中把一个Maven工程安装到本地仓库
首先说明问题,我需要使用PageHelper插件,但是这个分页插件是改造过的,是一个pom工程,现在需要当作一个jar包使用,所以需要安装到本地仓库 1. 2. 3. 4. 5.成功 6.需要使用该j ...
- Linux上安装JDK1.7步骤
1.使用SecurtCRT连接上Linux,把jdk的压缩包传递过去:(传递的方法在我的博客中也有写,参考之前的博客) 2.解压缩jdk:tar -zxvf jdk-7u55-linux-i586.t ...
- .Net基础篇_学习笔记_第四天_if结构
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Linux变量替换技术
1.1.1 ${value:-word} 如果变量value没有定义,则返回word,如果已经赋值则返回value变量的值 result=${jimyy:-UNSET} echo $result ...
- springMvc 注解@JsonFormat 日期格式化
1:一定要加入依赖,否则不生效: <!--日期格式化依赖--> <dependency> <groupId>com.fasterxml.jackson.core&l ...
- Cookie的删除
1.设置一个Cookie,与要删除的Cookie同名,并将有效时间设置为0: protected void doGet(HttpServletRequest request, HttpServletR ...
- caffe学习二:py-faster-rcnn配置运行faster_rcnn_end2end-VGG_CNN_M_1024 (Ubuntu16.04)
本文的主要目的是学习记录. 原文连接:https://blog.csdn.net/samylee/article/details/51099508 本博客中我将对py-faster-rcnn配置运行f ...
- validator 自动化校验
温馨提示 请收藏再看.此文篇幅太长,你短时间看不完:此文干货太多,错过太可惜. 示例代码可以关注逸飞兮(公众号)回复jy获取. 收获 讲解详细:能让你掌握使用 hibernate-validator ...