发布时间:2024-01-27 10:00
JAVAWEB框架学习文章索引点这里
核心api:
BeanFactory:用于生成任意bean,采取延迟加载,第一次getBean的时候才会初始化Bean
ApplicationContext:是BeanFactory的子接口,功能更强大。当配置文件被加载时,就进行对象实例化。
ClassPathXmlApplicationContext :用于加载classpath(类路径、src)下的xml
FileSystemXmlApplicationContext:用于加载指定盘符下的xml
spring loC(控制反转)简单使用
1,导入jar包(beans,context,core,expression,logging)
2,编写配置文件
配置文件
<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="Person" class="com.IocDemo.Person">bean>
beans>
测试
@Test
public void test1() {
//1,获得spring容器
String xmlPath = "com/IocDemo/beans.xml";
ApplicationContext appCont = new ClassPathXmlApplicationContext(xmlPath);
//2,获得内容
Person person = (Person) appCont.getBean("Person");
person.setId(20);
System.out.println(person.getId());
}
DI 依赖注入
分别创建StudentDao、StudentService接口
分别创建StudentDaoImpl、StudentServiceImpl实现类
在StudentServiceImpl中设置属性StudentDao并设置对于的setStudentDao方法(配置文件配置后自动调用进行赋值),由于StudentServiceImpl中并不知道StudentDao的实现类的内容(实际上调用setStudentDao方法的时候配置传入的是它的实现类),这是一种解耦。
几个接口,实现类
package com.b_DI;
public interface StudentDao {
void info();
}
package com.b_DI;
public class StudentDaoImpl implements StudentDao {
@Override
public void info() {
System.out.println("我是学生");
}
}
package com.b_DI;
public interface StudentService {
void study();
}
package com.b_DI;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
@Override
public void study() {
System.out.println("学生学习");
studentDao.info();
}
}
配置文件:
<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="StudentService" class="com.b_DI.StudentServiceImpl">
<property name="studentDao" ref="StudentDao">property>
bean>
<bean id="StudentDao" class="com.b_DI.StudentDaoImpl">bean>
beans>
测试类
package com.b_DI;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.a_IocDemo.Person;
public class MyTest {
@Test
public void test1() {
//1,获得spring容器
String xmlPath = "com/b_DI/DITest.xml";
ApplicationContext appCont = new ClassPathXmlApplicationContext(xmlPath);
//依赖注入
StudentService studentService = (StudentService) appCont.getBean("StudentService");
studentService.study();
}
}
结果:
学生学习
我是学生
Topic 10. 单因素 Logistic 回归分析—单因素分析表格
python pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted
【USENIX ATC】支持异构GPU集群的超大规模模型的高效的分布式训练框架Whale
【面试:并发篇16:多线程:wait/notify详解】原理及错误用法(虚假唤醒等)
【数字IC手撕代码】Verilog全加器半加器|题目|原理|设计|仿真
C#工控上位机实例_「C#上位机」西门子1200PLC实用定位控制程序案例
AMiner发布2022 AI 2000人工智能最具影响力学者名单