栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring Bean的三种配置方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring Bean的三种配置方式

spring Bean的三种配置方式 - StoneGeek - 博客园

正文

Spring Bean有三种配置方式:

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

添加spring的maven repository


      org.springframework
      spring-core
      
      4.1.0.RELEASE
    
    
      org.springframework
      spring-context
      
      4.1.0.RELEASE
    
    
      org.springframework
      spring-test
      
      4.1.0.RELEASE
    
    
      org.springframework
      spring-tx
      
      4.1.0.RELEASE
    
    
      org.springframework
      spring-beans
      
      4.1.0.RELEASE
    
    
      org.springframework
      spring-jdbc
      
      4.1.0.RELEASE
    
    
      junit
      junit
      4.12
    

回到顶部

一、传统的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



    

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

  如果一个类使用了@Service,那么此类将自动注册成一个bean,不需要再在applicationContext.xml文件定义bean了,类似的还包括@Component、@Repository、@Controller。

  然后需要在applicationContext.xml文件中加一行,作用是自动扫描base-package包下的注解:

  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



    


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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362237.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号