展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

spring5入门(二十二):@Nullable

@Nullable注解

  • 注解用在方法上面,方法返回值可以为空
@Nullable
public String add(){
}
  • 注解使用在方法参数里面,方法参数可以为空
public String add(@Nullable String id){
}
  • 注解使用在属性上面,属性值可以为空
@Nullable
private String id;

GenericApplicationContext

  • 简介
spring5容器支持函数式风格
  • 案例1
# 实体类
public class User {
}
# 测试方法
@Test
public void testGenericApplicationContext1() {
//1 创建GenericApplicationContext对象
GenericApplicationContext context = new GenericApplicationContext();
//2 调用context的方法对象注册
context.refresh();
context.registerBean(User.class, () -> new User());
//3 获取在spring注册的对象
User user = (User)context.getBean("com.atguigu.spring5.test.User");
System.out.println(user);
}
# 控制台
org.example.demo10.test.User@55182842
  • 案例2
# 实体类
public class User {
}
# 测试方法
@Test
public void testGenericApplicationContext2() {
//1 创建GenericApplicationContext对象
GenericApplicationContext context = new GenericApplicationContext();
//2 调用context的方法对象注册
context.refresh();
context.registerBean("user1",User.class,() -> new User());
//3 获取在spring注册的对象
User user = (User)context.getBean("user1"); // 由于这种方式获取bean,不是根据类名小写,而是在第2步指定
System.out.println(user);
}
# 控制台
org.example.demo10.test.User@55182842
posted @   DogLeftover  阅读(232)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示