001 spring ioc(xml)

文章目录

UserController

java 复制代码
package com.aistart.controller;

import com.aistart.pojo.Student;
import com.aistart.service.UserService;
import com.aistart.service.impl.UserServiceImpl;


public class UserController {
    /*依赖抽象*/
    UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public String Login(Student student){


        boolean login = userService.login(student);

        if (login)return "yes";
        else return "no";
    }

}

UserMapperImpl

java 复制代码
package com.aistart.mapper.impl;

import com.aistart.mapper.UserMapper;
import com.aistart.pojo.Student;


public class UserMapperImpl implements UserMapper {
    @Override
    public int selectUser(Student student) {

        System.out.println("这里是查找是否有用户的mapper");

        return 0;
    }
}

School.java

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


public class School {

    private String schoolName;

    public School(String schoolName) {
        this.schoolName = schoolName;
    }

    public School() {
        System.out.println("school的无参被调用了");

    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("School{");
        sb.append("schoolName='").append(schoolName).append('\'');
        sb.append('}');
        return sb.toString();
    }
}

Student.java

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


public class Student {

    private String stuName;

    private Integer age;

    private School school;

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

    public Student() {

        System.out.println("student无参被调用了");
    }

    public Student(String stuName, Integer age) {
        this.stuName = stuName;
        this.age = age;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Student{");
        sb.append("stuName='").append(stuName).append('\'');
        sb.append(", age=").append(age);
        sb.append(", school=").append(school);
        sb.append('}');
        return sb.toString();
    }
}

UserServiceImpl.java

java 复制代码
package com.aistart.service.impl;

import com.aistart.mapper.UserMapper;
import com.aistart.mapper.impl.UserMapperImpl;
import com.aistart.pojo.Student;
import com.aistart.service.UserService;

public class UserServiceImpl implements UserService {
    UserMapper userMapper;

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public boolean login(Student student) {

//        具体事先
        System.out.println("这是login的service业务");

        /*判断各种*/
        int i = userMapper.selectUser(student);

        if (i==1){
            return true;
        }
        return false;
    }
}

ApplicationContext.xml

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


    <!--第二步,创建元数据-->

    <!--id就相当于这个对象的唯一标识,理解为这对象在容器中的"变量名"-->

    <!--bean中,调用默认的构造方法,去创建-->
    <bean id="alice" class="com.aistart.pojo.Student">
        <property name="stuName" value="Alice"/>
        <!--标签中,相当于键值对,name:value-->
        <!--name等同于属性名-->
        <!--value等同于注入的值
                                注意,不管加入什么类型都是""引起来的-->
        <property name="age" value="19"/>
        <!--这个标签,其实是执行了set方法去注入的-->
        <!--一定要有set方法-->

        <!--name   搭配value和ref
        value是简单类型,字面量能够赋值的
        ref是引入类型,是外部获取得另一个对象
        -->
        <property name="school" ref="ksdx"/>
        <!-- 这个bean的合作者和配置在这里 -->
    </bean>

    <bean id="sqxy" class="com.aistart.pojo.School">
        <!-- 这个bean的合作者和配置在这里 -->
        <property name="schoolName" value="小清华"></property>
    </bean>

    <bean id="ksdx" class="com.aistart.pojo.School">
        <!-- 这个bean的合作者和配置在这里 -->
        <property name="schoolName" value="呼呼睡觉"></property>
    </bean>

    <!-- 更多bean 定义在这里 -->


    <bean id="userController" class="com.aistart.controller.UserController">
        <property name="userService" ref="userService"></property>

    </bean>

    <bean id="userService" class="com.aistart.service.impl.UserServiceImpl">

        <property name="userMapper" ref="userMapper"></property>

    </bean>
    <bean id="userMapper" class="com.aistart.mapper.impl.UserMapperImpl"/>

</beans>

UserControllerTest.java

java 复制代码
package com.aistart.controller;

import com.aistart.pojo.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class UserControllerTest {

    @Test
    public void login() {

        /*利用spring容器获取controller对象*/

         ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

        UserController userController = (UserController) context.getBean("userController");

        String login = userController.Login(new Student());

        System.out.println(login);


    }
}

StudentTest.java

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

import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import static org.junit.Assert.*;

public class StudentTest {

    @Test
    public void test(){
        /*传统的注入方式*/


        /*1.构造器注入*/
        Student alice = new Student("Alice", 18);


        /*2.用set方法注入*/
        Student student = new Student();

        student.setStuName("Bob");
        student.setAge(19);


    }
    /*使用spring来创建对象*/
    @Test
    public void beanTest(){

        /*第三步,实例化一个容器*/
        /*无参被调用说明:容器不单单是加载xml,并且根据xml去创建对象,全都创建了*/
        /*创建的同时应该将值注入其中*/
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

        /*第四步,获取实例对象
        *
        * 参数:是xml中对象实例的id
        * 返回:类型都是Object
        * */
        Student alice =(Student) context.getBean("alice");
        Student alice1 =(Student) context.getBean("alice");

        /*在spring容器中,对象实例默认都是单例的*/
        System.out.println(alice1==alice);

        System.out.println(alice);


    }
}

pom.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.aistart</groupId>
    <artifactId>Spring_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <!--第一步,导入环境-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.30</version>
        </dependency>
    </dependencies>

</project>
相关推荐
XiaoLeisj2 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
paopaokaka_luck2 小时前
【360】基于springboot的志愿服务管理系统
java·spring boot·后端·spring·毕业设计
dayouziei2 小时前
java的类加载机制的学习
java·学习
Yaml44 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台
java·spring boot·后端·mysql·spring·vue·二手书籍
小小小妮子~4 小时前
Spring Boot详解:从入门到精通
java·spring boot·后端
hong1616884 小时前
Spring Boot中实现多数据源连接和切换的方案
java·spring boot·后端
aloha_7894 小时前
从零记录搭建一个干净的mybatis环境
java·笔记·spring·spring cloud·maven·mybatis·springboot
记录成长java5 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet
睡觉谁叫~~~5 小时前
一文解秘Rust如何与Java互操作
java·开发语言·后端·rust
程序媛小果5 小时前
基于java+SpringBoot+Vue的旅游管理系统设计与实现
java·vue.js·spring boot