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();
    }
}

相比之下,注解开发更为简便

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值