天天看點

spring-boot學習筆記 java配置bean

#Spring boot使用 java配置檔案的方式注入bean#
				記錄在 《java開發的颠覆者 spring-boot實戰中的筆記》	
           
//所需jar包依賴
<properties>
        <java.version>1.8</java.version>
        <spring.version>4.1.6.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
           

第一種方式

//最終要調用的方法
public class DoService {
	
    public String sayHello(String name){
        return "hello"+name+"!";
    }
}

           
//用來調用 Beans DoService的bean
public class UseService {
	//聲明屬性
    DoService doService;

    public void setDoService(DoService doService) {
        this.doService = doService;
    }

    public String sayHello(String world){
        return doService.sayHello(world);
    }
}

           
//spring 的java配置檔案
@Configuration 
//這個注解表示這是一個配置檔案   我測試的時候沒有添加也可以注入成功
public class JavaConfig {
    //聲明一個bean
    @Bean
    public DoService doService(){
        return new DoService();
    }
    @Bean
    public UseService useService(){
        UseService useService = 
        new UseService();
        //将UseService中的DoService注入
        useService.setDoService(doService());
        return useService;
    }
}
           
//測試運作類
   public static void main(String[] args) {
   //使用AnnotationConfigApplicationContext 擷取容器中的bean
        AnnotationConfigApplicationContext context =
         new AnnotationConfigApplicationContext(JavaConfig.class);
        UseService useService =
         context.getBean(UseService.class);
        System.out.println(useService.sayHello("火星來了"));
        context.close();
    }
           

第二種方式

更多的使用注解的方式進行開發

//聲明這是要注入的bean
@Service
public class FunctionService {
    public String sayHello(String world){
        return "Hello "+world+" !";
    }
}

           
@Service
public class UseFunctionService {
    //将spring中的bean指派給該屬性
    @Autowired
    FunctionService functionService;

    public String sayHello(String world){
        return functionService.sayHello(world);
    }
}

           
//@Configuration 表示這是一個配置類
@Configuration
//@ComponentScan 掃碼包,及包下的子包
@ComponentScan("com.yjl.highlight_spring4.chi.di")
public class DiConfig {
}
           
public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =
       
        new AnnotationConfigApplicationContext(DiConfig.class);
        UseFunctionService useFunctionService =
                context.getBean(UseFunctionService.class);
        System.out.println(useFunctionService.sayHello("Helloworld"));
        context.close();
    }
}
           

相比之下,注解開發更為簡便

繼續閱讀