软件编程
位置:首页>> 软件编程>> java编程>> Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

作者:熬夜磕代码丶  发布时间:2022-06-10 00:35:24 

标签:Spring,注解,存储,读取

一、存储Bean对象

Spring使用注解存储和读取对象详解

之前我们存储Bean时,需要在spring-config.xml中添加bean注册才行,这样的方式并不简单。我们要想更简单的存储和读取对象的核心是使用注解

1.使用类注解(五大类注解):

@Controller:控制器,验证用户请求的数据正确性(安保系统)
@Service:服务层,编排和调度具体执行方法的(客服中心)
@Repository:持久层,和数据库进行交互,等同于DAO(Data Access Object) 数据访问层
@Component:组件(工具类)
@Configuration:配置项(配置项目中的一些配置)

2.方法注解:

@Bean路径

配置扫描

要想将对象成功存储到Spring中,物品们需要配置一下存储对象的扫描包路径,只有被配置的包下的所有类,添加了注解才能被正确的识别并保存到Spring中,需要在spring-config.xnl添加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:content="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
   <content:component-scan base-package="com.zd.demo"></content:component-scan>
</beans>

Spring使用注解存储和读取对象详解

这步是十分重要的,要是不是在配置扫描包下的类对象,即使加了注解,也是不能存储到Spring中的

添加注解存储Bean对象

使用 @Controller 存储 bean 的代码如下所示:

@Controller //将对象存储到Spring中
public class StudentController {
   public void hello() {
       System.out.println("hello Im student");
   }
}

读取StudentController对象

public static void main(String[] args) {
       //得到Spring上下文
       ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
       //得到bean
       StudentController studentController =
               context.getBean("studentController",StudentController.class);
       //调用bean方法
       studentController.hello();
   }

Spring使用注解存储和读取对象详解

使用 @Service 存储 bean 的代码如下所示:

@Service
public class StudentService {
   public void hello() {
       System.out.println("hello Im studentService");
   }
}

获取Bean对象:

public static void main(String[] args) {
       //得到Spring上下文
       ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
       //得到bean
       StudentService studentService =
               context.getBean("studentService",StudentService.class);
       //调用bean方法
       studentService.hello();
   }

Spring使用注解存储和读取对象详解

其他几个类注解的使用方法都是一致的,在这里就不一一演示了

注解使用范围

1.是否可以与component-scan一起使用?

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

我们可以发现是可以一起使用的

2.五大类注解可以不再component-scan包下吗?
不可以

3.component-scan下的类,没有加五大类注解,可以存储到Spring吗?
不可以

4.componemt-scan下的所有子包下的类只要加了五大类注解,可以存储到Spring吗?
子包下的类只要加了五大类注解,同样可以存储到Spring中

Bean的命名

Spring使用注解存储和读取对象详解

我们在获取Bean对象时,传入名称时,一般分为两种情况:
默认情况下:使用原类名首字母小写就能读取到Bean对象
特殊情况:原类名如果首字母和第二个字母都是大写的情况,那么使用原类名获取

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

五大类注解的关系

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

可以认为@Controller / @Service / @Repository /@Configuration都是@Component的"子类",都是针对于@Component的一个扩展

为什么需要五大类注解?

我们可以发现,只要我们在类上加了注解,都可以获取到Bean对象,为什么需要这么多的类注解呢?
为了让程序员看到注解之后一眼就知道当前类的作用

JavaEE标准分层:
1.控制层(Controller)
2.服务层(Service)
3.数据持久层(Dao)

Spring使用注解存储和读取对象详解

二、方法注解@Bean

五大类注解是添加到某个类上的,而方法注解是放到方法上的

Spring使用注解存储和读取对象详解

我们首先准备一个实体类,然后使用方法注解@Bean将对象存储到Spring容器中

public class UserBeans {
   @Bean
   public static User getUser() {
       User user = new User();
       user.setUid(1);
       user.setUsername("张三");
       user.setPassword("123456");
       return user;
   }
}

然后从Spring容器中获取对象

public static void main(String[] args) {
       ApplicationContext context =
               new ClassPathXmlApplicationContext("spring-config.xml");
       User user = context.getBean("user",User.class);
       System.out.println(user);
   }

Spring使用注解存储和读取对象详解

我们发现使用的时候报错了,没有名为user的Bean对象,出现这种现象一共有两大原因:

