• Spring的注入


    目录

    一、Spring的概念

    二、各种数据类型的注入

    (1)studentService

    (2)applicationContext.xml(Sring核心配置文件)

    (3)测试

    三、注入null或者empty类型的数据

    (1)UserService

    (2)applicationContext.xml(Spring核心配置文件)

    (3)测试


    一、Spring的概念

    Spring是一个开源框架,为简化企业级开发而生。它以IOC(控制反转)和AOP(面向切面)为思想内核,提供了控制层SpringMVC、数据层SpringData、服务层事务管理等众多技术,并可以整合众多第三方框架。

    Spring将很多复杂的代码变得优雅简洁,有效的降低代码的耦合度,极大的方便项目的后期维护、升级和扩展。

    Spring官网地址:Spring | Home

    这里的IOC控制反转的意思是:本来我们创建对象是需要自己创建的,使用new,但是这有很大的缺点的,如下:

       (1)浪费资源:StudentService调用方法时即会创建一个对象,如果不断调用方法则会创建大量StudentDao对象。

       (2)代码耦合度高:假设随着开发,我们创建了StudentDao另一个更加完善的实现类StudentDaoImpl2,如果在StudentService中想使用StudentDaoImpl2,则必须修改源码。

    所以IOC其实就是我们将创建对象的权利交给框架,让框架帮我们创建对象

    二、各种数据类型的注入

    什么事注入呢,这里我们讲的注入其实就是在你创建一个对象后,你是不是要给他赋值呢,所以你可以简单地理解成,注入就是给创建的对象赋值。 

    (1)studentService

    1. package com.gq.pojo;
    2. import java.util.List;
    3. import java.util.Map;
    4. import java.util.Properties;
    5. import java.util.Set;
    6. public class StudentService {
    7. //数组练习
    8. private student[] arrayStudent;
    9. //list练习
    10. private List studentList;
    11. //set练习
    12. private Set studentSet;
    13. //Map练习
    14. private Map studentMap;
    15. //Properties属性练习
    16. private Properties properties;
    17. //各种各样的输出方法来判断是哪种类别的
    18. public void arrayTest(){
    19. System.out.println("array练习");
    20. System.out.println(this.getArrayStudent());
    21. }
    22. public void ListTest(){
    23. System.out.println("List练习");
    24. System.out.println(this.getStudentList());
    25. }
    26. public void setTest(){
    27. System.out.println("Set练习");
    28. System.out.println(this.getStudentSet());
    29. }
    30. public void mapTest(){
    31. System.out.println("map练习");
    32. System.out.println(this.getStudentMap());
    33. }
    34. public void proTest(){
    35. System.out.println("properties练习");
    36. System.out.println(this.getProperties());
    37. }
    38. public student[] getArrayStudent() {
    39. return arrayStudent;
    40. }
    41. public void setArrayStudent(student[] arrayStudent) {
    42. this.arrayStudent = arrayStudent;
    43. }
    44. public List getStudentList() {
    45. return studentList;
    46. }
    47. public void setStudentList(List studentList) {
    48. this.studentList = studentList;
    49. }
    50. public Set getStudentSet() {
    51. return studentSet;
    52. }
    53. public void setStudentSet(Set studentSet) {
    54. this.studentSet = studentSet;
    55. }
    56. public Map getStudentMap() {
    57. return studentMap;
    58. }
    59. public void setStudentMap(Map studentMap) {
    60. this.studentMap = studentMap;
    61. }
    62. public Properties getProperties() {
    63. return properties;
    64. }
    65. public void setProperties(Properties properties) {
    66. this.properties = properties;
    67. }
    68. }

    (2)applicationContext.xml(Sring核心配置文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://www.springframework.org/schema/beans
    5. http://www.springframework.org/schema/beans/spring-beans.xsd">
    6. <bean name="student1" class="com.gq.pojo.student">
    7. <property name="id" value="1">property>
    8. <property name="name" value="zhansgan">property>
    9. bean>
    10. <bean name="student2" class="com.gq.pojo.student">
    11. <property name="name" value="lisi">property>
    12. <property name="id" value="2">property>
    13. bean>
    14. <bean name="StudentService" class="com.gq.pojo.StudentService">
    15. <property name="arrayStudent">
    16. <list>
    17. <ref bean="student1">ref>
    18. <ref bean="student2">ref>
    19. <bean class="com.gq.pojo.student">
    20. <property name="id" value="3">property>
    21. <property name="name" value="wangwu">property>
    22. bean>
    23. list>
    24. property>
    25. <property name="studentList">
    26. <list>
    27. <ref bean="student1"/>
    28. <ref bean="student2"/>
    29. <bean class="com.gq.pojo.student">
    30. <property name="name" value="uiui">property>
    31. <property name="id" value="11">property>
    32. bean>
    33. list>
    34. property>
    35. <property name="studentSet">
    36. <set>
    37. <ref bean="student1">ref>
    38. <ref bean="student2">ref>
    39. <bean class="com.gq.pojo.student">
    40. <property name="id" value="21">property>
    41. <property name="name" value="setset">property>
    42. bean>
    43. set>
    44. property>
    45. <property name="studentMap">
    46. <map>
    47. <entry key="map1" value-ref="student1">entry>
    48. <entry key="map2" value-ref="student2">entry>
    49. <entry key="map3">
    50. <bean class="com.gq.pojo.student">
    51. <property name="name" value="maptest">property>
    52. <property name="id" value="33">property>
    53. bean>
    54. entry>
    55. map>
    56. property>
    57. <property name="properties">
    58. <props>
    59. <prop key="prop1">i am 1prop>
    60. <prop key="prop2">i am 2prop>
    61. props>
    62. property>
    63. bean>
    64. beans>

    (3)测试

    1. @Test
    2. public void t2(){
    3. ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    4. StudentService service=context.getBean("StudentService",StudentService.class);
    5. service.arrayTest();
    6. service.ListTest();
    7. service.mapTest();
    8. service.setTest();
    9. service.proTest();
    10. }

     关于乱码问题,只需要将编码格修改成UTF-8就可以了,具体的修改我就不多说了,大家也可以自己查找资料,不是很难的。

    三、注入null或者empty类型的数据

    (1)UserService

    1. public class UserService {
    2. private List list;
    3. public void test(){
    4. System.out.println("i am listTest");
    5. }
    6. public UserService(List list) {
    7. this.list = list;
    8. }
    9. public UserService() {
    10. }
    11. public List getList() {
    12. return list;
    13. }
    14. public void setList(List list) {
    15. this.list = list;
    16. }
    17. }

    (2)applicationContext.xml(Spring核心配置文件)

    1. <bean name="UserService" class="com.gq.pojo.UserService">
    2. <property name="list">
    3. <list>
    4. <value>i am first persionvalue>
    5. <value>value>
    6. <null>null>
    7. <value>i am last persionvalue>
    8. list>
    9. property>
    10. bean>

    (3)测试

    1. @Test
    2. public void t3(){
    3. ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
    4. UserService userService=context.getBean("UserService",UserService.class);
    5. List list = userService.getList();
    6. System.out.println(list);
    7. }

     

  • 相关阅读:
    创建一个GO模块
    无线蓝牙耳机什么牌子好?三百内最好的无线耳机推荐
    运放:运放输入电压
    MS17010(永恒之蓝)漏洞实战
    java计算机毕业设计教学质量评价系统源程序+mysql+系统+lw文档+远程调试
    [微前端]微前端的简单介绍
    【Python脚本进阶】2.2、构建一个SSH僵尸网络(中):用Pxssh暴力破解SSH密码
    问 ChatGPT 关于 GPT 的事情:数据准备篇
    odoo13笔记点
    【Unity编辑器扩展】| 顶部菜单栏扩展 MenuItem
  • 原文地址:https://blog.csdn.net/gaoqiandr/article/details/134305220