使用java方式配置Spring
完全不使用xml
配置类
| |
| |
| @Configuration |
| public class Config { |
| |
| |
| |
| @Bean |
| public User user(){ |
| return new User(); |
| } |
| } |
实体类
| @Controller |
| public class User { |
| @Value("Spring") |
| private String name; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| } |
| |
实现
| |
| public class MyTest { |
| public static void main(String[] args) { |
| ApplicationContext Context = new AnnotationConfigApplicationContext(Config.class); |
| User getUser = Context.getBean("user", User.class); |
| System.out.println(getUser.getName()); |
| } |
| } |
这里要用AnnotationConfig拿到上下文,参数要填写配置类的class
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术