javaEE高阶---Spring 更简单的读取和存储对象
创始人
2024-01-28 18:54:33
0

一 : 引言

经过前面的学习,我们已经可以实现基本的 Spring 读取和存储对象的操作了,但在操作的过程中我们发现读取和存储对象并没有想象中的那么“简单”,所以接下来我们要学习更加简单的操作 Bean 对象的方法 .

二 : 存储Bean对象

2.1 使用类注解

在这里插入图片描述

在这里插入图片描述

示例代码 :
在这里插入图片描述

在这里插入图片描述

运行结果 :
在这里插入图片描述

在这里插入图片描述
接下来我们对类注解进行一一验证 :

import com.component.UserComponent;
import com.configuration.UserConfiguration;
import com.controller.UserController;
import com.respository.UserRepository;
import com.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
//启动类
public class Test {public static void main(String[] args) {//1.得到Spring上下文对象ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");//2.从Spring获取bean对象[使用注解,默认命名规则:类名首字母小写]UserService userService = (UserService) context.getBean("userService");//3.使用beanuserService.doService();UserRepository userRepository = (UserRepository) context.getBean("userRepository");userRepository.doRepository();UserComponent userComponent = (UserComponent) context.getBean("userComponent");userComponent.doComponent();UserConfiguration userConfiguration = (UserConfiguration) context.getBean("userConfiguration");userConfiguration.doConfiguration();UserController userController = (UserController) context.getBean("userController");userController.doUserController();}
}

代码结构基本一致 :

在这里插入图片描述
运行结果 :

在这里插入图片描述
在这里插入图片描述

Q1 : 5大类注解的关系 ?

在这里插入图片描述

@Controller、@Service、@Repository、@Configuration 都是基于@Component,它们的作用都是将Bean存储到Spring 中 .

Q2 : Bean的命名规则 ?

在这里插入图片描述

在这里插入图片描述

查看Bean命名的源码 :

在这里插入图片描述

在这里插入图片描述

2.2 使用方法注解

2.2.1 演示

在这里插入图片描述
在这里插入图片描述

方法注解Bean命名规则 :

在这里插入图片描述

2.2.2注意事项

在这里插入图片描述

2.2.3 @Bean重命名

方法一:@Bean(name="xxxxx")
package com.bean;import com.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;@Controller
public class UserBean {@Bean(name = "computerUser")public User user1() {User user = new User();user.setAge(18);user.setName("百乔");return user;}
}
import com.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test2 {public static void main(String[] args) {//1.得到Spring上下文对象ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");//2.从Spring获取bean对象[使用注解,默认命名规则:类名首字母小写]User user = (User) context.getBean("computerUser");//3.使用beanSystem.out.println(user);}
}

在这里插入图片描述

方法二:@Bean("xxxxx")
package com.bean;import com.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;@Controller
public class UserBean {@Bean("computerUser")public User user1() {User user = new User();user.setAge(18);user.setName("百乔");return user;}
}
import com.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test2 {public static void main(String[] args) {//1.得到Spring上下文对象ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");//2.从Spring获取bean对象[使用注解,默认命名规则:类名首字母小写]User user = (User) context.getBean("computerUser");//3.使用beanSystem.out.println(user);}
}

在这里插入图片描述

方法三 : @Bean("xxxx","xxxxxx")
package com.bean;import com.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;@Controller
public class UserBean {@Bean(name = {"computerUser","blackboardUser"})public User user1() {User user = new User();user.setAge(18);user.setName("百乔");return user;}
}
import com.model.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test2 {public static void main(String[] args) {//1.得到Spring上下文对象ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");//2.从Spring获取bean对象[使用注解,默认命名规则:类名首字母小写]User user = (User) context.getBean("blackboardUser");//3.使用beanSystem.out.println(user);}
}

在这里插入图片描述
在这里插入图片描述

三 : 获取Bean对象(依赖注入/对象装配)

在 Spring 中实现依赖注入的常见方式有以下 3 种:

  • 属性注入(Field Injection);
  • Setter 注入(Setter Injection);
  • 构造方法注入(Constructor Injection).

3.1 属性注入

属性注入是我们最熟悉,也是日常开发中使用最多的一种注入方式.
package com.controller;import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class UserController {//1.属性注入@Autowired // 自动装配private UserService userService;public void doUserController() {System.out.println("Do UserController!");userService.doService();}
}
import com.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test3 {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");UserController userController = (UserController) context.getBean("userController");userController.doUserController();}
}

在这里插入图片描述

在这里插入图片描述

3.2 Setter注入

package com.controller;import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class UserController {//2.Setter注入private UserService userService;@Autowiredpublic void setUserService(UserService userService) {this.userService = userService;}public void doUserController() {System.out.println("Do UserController!");userService.doService();}
}
import com.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test3 {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");UserController userController = (UserController) context.getBean("userController");userController.doUserController();}
}

在这里插入图片描述

在这里插入图片描述

3.3 构造方法注入

package com.controller;import com.model.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class UserController {//3.构造方法注入private  UserService userService;@Autowiredpublic UserController(UserService userService) {this.userService = userService;}public void doUserController() {System.out.println("Do UserController!");userService.doService();}
}
import com.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test3 {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");UserController userController = (UserController) context.getBean("userController");userController.doUserController();}
}

在这里插入图片描述

以下是关于构造方法注入的一些注意事项 :

1.当当前类只有一个构造方法时,@Autowired可以省略;当存在多个构造方法时,@Autowired注解不能省略.

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.可以在一个构造方法里注入多个Autowired对象,且当仅有1个构造方法时,@Autowired可以省略.

package com.controller;import com.configuration.UserConfiguration;
import com.model.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;@Controller
public class UserController {//3.构造方法注入private UserService userService;private UserConfiguration userConfiguration;@Autowiredpublic UserController(UserService userService,UserConfiguration userConfiguration) {this.userService = userService;this.userConfiguration = userConfiguration;}public void doUserController() {System.out.println("Do UserController!");userService.doService();userConfiguration.doConfiguration();}
}
import com.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test3 {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");UserController userController = (UserController) context.getBean("userController");userController.doUserController();}
}

在这里插入图片描述

3.答:当有多个构造方法的时候,加了@Autowired 的构造方法才会执行,并且构造方法中的参数(对象)必须要存在于Spring容器中,否则就会报错.

典型错误 :

在这里插入图片描述
构造方法注入的优点 :

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
Spring4.2之前推荐使用Setter注入方式 , 原因是其符合单一性原则 ; Spring4.2之后则推荐使用构造方法注入 . 有意思的是Spring官方在源代码中更多使用的 , 却是属性注入的方式 , 因为这种方法确实非常简单 .

3.2 @Resource

在进行类注入时,除了可以使用 @Autowired 关键字之外,我们还可以使用 @Resource 进行注入 , 示例如下 :

在这里插入图片描述

@Resource注解不能使用在构造方法实现上 .

@Resource和@Autowired的区别

在这里插入图片描述

在这里插入图片描述

先看@Resource :

package com.controller;
import com.model.User;
import org.springframework.stereotype.Controller;import javax.annotation.Resource;@Controller
public class SheepController {@Resource(name = "user1")private User user;public void doSheepController() {System.out.println("别看我只是一只羊");System.out.println(user);}
}
import com.controller.SheepController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test3 {public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("spring-config.xml");SheepController sheepController =(SheepController) context.getBean("sheepController");sheepController.doSheepController();}
}

在这里插入图片描述

解释 :
在这里插入图片描述

如果使用@Autowired , 需要配合另一个Spring的注解@qualifier .

在这里插入图片描述

还可以配合@Primary注解 :

在这里插入图片描述

在这里插入图片描述

以下是一道综合练习题 , 大家可以到我的码云(SpringBoot_Demo3)查看答案 :

在这里插入图片描述
在这里插入图片描述

本节内容结束 !

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
一帆风顺二龙腾飞三阳开泰祝福语... 本篇文章极速百科给大家谈谈一帆风顺二龙腾飞三阳开泰祝福语,以及一帆风顺二龙腾飞三阳开泰祝福语结婚对应...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...