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. css 元素实际宽高

    首先定义一个div. 然后稍微装修一下 下面开始区分 一.clientWidth和clientHeigh . clientTop和clientLeft 1,clientWidth的实际宽度 clien ...

  2. HTML+CSS+JavaScript实现2048小游戏

    相信很多人都玩过2048小游戏,规则易懂.操作简单,我曾经也“痴迷”于它,不到2048不罢休,最高成绩合成了4096,现在正好拿它来练练手. 我对于2048的实现,除了使用了现有2048小游戏的配色, ...

  3. 解决SSM框架中,存储到mysql数据库中乱码问题的究极方案

    因为编码格式不匹配的问题,设置了好多遍,都不行,就试着让所有的编码格式保持一致.然后读取.插入数据库再也不乱码了. 数据库编码格式必须和myeclipse编码格式一致 其次依次让以下各文件的编码格式保 ...

  4. apache ignite系列(五):分布式计算

    ignite分布式计算 在ignite中,有传统的MapReduce模型的分布式计算,也有基于分布式存储的并置计算,当数据分散到不同的节点上时,根据提供的并置键,计算会传播到数据所在的节点进行计算,再 ...

  5. python常用内建模块——datetime

    datetime是python处理日期和时间的标准库. 获取当前日期和时间 >>>from datetime import datetime >>>now = da ...

  6. 章节十六、10-TestNG报告和日志

    一.在进行自动化的过程中,日志一般采用log4j 2进行日志记录,但TestNG自己本身也带有日志记录功能(reporter),它的好处在于日志中记录的内容都是testng自动生成的. package ...

  7. 词义消除歧义NLP项目实验

    词义消除歧义NLP项目实验 本项目主要使用https://github.com/alvations/pywsd 中的pywsd库来实现词义消除歧义 目前,该库一部分已经移植到了nltk中,为了获得更好 ...

  8. 【面试】我是如何在面试别人Redis相关知识时“软怼”他的

    事出有因 Redis是一个分布式NoSQL数据库,因其数据都存储在内存中,所以访问速度极快,因此几乎所有公司都拿它做缓存使用,所以Redis常被称为分布式缓存. 一次我的一个同事让我帮他看Redis相 ...

  9. LoadRuuner资源监控

    用ipconfig命令查看IP地址的具体方法.初级工程师面试常面临的问题:网址:http://url.cn/5BaDWvB本机IP:172.0.0.1localhostipconfig命令c查看本机I ...

  10. CF #579 (Div. 3) D1.Remove the Substring (easy version)

    D1.Remove the Substring (easy version) time limit per test2 seconds memory limit per test256 megabyt ...