史上最全Spring教程,从零开始带你深入♂学习(一)——Spring入门

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

public static void main(String[] args) {

//获取Spring的上下文对象

ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);

//加群1025684353一起吹水聊天

//我们的对象都在spring中管理,我们要使用直接去里面取出来!

//Hello hello = context.getBean(“hello”,Hello.class);

Hello hello = (Hello) context.getBean(“hello”);

System.out.println(hello.toString());

}

}

五、测试结果

image

六、总结

领取资料

控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的

反转:程序本身不创建对象有bean去创建管理, 而变成被动的接收对象

依赖注入:就是利用set方法来进行注入的

IOC是一种编程思想,由主动的编程变成被动的接收

IOC创建对象方式

========================================================================

一、通过无参构造方法来创建

1、编写实体类

package com.study.pojo;

public class User {

private String name;

public User() {

System.out.println(“user无参构造方法”);

}//加群1025684353一起吹水聊天

public void setName(String name) {

this.name = name;

}

public void show(){

System.out.println(“name=”+ name );

}

}

2、编写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”>

@Test

public void test(){

ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);

//在执行getBean的时候, user已经创建好了 , 通过无参构造

User user = (User) context.getBean(“user”);

//调用对象的方法 .

user.show();

}

领取资料

二、通过有参构造方法来创建

package com.study.pojo;

public class UserT {

private String name;

public UserT() {

System.out.println(“UserT被创建了”);

}

public UserT(String name) {

this.name = name;

}//加群1025684353一起吹水聊天

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void show(){

System.out.println(“name=”+name);

}

}

领取资料

编写beans.xml三种方式

(1)根据index参数下标设置

(2)根据参数名字设置

(3)根据参数类型设置

领取资料

3、编写测试类

@Test

public void testT(){

ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);

UserT user = (UserT) context.getBean(“userT”);

user.show();

}

Spring配置

=======================================================================

一、别名

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结

在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。

[外链图片转存中…(img-nOuT8ejx-1714642985397)]

[外链图片转存中…(img-fNYSQwEM-1714642985397)]

[外链图片转存中…(img-4aAtgHVQ-1714642985397)]

[外链图片转存中…(img-3Hu8MZXM-1714642985397)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 29
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值