欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring的基本使用方法

程序员文章站 2022-07-06 15:22:08
最开始的基础:Dao层如下:package com.zzx.dao;public interface UserDao { void adds();}Dao实现类:package com.zzx.dao.impl;import com.zzx.dao.UserDao;public class UserDaoImpl implements UserDao { @Override public void adds() { System.out.printl...

最开始的基础:

Dao层如下:

package com.zzx.dao;
public interface UserDao {
    void adds();
}

Dao实现类:

package com.zzx.dao.impl;
import com.zzx.dao.UserDao;
public class UserDaoImpl implements UserDao {
    @Override
    public void adds() {
        System.out.println("Spring学习的第一天.对于Dao层的调用");
    }
}

Service层:

package com.zzx.service;
public interface UserService {
    void add();
}

Service实现类:

package com.zzx.service.impl;
import com.zzx.service.UserService;
public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("这是Spring第一天对于UserService的调用");
    }
}

配置application.xml:

<?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">

    <!-- IOC 控制符反转,将创建对象的动作交给Spring容器 -->
    <!-- 配置文件就是spring的容器 -->
    <!-- 在此处配置需要由Spring创建对象的类
        id 创建后的对象名,后续需要使用该id从容器获得对象
        class 要创建对象的类路径
    -->
    <bean id="userService" class="com.zzx.service.impl.UserServiceImpl"></bean>
    <bean id="userDao" class="com.zzx.dao.impl.UserDaoImpl"></bean>


</beans>

最后写一个实现类,来测试代码是否正确:

package com.zzx.test;

import com.zzx.dao.UserDao;
import com.zzx.model.User;
import com.zzx.service.DiService;
import com.zzx.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {
    @Test
    public void 从容器获得对象(){
        //1.获得对象
        String path = "application.xml";        //此处的路径以src为根
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(path);

        //2.从容器获得对象,参数是xml中的id
        UserService userService =(UserService) app.getBean("userService");

        //3.使用对象
        userService.add();
    }
}

但是我们在日常开发中,不会这么写,那么就再次加大一个难度点:
dao层:

package com.zzx.dao;

public interface DI {
    void add();
}

dao的实现类:

package com.zzx.dao.impl;


import com.zzx.dao.DI;

public class DiDaoImpl implements DI {
    @Override
    public void add() {
        System.out.println("Spring第一天,注入实现");
    }
}

Service层:

package com.zzx.service;

public interface DiService {
    void add();
}

Service的实现类:

package com.zzx.service.impl;

import com.zzx.dao.DI;
import com.zzx.service.DiService;

public class DiServiceImpl implements DiService {
     private DI di;

    //提供属性的set方法
    public void setDi(DI di) {
        this.di = di;
    }

    @Override
    public void add() {
        System.out.println("暗黑三等奖爱啥啥");
        di.add();
    }
}

xml配置如下:

<?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">

    <!-- IOC 控制符反转,将创建对象的动作交给Spring容器 -->
    <!-- 配置文件就是spring的容器 -->
    <!-- 在此处配置需要由Spring创建对象的类
        id 创建后的对象名,后续需要使用该id从容器获得对象
        class 要创建对象的类路径
    -->
    <bean id="diService" class="com.zzx.service.impl.DiServiceImpl">
        <property name="di" ref="diDao"></property>
    </bean>

    <bean id="diDao" class="com.zzx.dao.impl.DiDaoImpl"></bean>


</beans>

测试类如下:

  @Test
    public void Di注入(){
        //1.获得对象
        String path = "application1.xml";
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(path);

        //2.从容器获得对象,参数是xml中的id
        DiService diService = (DiService) app.getBean("diService");
        System.out.println(diService);
        //3.使用对象
        diService.add();
    }

如果我们想给各种类型的属性赋值(model代码如下):

package com.zzx.model;

public class User {
    private int id;
    private Integer age;
    private String name;

    public User(int id) {
        this.id = id;
    }

    public User(Integer age) {
        this.age = age;
    }

    public User(String name) {
        this.name = name;
    }

