ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用JavaConfig实现配置

2022-06-29 19:31:12  阅读:156  来源: 互联网

标签:实现 配置 springframework JavaConfig Person context import org public


使用JavaConfig实现配置

概述

本文主要讲述使用Config配置文件来代替xml配置文件,Config配置文件和xml配置文件功能一模一样,xml配置可能相对繁琐,每次要去官网拷贝外面的一层

先看代码

背景:一人一猫一狗
猫类

package com.kuangstudy.pojo;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 功能描述
 *
 * @since 2022-06-26
 */
@Component
@Scope("prototype")
public class Cat {
    public void shout() {
        System.out.println("miao~");
    }
}

狗类

package com.kuangstudy.pojo;

import org.springframework.stereotype.Component;

/**
 * 功能描述
 *
 * @since 2022-06-26
 */
@Component
public class Dog {
    public void shout() {
        System.out.println("wang~");
    }
}

人类

package com.kuangstudy.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * 功能描述
 *
 * @since 2022-06-26
 */
@Component
@Scope("prototype")
public class Person {
    @Value("xiaohong")
    private String name;

    @Autowired(required = false)
    private Cat cat;
    //
    @Autowired
    private Dog dog;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", cat=" + cat +
                ", dog=" + dog +
                '}';
    }
}

配置类

package com.kuangstudy.config;

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

/**
 * 功能描述
 *
 * @since 2022-06-29
 */
@Configuration
@ComponentScan("com.kuangstudy.pojo")
public class AppConfig {
//    @Bean
//    public Person getPerson() {
//        return new Person();
//    }
}

测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.kuangstudy.config.AppConfig;
import com.kuangstudy.pojo.Person;

/**
 * 功能描述
 *
 * @since 2022-06-29
 */
public class Test1 {
    @Test
    public void test1() {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        Person person = context.getBean("person", Person.class);
        System.out.println(person.getName());
        person.getCat().shout();
        person.getDog().shout();

    }
}

结果

xiaohong
miao~
wang~

重点分析

  1. 本示例中没有xml配置文件,而是用了一个AppConfig文件来代替,如果没有这个文件,测试会出错
  2. @Configuration和@ComponentScan是两个重点注解,@Configuration标识文件是一个配置文件,@ComponentScan指定bean的扫描路径,缺失任何一个都不行
  3. AppConfig.java中注释了几行代码,如果把注释打开,把Person类上的@Component注释掉,测试类中获取getPersonbean,也可以运行成功,这里涉及到@Bean的用法,不过@Bean不这样使用,而是用来获取一些Resource资源

标签:实现,配置,springframework,JavaConfig,Person,context,import,org,public
来源: https://www.cnblogs.com/Oh-mydream/p/16424673.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有