今天配置的吐血,三个小时搞定了bean……
idea安装spring
- idea2021是没有Spring的!!!
- 需要安装maven。
- 在项目名右键–添加框架支持–Spring4。
新建一个类
新建一个类
- 需要实现无参构造器
- 需要实现get和set方法
- toString也要写,但是非必须
public class hello {
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
@Override
public String toString() {
return "hellp{" +
"s='" + s + '\'' +
'}';
}
String s;
}
XML文件
-
有N种方式,非侵入式的就是XML式。其他的都是侵入式(需要注解),新版本的支持XML,旧版本的似乎只支持注解
-
xml文件需要在resource文件夹下才能进入源代码。
如果不存在会有这个问题:
如果是非侵入式的话,需要写xml文件,默认是spring.xml。
右键:添加springXml文件,会生成:
<?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">
</beans>
是在两个Beans 之间写入bean,不要在beans后面写,不然不会扫到,IDEA会报错。
格式是:
其余的可以去查,这个是最基础的。
新增一个参数
<property name="s" value="Spring"/>
初始化bean:
需要用配置文件初始化注册机。
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class text02 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring1.xml");
hello hello = (hellp) context.getBean("hello");
System.out.println(hello.toString());
}
}
需要在参数里面写文件名。不然会报错。
解决方式:需要配置文件名。
- 需要格式转化。
获得的类型是object,所以需要格式转化为需要的类型。
而且需要class类型,和普通的一样。
- 最后就是结果了:
出现这个就证明:已经成功配置spring 的bean。
附加一个查找方式:
人不是ide,所以记录一下。