1.@Bean命名规则与五大类注解的命名规则不同,@Bean命名规则,默认@Bean存储的对象名称 == 方法名

Spring使用注解存储和读取对象详解

2.@Bean注解必须要搭配五大类注解一起使用(Spring为了提升性能所做的规定)

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

现在就可以正常获取到Bean对象了

Bean重命名

我们上述通过方法名获取Bean对象太抽象了,我们可以通过设置name属性给Bean对象进行重命名操作,如下述操作:

Spring使用注解存储和读取对象详解

我们给Bean起一个user的名字

Spring使用注解存储和读取对象详解

我们可以发现就可以通过这个重名获取了。
我们这里的重命名可以起多个名字,因为我们Spring容器中允许将同一类型的对象,存储到容器多份

Spring使用注解存储和读取对象详解

当@Bean使用了重命名之后,使用方法名是否还能获取到对象?
不能,当@Bean对象重命名之后,默认的使用方法名获取的方式就不能使用了

Spring使用注解存储和读取对象详解

三、对象注入

获取Bean对象也称之为对象装配,就是将对象取出来放到某个类中,有时候也称之对象注入
对象注入的实现方法有以下三种:
1.属性注入
2.Setter注入
3.构造方法注入

属性注入

属性注入是使用@AutoWired注解实现的

@Component
public class Group {
   @Autowired
   private User user;

public User getUser() {
       return user;
   }
   public void setUser(User user) {
       this.user = user;
   }
}

Spring使用注解存储和读取对象详解

这样就可以实现属性注入了,虽然属性注入实现简单、使用简单,但它有以下缺点:

Spring使用注解存储和读取对象详解

我么将鼠标放在注解上,会提示我们属性注入不推荐使用

1.无法注入一个不可变对象(final 修饰的对象)

Spring使用注解存储和读取对象详解

final修饰的对象要么直接复制,要么在构造方法中赋值。放我们属性注入时,上述两种都不满足,所以就注入失败了

2.通用性,属性注入只能在IoC容器中使用,其他容器中不支持

3.违背单一设计原则。简单理解就是注入方式越简单,滥用的概率越大,出现违背单一职责的概率也越大

Setter注入

setter注入也是使用@Autowired注解实现

@Component
public class Group {

private  User user;
   @Autowired
   public void setUser(User user) {
       this.user = user;
   }
   public User getUser() {
       return user;
   }

}

Spring使用注解存储和读取对象详解

我们使用Setter注入也可以成功存储取出,Setter注入符合单一职责的设计原理,但也有以下缺点:
1.不能注入不可变对象

Spring使用注解存储和读取对象详解

2.注入的对象可能被修改,因为我们在任何事件都可以调用setXXX方法来改变注入的对象

构造方法注入

构造方法注入也是Spring官方推荐的注入方式:

public class Group {

private User user;

@Autowired
   public Group(User user) {
       this.user = user;
   }
}    

Spring使用注解存储和读取对象详解

如果当前类只有一个构造方法的话,@Autowired注解可以省略

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

构造方法注入。有以下优点:
1.可注入不可变对象

Spring使用注解存储和读取对象详解

2.注入对象不会被修改
构造方法在对象创建时只会执行一次,因为不存在注入对象被随时修改的情况

3.完全初始化
构造方法是在对象创建之前之前的,当我们使用被注入的对象时,会被完全初始化

4.通用性强
支持各种框架

@Autowired 和 @Resource 的区别

在进行类注入时,除了可以使用@Autowired关键字,我们还可以使用@Resource进行注入

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

@Autowired 和 @Resource 的区别:

1.出身不同:@Autowired是Spring的注解,@Resource是JDK的注解
2.查找顺序不同:@Autowired 先根据类型再根据名称查询,而 @Resource 先根据名称再根据类型查询
3.支持参数不同,@Autowired支持一个,@Resource支持7个

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

4.依赖注入支持不同:@Autowired支持三种注入,而@Resource只支持属性注入和Setter注入

当同一类型多个Bean时会报错

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

再去获取对象就会出错,非唯一的Bean的对象,因为我们Spring中有user1和user2,不知道注入那个,解决方案有两个:

1.使用@Resource(name = &ldquo;XXX&rdquo;)

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

2.使用@Qualifier注解定义名称,搭配@Autowired注解使用

Spring使用注解存储和读取对象详解

Spring使用注解存储和读取对象详解

来源:https://blog.csdn.net/buhuisuanfa/article/details/130110318

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com