spring
发布时间 :
阅读 :
了解spring
spring 参考文档:https://docs.spring.io/spring-framework/docs/5.2.0.RELEASE/spring-framework-reference/core.html#spring-core
笔记 1
·当类被配置了Bean后,当spring容器被初始化配置文件被加载的时候,类的无参构造也会同时被执行。
准备工作
下载spring
spring 官网:https://repo.spring.io/webapp/#/home
下载安装教程:https://blog.csdn.net/qq_35661171/article/details/86165467?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522160679455619724848160716%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=160679455619724848160716&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-6-86165467.first_rank_v12_rank_v28p3&utm_term=spring%E4%B8%8B%E8%BD%BD&spm=1018.2118.3001.4449
编写 .xml文件
1 2 3 4 5 6 7 8
| <?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 https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
|
第一个 spring
配置Bean
在.xml文件的beans标签里嵌套bean标签
1 2 3 4 5 6 7
| <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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="firstSpring" class="Demon01" /> </beans>
|
被测试类
1 2 3 4 5 6 7 8 9 10 11
| public class Demon01 { private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; } }
|
测试类
1 2 3 4 5 6 7 8 9 10 11
| import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemon { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springBeansXML.xml"); Demon01 am = (Demon01) applicationContext.getBean("firstSpring"); am.setName("guolu"); System.out.printf(am.getName()); } }
|
界面图
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 jaytp@qq.com