【Spring】使用JavaConfig实现配置

闲话

哈哈哈哈哈,我又回来了,过年偷懒了一个月,今天开始恢复博客的正常更新咯~

基本要点

除了传统的xml配置方式之外,我们还可以使用Java配置类实现bean的配置
下面我们就简单介绍一下使用javaConfig实现配置的几种方式

前提:需要在xml文件中开启注解支持相关配置,可以参考我上一篇博客 spring使用注解开发

关键注解:@Configuration,用于定义配置类,无论是哪种方式,都必须在配置类上加上这个注解

1、@Configuration+@ComponentScan+@Component

首先我们创建一个实体类User,使用@Component将其注册为一个组件,@Component()里的value为被注册时的bean的id

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("decade")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" 
            + "name='" + name + '\'' 
            + '}';
    }
}

然后我们创建一个配置类MyConfig,@ComponentScan这个注解主要作用是开启扫描指定路径下的组件,自动创建bean,并且会对属性自动装配

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.decade.pojo")
public class MyConfig {
	
}

接下来我们写一个测试类

import com.decade.config.MyConfig;
import com.decade.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Client {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        User user = context.getBean("user2", User.class);
        System.out.println(user.getName());
    }
}

2、@Configuration+@Bean

首先我们还是新建一个实体类User2,使用@Bean注解的时候,对应的实体类上不需要加@Component注解

package com.decade.pojo;

import org.springframework.beans.factory.annotation.Value;

public class User2 {
    private String name;

    public String getName() {
        return name;
    }

    @Value("decade2")
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User2{"
            + "name='" + name + '\''
            + '}';
    }
}

然后我们创建相关的配置类,主要使用@Configuration+ @Bean注解

package com.decade.config;

import com.decade.pojo.User2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig2 {

    @Bean
    public User2 getUser() {
        return new User2();
    }
}

最后,我们写一个测试类来进行测试
这里,getBean方法里面的第一个参数必须与配置类中的方法名相同

import com.decade.config.MyConfig2;
import com.decade.pojo.User2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Client2 {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig2.class);
        User2 user = context.getBean("getUser", User2.class);
        System.out.println(user.getName());
    }
}

运行结果如下图
在这里插入图片描述

如有错误,欢迎指正

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值