使用Java方法配置Spring代码解析
作者:CodeHuba 时间:2023-07-15 09:20:59
使用Java的方式配置Spring
我们现在要完全不使用Spring的xml配置,全权使用Java来配置Spring!
JavaConfig是Spring的一个子项目,在Spring4之后,他成为了一个核心功能。
实体类:
public class User {
private String name;
public String getName() {
return name;
}
@Value("huba") //属性注入值
public void setName(String name) {
this.name = name;
}
}
配置类:
package com.kuang.config;
import com.kuang.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration //这个也会spring容器托管,注册到容器中,因为他本来就是一个@Component
//@Configuration:代表这个一个配置类,就等同于beans.xml
@ComponentScan("com.kuang.pojo")
@Import(MyConfig2.class)
public class MyConfig {
//注册一个bean,就相当于我们之前写的<bean>标签
//方法名就相当于bean标签中的id
//方法的返回值就相当于bean标签中的class属性
@Bean
public User user(){
return new User();//就是返回要注入bean的对象
}
}
测试类:
import com.kuang.config.MyConfig;
import com.kuang.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class myTest {
@Test
public void test(){
//如果完全使用配置类方式,只能通过AnnotationConfigApplicationContext获取容器,通过配置类的class对象加载!
ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}
这种纯Java的配置方式,在SpringBoot中随处可见!
来源:https://www.cnblogs.com/CodeHuba/p/13559405.html
标签:Java,配置,Spring
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#中WinForm使用OpencvSharp4实现简易抓边
2023-04-15 15:54:39
![](https://img.aspxhome.com/file/2023/7/119577_0s.png)
Java开发环境配置方法
2022-05-25 02:02:29
![](https://img.aspxhome.com/file/2023/0/69340_0s.jpg)
浅谈C#中对引用类型的误解
2021-10-18 12:40:37
![](https://img.aspxhome.com/file/2023/4/74844_0s.png)
C++ 继承,虚继承(内存结构)详解
2023-09-05 21:28:01
![](https://img.aspxhome.com/file/2023/8/120808_0s.png)
Java 普通代码块静态代码块执行顺序(实例讲解)
2022-07-11 05:44:28
Spring实战之Bean定义中的SpEL表达式语言支持操作示例
2021-07-07 10:46:19
ThreadLocal的set方法原理示例解析
2023-11-09 15:06:09
![](https://img.aspxhome.com/file/2023/1/59181_0s.png)
java Springboot实现教务管理系统
2023-01-18 00:28:12
![](https://img.aspxhome.com/file/2023/6/64906_0s.png)
C#泛型集合类System.Collections.Generic
2023-02-24 19:21:18
Java接口返回省市区树形结构的实现
2021-10-16 05:07:05
![](https://img.aspxhome.com/file/2023/1/83101_0s.jpg)
Java求一个分数数列的前20项之和的实现代码
2021-08-22 14:59:58
WinForm拖拽控件生成副本的解决方法
2022-07-02 13:28:26
![](https://img.aspxhome.com/file/2023/5/76385_0s.png)
Java中一个线程执行死循环有什么后果
2022-05-24 00:28:35
Android通过LIstView显示文件列表的两种方法介绍
2022-05-26 00:00:39
![](https://img.aspxhome.com/file/2023/4/138684_0s.gif)
Java使用Redisson分布式锁实现原理
2023-12-10 11:07:30
![](https://img.aspxhome.com/file/2023/1/68141_0s.png)
SpringBoot整合支付宝APP支付
2021-10-12 02:57:24
![](https://img.aspxhome.com/file/2023/6/67476_0s.jpg)
Spring Cloud Gateway(读取、修改 Request Body)的操作
2023-11-09 19:25:46
![](https://img.aspxhome.com/file/2023/0/59240_0s.jpg)
使用Maven搭建Hadoop开发环境
2021-09-11 07:55:45
Dubbo服务校验参数的解决方案
2023-06-09 14:30:10
![](https://img.aspxhome.com/file/2023/1/75751_0s.png)
Java类的继承实例详解(动力节点Java学院整理)
2023-01-28 13:19:31
![](https://img.aspxhome.com/file/2023/4/65794_0s.png)