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>
相关推荐
大数据编程之光21 分钟前
Flink Standalone集群模式安装部署全攻略
java·大数据·开发语言·面试·flink
爪哇学长35 分钟前
双指针算法详解:原理、应用场景及代码示例
java·数据结构·算法
ExiFengs39 分钟前
实际项目Java1.8流处理, Optional常见用法
java·开发语言·spring
paj12345678940 分钟前
JDK1.8新增特性
java·开发语言
繁依Fanyi1 小时前
简易安卓句分器实现
java·服务器·开发语言·算法·eclipse
慧都小妮子1 小时前
Spire.PDF for .NET【页面设置】演示:打开 PDF 时自动显示书签或缩略图
java·pdf·.net
m51271 小时前
LinuxC语言
java·服务器·前端
IU宝1 小时前
C/C++内存管理
java·c语言·c++
瓜牛_gn1 小时前
依赖注入注解
java·后端·spring
hakesashou1 小时前
Python中常用的函数介绍
java·网络·python