Spring Bean的三种配置方式

目录

一、传统的XML配置方式

二、基于java注解的配置

三、基于类的Java Config


正文

Spring Bean有三种配置方式:

  • 传统的XML配置方式
  • 基于注解的配置
  • 基于类的Java Config

添加spring的maven repository

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <!--这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心 -->
      <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <!--这个jar文件为Spring核心提供了大量扩展 -->
      <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <!--对JUNIT等测试框架的简单封装 -->
      <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <!--为JDBC、Hibernate、JDO、JPA等提供的一致的声明式和编程式事务管理。-->
      <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <!--这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行(IoC/DI)操作相关的所有类 -->
      <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类。 -->
      <version>4.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>

一、传统的XML配置方式

        BeanFactory.java

package com.stonegeek.service;


public interface BeanFactory {
    public void Beantest();
}

        BeanFactoryImpl.java 

package com.stonegeek.service.impl;

import com.stonegeek.service.BeanFactory;


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;


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注解的配置

  使用组件扫描可以不必在Spring的配置文件中配置各个`<bean/>`节点:

<context:component-scan base-package="com.stonegeek" />

        `base-package`表示需要扫描的“根包”,当配置后,Spring容器会自动扫描根包下所有类,及各及子包类。

        目的:让Spring知道有哪些类,事实上,只是配置组件扫描,Spring并不会创建这些类的对象。如需要Spring创建某些类的对象,还需要为这些类添加注解!

常用类注解
@Component---------通用注解(推荐)
@Named-------------通用注解(不推荐)
@Service-----------业务逻辑类注解
@Controller--------控制器类注解
@Repository--------持久层(数据访问层)处理类注解
>以上5种注解是等效的!只是语义不同!(推荐采用对应的)

当同时配置组件扫描和注解后,Spring就会创建这些对象!

  • Spring创建类的对象时,默认使用bean的id是将类的首字母改为小写:

        class:Userao---->bean id:userDao

  • 如果需要自定义名称,可以在注解上添加配置:

        @Component("dao")

  BeanFactoryImpl.java

package com.stonegeek.service.impl;

import com.stonegeek.service.BeanFactory;
import org.springframework.stereotype.Service;


@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;


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;

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;

@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;


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的三种配置方式的简单介绍!!

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值