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

java 复制代码
package com.gq.pojo;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class StudentService {
    //数组练习
    private student[] arrayStudent;
    //list练习
    private List<student> studentList;
    //set练习
    private Set<student> studentSet;
    //Map练习
    private Map<String,student> studentMap;
    //Properties属性练习
    private Properties properties;

    //各种各样的输出方法来判断是哪种类别的
    public void arrayTest(){
        System.out.println("array练习");
        System.out.println(this.getArrayStudent());
    }
    public void ListTest(){
        System.out.println("List练习");
        System.out.println(this.getStudentList());
    }
    public void setTest(){
        System.out.println("Set练习");
        System.out.println(this.getStudentSet());
    }
    public void mapTest(){
        System.out.println("map练习");
        System.out.println(this.getStudentMap());
    }
    public void proTest(){
        System.out.println("properties练习");
        System.out.println(this.getProperties());
    }

    public student[] getArrayStudent() {
        return arrayStudent;
    }

    public void setArrayStudent(student[] arrayStudent) {
        this.arrayStudent = arrayStudent;
    }

    public List<student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<student> studentList) {
        this.studentList = studentList;
    }

    public Set<student> getStudentSet() {
        return studentSet;
    }

    public void setStudentSet(Set<student> studentSet) {
        this.studentSet = studentSet;
    }

    public Map<String, student> getStudentMap() {
        return studentMap;
    }

    public void setStudentMap(Map<String, student> studentMap) {
        this.studentMap = studentMap;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}

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

html 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--配置不同的JavaBean,交由 IOC 容器进行管理-->
    <!--注入简单数据类型-->
    <bean name="student1" class="com.gq.pojo.student">
        <property name="id" value="1"></property>
        <property name="name" value="zhansgan"></property>
    </bean>
    <bean name="student2" class="com.gq.pojo.student">
        <property name="name" value="lisi"></property>
        <property name="id" value="2"></property>
    </bean>

    <bean name="StudentService" class="com.gq.pojo.StudentService">
        <!-- 注入数组类型的数据-->
        <property name="arrayStudent">
             <list>
                <ref bean="student1"></ref>
                 <ref bean="student2"></ref>
                 <bean class="com.gq.pojo.student">
                     <property name="id" value="3"></property>
                     <property name="name" value="wangwu"></property>
                 </bean>
             </list>
        </property>
        <!--注入List类型的数据 -->
        <property name="studentList">
            <list>
                <ref bean="student1"/>
                <ref bean="student2"/>
                <bean class="com.gq.pojo.student">
                    <property name="name" value="uiui"></property>
                    <property name="id" value="11"></property>
                </bean>

            </list>
        </property>
        <!--注入Set数据-->
        <property name="studentSet">
            <set>
                <ref bean="student1"></ref>
                <ref bean="student2"></ref>
                <bean class="com.gq.pojo.student">
                    <property name="id" value="21"></property>
                    <property name="name" value="setset"></property>
                </bean>
            </set>
        </property>
        <!--注入map数据-->
        <property name="studentMap">
            <map>
                <entry key="map1" value-ref="student1"></entry>
                <entry key="map2" value-ref="student2"></entry>
                <entry key="map3">
                    <bean class="com.gq.pojo.student">
                        <property name="name" value="maptest"></property>
                        <property name="id" value="33"></property>
                    </bean>
                </entry>
            </map>
        </property>
        <!--注入Properties类型数据-->
        <property name="properties">
            <props>
                <prop key="prop1">i am 1</prop>
                <prop key="prop2">i am 2</prop>

            </props>
        </property>
    </bean>





</beans>

(3)测试

java 复制代码
@Test
    public void t2(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentService service=context.getBean("StudentService",StudentService.class);
        service.arrayTest();
        service.ListTest();
        service.mapTest();
        service.setTest();
        service.proTest();

    }

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

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

(1)UserService

java 复制代码
public class UserService {
   private List<String>  list;

   public void test(){
       System.out.println("i am listTest");
   }

    public UserService(List<String> list) {
        this.list = list;
    }

    public UserService() {
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }
}

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

html 复制代码
<bean name="UserService" class="com.gq.pojo.UserService">
        <property name="list">
            <list>
                <value>i am first persion</value>
                <value></value><!--空值-->
                <null></null><!--空字符串-->
                <value>i am last persion</value>
            </list>
        </property>
    </bean>

(3)测试

java 复制代码
 @Test
    public void t3(){
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService=context.getBean("UserService",UserService.class);
        List<String> list = userService.getList();
        System.out.println(list);

    }
相关推荐
666786661 分钟前
Mysql高级篇(中)—— SQL优化
linux·运维·服务器·数据库·sql·mysql
十年人间~2 分钟前
mysql等保数据库命令
数据库·mysql
Amagi.6 分钟前
Redis的内存淘汰策略
数据库·redis·mybatis
hai41174196211 分钟前
mysql 与postgresql 的区别(gpt4)
数据库·mysql·postgresql
Freak嵌入式16 分钟前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
java·开发语言·数据结构·python·接口·抽象基类
知识分享小能手21 分钟前
mysql学习教程,从入门到精通,SQL 删除数据(DELETE 语句)(19)
大数据·开发语言·数据库·sql·学习·mysql·数据开发
前端小马26 分钟前
解决IDEA出现:java: 程序包javax.servlet不存在的问题
java·servlet·intellij-idea
白总Server36 分钟前
MongoDB解说
开发语言·数据库·后端·mongodb·golang·rust·php
冰镇毛衣42 分钟前
2.4 数据库表字段约束
数据库·sql·mysql
&木头人&1 小时前
oracle select字段有子查询会每次执行子查询吗
数据库·oracle