spring01
spring pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring__</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--start spring 支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<!--end spring支持-->
<!--start log4j支持-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--end log4j支持-->
<!--start junit支持-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--end junit支持-->
</dependencies>
</project>
spring ioc
IOC:inverse of control
正常模式下,对象在何处被需要便在何处创建;spring中,ioc是指与正常模式不同,将对象的创建交给spring去完成;这好像是对象的创建权利交由spinrg去处理,这种形式便被称为inverse of control--->控制反转
/**
* ioc示例
*/
@Test
public void fun(){
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
DemoDaoImpl obj = (DemoDaoImpl)context.getBean("Demo");
obj.demoFun();
}
/**
* 正常模式示例
*/
@Test
public void fun01(){
DemoDaoImpl obj=new DemoDaoImpl();
obj.demoFun();
}
关于IOC的好处,现在还理解不了
spring bean配置
ApplicationContext是BeanFactory的子接口
BeanFactory是Spring容器的顶级接口
ApplacationContext只要一读取配置文件就将里面涉及到的类都创建出来
BeanFactory是在要使用时才创建
ApplacationContext接口的实现类
ClassPathXmlApplicationCntext:
从类的根路径下加载配置文件
FileSystemXmlApplicationContext:
从磁盘路径上加加载配置文件,配置文件可以在磁盘的任意位置
AnnotationConfigApplicationContext:
读取注解
IOC中bean标签和对象管理细节
bean标签作用
用于配制可以让spring创建实例的类
创建对象时默认调用对象的无参构造函数,如果没有,则对象创建不成功
bean属性
id:一个bean的唯一标识,用于在使用时会去一个bean
class:了诶的全限定类名
scope:指定对象的作用范围
singleton:单例(默认值)
prototype:多例的
request:web项目中,创建并放入request域
response:web项目中,创建并放入response域
init-method:指定类中的初始化方法名
destory-method:指定类中销毁方法名
点赞
评论留言