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());
}
}
五、测试结果
六、总结
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的,使用Spring后,对象是由Spring来创建的
反转:程序本身不创建对象有bean去创建管理, 而变成被动的接收对象
依赖注入:就是利用set方法来进行注入的
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();
}
=======================================================================
一、别名
总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
[外链图片转存中…(img-nOuT8ejx-1714642985397)]
[外链图片转存中…(img-fNYSQwEM-1714642985397)]
[外链图片转存中…(img-4aAtgHVQ-1714642985397)]
[外链图片转存中…(img-3Hu8MZXM-1714642985397)]