Spring的Java配置

本文介绍Spring框架中的Java配置方式,包括使用@Configuration和@Bean注解进行Bean管理的具体步骤,并通过示例代码展示了如何替代XML配置。

一 点睛

Java配置是Spring 4.x推荐的配置方式,可以完全替代xml配置。

Java配置是通过@Configuration和@Bean来实现的。

  • @Configuration声明当前类是一个配置类,相对于一个Spring配置的xml。

  • @Bean注解在方法上,声明当前方法返回值是一个Bean。

何时使用Java配置,何时使用注解配置呢?主要原则是:全局配置使用Java配置(如数据库相关配置、MVC相关配置)、业务Bean的配置使用注解配置(@Component、@Service、@Respository、@Controller)

二 实战

演示简单的Java配置

1 编写功能类Bean

package com.wisely.highlight_spring4.ch1.javaconfig;
//此处没有使用@Service注解声明Bean
public class FunctionService {
    
    public String sayHello(String word){
        return "Hello " + word +" !";
    }
}

2 编写使用类Bean

package com.wisely.highlight_spring4.ch1.javaconfig;

import com.wisely.highlight_spring4.ch1.javaconfig.FunctionService;
//此处没有使用@Service注解声明Bean
public class UseFunctionService {
    //此时没有使用@Autowired注解注入Bean,所以要编写set方法
    FunctionService functionService;
    
    public void setFunctionService(FunctionService functionService) {
        this.functionService = functionService;
    }
    
    public String SayHello(String word){
        return functionService.sayHello(word);
    }

}

3 编写配置类

package com.wisely.highlight_spring4.ch1.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//使用@Configuration 注解表明当前是一个配置类,这意味着这个类
//可能有0个或多个@Bean注解。此处没有使用包扫描,是因为所有的
//Bean都在此类中定义
@Configuration
public class JavaConfig {
    
    //使用@Bean注解声明当前方法functionService,它的的返回值
    //是一个Bean,Bean的名称是方法名。
    @Bean
    public FunctionService functionService(){
        return new FunctionService();
    }
    
    @Bean
    public UseFunctionService useFunctionService(){
        UseFunctionService useFunctionService = new UseFunctionService();
        //注入FunctionService的Bean的时候直接调用functionService()
        useFunctionService.setFunctionService(functionService());
        return useFunctionService;
        
    }

//另外一种注入方式,直接将FunctionService作为参数给useFunctionService()
//这也是Spring容器提供的极好的功能。在Spring容器中,只要容器中存在某个Bean
//就可以在另外一个Bean的声明方法的参数中注入。
//    @Bean
//    public UseFunctionService useFunctionService(FunctionService functionService){//4
//        UseFunctionService useFunctionService = new UseFunctionService();
//        useFunctionService.setFunctionService(functionService);//和上面那个的@Bean的区别在此
//        return useFunctionService;
//    }
}

4 编写主类

package com.wisely.highlight_spring4.ch1.javaconfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
         AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(JavaConfig.class);
        
         UseFunctionService useFunctionService = context.getBean(UseFunctionService.class);
        
         System.out.println(useFunctionService.SayHello("java config"));
        
         context.close();
        
    }
}

三 测试

Hello java config !

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值