使用JavaConfig实现配置
2022/6/30 1:51:23
本文主要是介绍使用JavaConfig实现配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用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~
重点分析
- 本示例中没有xml配置文件,而是用了一个AppConfig文件来代替,如果没有这个文件,测试会出错
- @Configuration和@ComponentScan是两个重点注解,@Configuration标识文件是一个配置文件,@ComponentScan指定bean的扫描路径,缺失任何一个都不行
- AppConfig.java中注释了几行代码,如果把注释打开,把Person类上的@Component注释掉,测试类中获取
getPerson
bean,也可以运行成功,这里涉及到@Bean的用法,不过@Bean不这样使用,而是用来获取一些Resource资源
这篇关于使用JavaConfig实现配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-04-26敏捷开发:想要快速交付就必须舍弃产品质量?
- 2024-04-26静态代码分析的这些好处,我竟然都不知道?
- 2024-04-26你在测试金字塔的哪一层?(下)
- 2024-04-26快刀斩乱麻,DevOps让代码评审也自动起来
- 2024-04-262024年最好用的10款ER图神器!
- 2024-04-2203-为啥大模型LLM还没能完全替代你?
- 2024-04-2101-大语言模型发展
- 2024-04-17基于SpringWeb MultipartFile文件上传、下载功能
- 2024-04-14个人开发者,Spring Boot 项目如何部署
- 2024-04-14RAG应用开发实战02-相似性检索的关键 - Embedding