【Spring配置元数据的三种方式】

传统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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

Java类配置 基于@Configuration注解

@Bean可以对任何使用Spring @Component注解的类中的方法使用

@Configuration
public class BeanConfiguration {

    @Bean
    public Account account(){
        return new Account("001001001");
    }

    @Bean
    public User user(Account account){
        return new User("张三",18,account);
    }

    @Bean
    public User userNoAccount(){
        return new User("张三",18,null);
    }
}

基于注解

Spring提供进一步典型化注解:@Component,@Service,@Repository和 @Controller。@Component是任何Spring托管组件的通用构造型。 @Repository,@Service和@Controller是@Component针对更特定用例的专业化(分别在持久层,服务层和表示层)。因此,可以使用@Component,但是使用@Repository,@Service或者@Controller,能更好地适合于通过工具处理,或与切面进行关联,而且这些构造型注解在Spring框架的将来版本中还可以包含其他语义。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dangkei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值