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的三种配置方式的更多相关文章

  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. 一文看懂java的IO流

    废话不多说,直接上代码 import com.fasterxml.jackson.databind.ObjectMapper; import java.io.*; import java.nio.ch ...

  2. 云原生生态周报 Vol. 19 | Helm 推荐用户转向 V3

    作者| 禅鸣.忠源.天元.进超.元毅 业界要闻 Helm 官方推荐用户迁移到 V3 版本 Helm 官方发布博客,指导用户从 v2 迁移到 v3,这标志着官方开始正式推进 helm 从 v2 转向 v ...

  3. Python集训营45天—Day02

    目录 变量和运算符 1.1 初步介绍 1.2 使用案例 1.3 知识点梳理 1.4 练习 序言:这一章我们将学习变量以及常见的类型,我们将以案例和代码相结合的方式进行梳理,但是其中所有的案例和知识点 ...

  4. MySQL查看当前用户

    mysql> select current_user();+----------------+| current_user() |+----------------+| root@localho ...

  5. Java第二次作业第五题

    自定义异常类,非法年龄类,并在person3类中使用此类,根据情况抛出异常,并进行处理. package naizi; class IllegalAgeException extends Except ...

  6. 链表实现比较高效的删除倒数第k项

    最近写链表不太顺,无限的段错误.今天中午写的链表删除倒数第k项,用的带尾节点的双向链表,感觉已经把效率提到最高了,还是超时,改了很多方法都不行,最 终决定看博客,发现原来是审题错了,阳历给的是以-1结 ...

  7. Nginx 的三大功能

    1.HTTP服务器 Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 2.反向代理服务器 Nginx也是反向代理服务器. 说反向代理之前先说一 ...

  8. EasyJson 发布

    JSON库很常用了,现在开源的JSON库也有很多.但是我们仍然面临下列问题 1)时不时的爆出这个Json库出现漏洞,那个json库出现漏洞.一旦出现漏洞只能升级,想切换JSON都不成. 2)一个项目中 ...

  9. python中pyqt5的进度条--python实战(十)

    python太博大精深了,使用场景非常多.最近笔者一直使用PyQt5编一些小程序,顺便就把一些常用的东西列出来,做个记录和积累吧.进度条是非常常用的东西,今天用的时候,顺便温习了一下,这个东西自己感觉 ...

  10. Node 与JS的区别

    1.nodejs是运行于服务器端的:2.global:代表node当中的一个全局对象,类似于浏览器当中的window,定义全局对象:global.a:3.作用域:nodejs中一个文件就是一个作用域: ...