4-5 Spring Bean裝配之基於Java的容器註解-@Bean

基於java的容器註解

  • @Bean標識一個用於配置和初始化一個由SpringIoC容器管理的新對象的方法,類似於XML配置文件的< bean/ >
  • 可以在Spring的@Component註解的類中使用@Bean註解任何方法(僅僅是可以)
  • 上一點中,通常使用的是@Configuration

@Bean的例子

在這裏插入圖片描述
pom.xml

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.14.RELEASE</version>
    </dependency>

spring-beanannotation.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.torey.beanannotation"/>
</beans>
package com.torey.beanannotation.javabased;
public interface Store {
}
package com.torey.beanannotation.javabased;

/**
 * @ClassName:StringStore
 * @Description:
 * @author: Torey
 */
public class StringStore implements Store {
    public void init(){
        System.out.println("執行了初始化方法-init");
    }
    public void destroy(){
        System.out.println("執行了銷燬方法destory");
    }
}
package com.torey.beanannotation.javabased;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName:StoreConfig
 * @Description:
 * @author: Torey
 */
@Configuration
public class StoreConfig {
    //bean的id爲 方法名
    @Bean(name="store1",initMethod = "init",destroyMethod = "destroy")
    public Store stringStore(){
        return new StringStore();
    }
}
package com.torey.springtest.base;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

/**
 * @ClassName:UnitTestBase
 * @Description:
 * @author: Torey
 */
public class UnitTestBase {
    public UnitTestBase(){}
    private String springXmlPath;
    private ClassPathXmlApplicationContext applicationContext;
    public UnitTestBase(String springXmlpath){
        this.springXmlPath=springXmlpath;
    }
    @Before
    public void before(){
        if (StringUtils.isEmpty(springXmlPath)) {
            springXmlPath="classpath*:spring-*.xml";
        }
        applicationContext= new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]"));
        applicationContext.start();
    }
    @After
   public void after(){
        applicationContext.destroy();
   }
   public Object getBean(String beanId){
       return applicationContext.getBean(beanId);
   }
}
package com.torey.springtest.bean;

import com.torey.beanannotation.javabased.Store;
import com.torey.springtest.base.UnitTestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

/**
 * @ClassName:TestResources
 * @Description:
 * @author: Torey
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
    public TestBeanAnnotation(){
        super("classpath:spring-beanannotation.xml");
    }
    @Test
    public void test(){
        //使用@Bean,beanId爲方法名
       // Store store= (Store)super.getBean("stringStore");
        //使用@Bean(name="store1")
        Store store= (Store)super.getBean("store1");
        System.out.println(store.getClass().getName());
    }
}

在這裏插入圖片描述
根據慕課網 moocer老師的spring課程 整理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章