spring-di(beans)

项目建立

实现简单的验证登录

在这里插入图片描述
dao层(UserMapper)

package com.a2003.dao;

public interface UserMapper {
    /*public boolean login(String name,String password);//方法一*/

    public String login(String name,String password);//方法二
}

dao层(UserMapperImpl)

package com.a2003.dao;

public class UserMapperImpl implements UserMapper{
    //方法一
    @Override
    public String login(String name, String password) {
        if (name.equals("嫦娥")&&password.equals("123456")){
            return "登录成功";
        }else {
            return "登录失败";
        }
    }
    //方法二
    /*@Override
    public boolean login(String name, String password) {
        if (name.equals("嫦娥")&&password.equals("123456")){
            return true;
        }else {
            return false;
        }
    }*/


}

pojo层(User),构造方法

package com.a2003.pojo;

public class User {
    private int id;
    private String name;
    private String password;



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

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

pojo层(User2)

package com.a2003.pojo;

public class User2 {

    private int id;
    private String name;
    private String password;

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

service层(UserService)

package com.a2003.service;

public interface UserService {
    /*public boolean login(String name,String password);*/
    public String login(String name,String password);

}

service层(UserServiceImpl)

package com.a2003.service;

import com.a2003.dao.UserMapper;

public class UserServiceImpl implements UserService{

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

    @Override
    public String login(String name, String password) {
        return userMapper.login(name, password);
    }




    /*@Override
    public boolean login(String name, String password) {
        return userMapper.login(name, password);
    }*/
}

beans.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="user" class="com.a2003.pojo.User">
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="李白"></constructor-arg>
        <constructor-arg name="password" value="123456"></constructor-arg>
    </bean>


    <!--通过set方法实现依赖注入-->
    <bean id="user2" class="com.a2003.pojo.User2">
        <property name="id" value="2"></property>
        <property name="name" value="change"></property>
        <property name="password" value="123456"></property>

    </bean>


    <!--UserMapperImpl-->
    <bean id="UserMapperImpl" class="com.a2003.dao.UserMapperImpl"></bean>

   <!-- UserServiceImpl-->
    <bean id="UserServiceImpl" class="com.a2003.service.UserServiceImpl">
        <property name="userMapper" ref="UserMapperImpl"/>
    </bean>



</beans>

Mytest

import com.a2003.dao.UserMapperImpl;
import com.a2003.pojo.User;
import com.a2003.pojo.User2;
import com.a2003.service.UserService;
import com.a2003.service.UserServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void test(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        //通过构造方法实现依赖注入
        User user = applicationContext.getBean("user",User.class);
        System.out.println(user);
        //通过set方法实现依赖注入
        User2 user2 = applicationContext.getBean("user2",User2.class);
        System.out.println(user2);

    }

    //方法一
    /*@Test
    public void  test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = applicationContext.getBean("UserServiceImpl",UserService.class);

        boolean result = userService.login("嫦娥","123456");
        if (result == true){
            System.out.println("登录成功!!!");
        }else{
            System.out.println("登录失败!!!");
        }
    }*/

    //方法二
    @Test
    public void  test2(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");

        UserService userService = applicationContext.getBean("UserServiceImpl",UserService.class);

        String result = userService.login("嫦娥","123456");
        System.out.println(result);

    }


    @Test
    public void  test3() {
        UserService userService = new UserServiceImpl();
        ((UserServiceImpl) userService).setUserMapper(new UserMapperImpl());
        String result = userService.login("嫦娥", "123456");
        System.out.println(result);
    }
}

test测试成功
test测试结果

test2测试成功,红色不要管test2测试成功
test3测试成功
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
spring-beans.jar是Spring框架中的一个核心JAR文件,它包含了Spring容器的基本组件BeanFactory和ApplicationContext,以及与Bean相关的一系列类和接口。如果没有spring-beans.jar,Spring框架将不能运行。 首先,Spring框架的核心思想是依赖注入(Dependency Injection,DI)和控制反转(Inversion of Control,IOC)。这意味着对象之间的依赖关系将不再由对象自身管理,而是由Spring容器来管理。在这种情况下,Spring容器必须能够创建和管理对象的实例,以及管理它们之间的依赖关系。这就是spring-beans.jar的作用。 BeanFactory是Spring容器的基本组件之一。它是一个工厂类,负责管理Bean的生命周期、创建Bean实例、管理Bean的依赖关系和Bean之间的通信。ApplicationContext是BeanFactory的扩展,除了BeanFactory的功能,它还提供了事件机制、AOP(面向切面编程)、异常处理等其他的功能。Spring框架的大部分应用程序都需要使用BeanFactory或ApplicationContext来管理Bean。 另外,spring-beans.jar还包含了一系列与Bean相关的类和接口。例如,BeanInfo、BeanDefinition、BeanWrapper、BeanPostProcessor、InitializingBean、DisposableBean等,这些类和接口提供了在Bean创建、初始化、销毁等过程中的扩展点,可以让开发者在这些过程中加入自己的逻辑,实现自己的扩展。 总之,spring-beans.jar可以说是Spring框架的核心之一,没有它,Spring框架将不能运行。它提供了创建、管理Bean实例以及它们之间的依赖关系、扩展点等功能,是Spring框架中不可替代的重要组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临桉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值