    public User(int id, Integer age) {
        this.id = id;
        this.age = age;
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public User(Integer age, String name) {
        this.age = age;
        this.name = name;
    }

    public User(int id, Integer age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

xml赋值写法如下:

<?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">

    <!--<property> 标签专用于 set方法注入-->
    <!--<constructor-arg>标签专用于 构造方法注入
        type : 构造方法中参数的类型
        index : 构造方法中参数的下标 , 从0开始
        name : 构造方法中参数的变量名
        value : 参数值 - 基本类型 - 特殊的,给String赋值使用value
        ref  : 参数值  - 引用类型
     ps: 当类中属性类型是int等基本类型时,type直接写基本类型
         当类中属性类型是包装类类型时,type需要写包装类全路径
    -->
    <bean id="user" class="com.zzx.model.User">
        <constructor-arg type="int" index="0" value="18"></constructor-arg>
        <constructor-arg type="java.lang.Integer" index="1" value="28"></constructor-arg>
        <constructor-arg index="2" name="name" value="张三"></constructor-arg>
    </bean>

    <bean id="users" class="com.zzx.model.User">
        <constructor-arg index="0" name="id" value="18" ></constructor-arg>
        <constructor-arg index="1" name="age" value="28"></constructor-arg>
        <constructor-arg index="2" name="name" value="李四"></constructor-arg>
    </bean>

    <bean id="user1" class="com.zzx.model.User">
        <constructor-arg index="0" name="age" value="89"></constructor-arg>
        <constructor-arg index="1" name="name" value="王五"></constructor-arg>
    </bean>
</beans>

但是.这种赋值方法还不够完美,那我们就需要换一种更加规范的写法来给对象赋值了:
首先model类的代码如下:

package com.zzx.model;

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

public class CollectBean {
    /**
     * 案例演示中即是使用的set方法注入.
     * set方法注入 – 注入引用类型
     * set方法注入 – 基本数据类型
     * set方法注入 – 数组
     * set方法注入 – List
     * set方法注入 – Set
     * set方法注入 – Map
     */
    private int base;
    private User user;
    private int[] baseArr;
    private User[] refArr[];
    private List<User> list;
    private Set<User> set;
    private Map<String,Integer> map;

    public int getBase() {
        return base;
    }

    public void setBase(int base) {
        this.base = base;
    }

    public User getUser() {
        return user;
    }

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

    public int[] getBaseArr() {
        return baseArr;
    }

    public void setBaseArr(int[] baseArr) {
        this.baseArr = baseArr;
    }

    public User[][] getRefArr() {
        return refArr;
    }

    public void setRefArr(User[][] refArr) {
        this.refArr = refArr;
    }

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

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

    public Set<User> getSet() {
        return set;
    }

    public void setSet(Set<User> set) {
        this.set = set;
    }

    public Map<String, Integer> getMap() {
        return map;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public CollectBean(int base, User user, int[] baseArr, User[][] refArr, List<User> list, Set<User> set, Map<String, Integer> map) {
        this.base = base;
        this.user = user;
        this.baseArr = baseArr;
        this.refArr = refArr;
        this.list = list;
        this.set = set;
        this.map = map;
    }

    public CollectBean() {}

    @Override
    public String toString() {
        return "CollectBean{" +
                "base=" + base +
                ", user=" + user +
                ", baseArr=" + Arrays.toString(baseArr) +
                ", refArr=" + Arrays.toString(refArr) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                '}';
    }
}

然后使用xml来给model雷电的各种属性赋值,XML代码如下:

<?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">

<bean id="collectBean" class="com.zzx.model.CollectBean">
      <!--
          set方法注入,使用<property>标签
          name : 属性名
          value: 基本数据类型赋值
          ref :  引用数据类型赋值,引用的是bean的id
          <array>标签,定义数组的值
       -->
<property name="base" value="28"></property>
<property name="user" ref="user"></property>
<property name="baseArr">
      <array>
            <value>130</value>
            <value>131</value>
            <value>132</value>
      </array>
</property>
<property name="refArr">
      <array>
            <ref bean="user"></ref>
            <ref bean="users"></ref>
      </array>
</property>
<property name="list">
      <list>
            <ref bean="user"/>
            <ref bean="users"/>
            <ref bean="user"/>
            <ref bean="users"/>
      </list>
</property>
<property name="set">
      <set>
            <ref bean="user"/>
            <ref bean="user"/>
            <ref bean="users"/>
            <ref bean="users"/>
      </set>
</property>
<property name="map">
      <map>
           <entry key="张三" value="25"></entry>
            <entry key="李四" value="28"></entry>
      </map>
</property>
</bean>
      <bean id="user" class="com.zzx.model.User">
            <constructor-arg name="age" value="56"></constructor-arg>
            <constructor-arg name="name" value="张三"></constructor-arg>
      </bean>
      <bean id="users" class="com.zzx.model.User">
            <constructor-arg name="age" value="66"></constructor-arg>
            <constructor-arg name="name" value="李四"></constructor-arg>
      </bean>
</beans>

本文地址:https://blog.csdn.net/a1422655169/article/details/114274425