该栏目会系统的介绍Spring的知识体系,共分为基础部分、源代码和综合实例等模块,有兴趣的小伙伴可以关注 下,如有疑问可以多多评论
容器简介
1、容器概述
:把对象的创建和对象之间的调用过程交给Spring容器,从而降低耦合度
2、底层原理
3、容器实现方式
BeanFactory
概述:IOC容器的基本实现,一般不会在开发中使用 特点:加载配置文件时不会创建对象,在获取对象时才创建 ApplicationContext
概述: BeanFactory接口的子接口 特点:加载配置文件时就会把对象创建完成 子类:ClassPathXmlApplicationContext
配置文件方式
1、类实例化方式
2、bean标签
属性名称 作用 id 可以根据id值找到对象 name 与id属性作用一样 class 配置创建对象的类路径 scope 配置创建对象的范围,如single、prototype、request、session、globalSession
3、属性注入
4、IOC与DI的比较
IOC
:控制反转,把创建对象的过程交给Spring进行配置DI
:依赖注入,向对象注入属性值,必须在IOC的基础之上完成
5、案例
public class Address {
private String city;
private String district;
public Address ( String city, String district) {
this . city = city;
this . district = district;
}
}
@Setter
public class User {
private String name;
private Address address;
private String[ ] hobbies;
private List< String> likeMusic;
private Map< String, Float> scores;
private Properties others;
}
public class TestDemo {
@Test
public void testAddUser ( ) {
ApplicationContext context = new ClassPathXmlApplicationContext ( "bean.xml" ) ;
final User user = ( User) context. getBean ( "user" ) ;
System. out. println ( user) ;
}
}
<?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" >
< bean id = " address" class = " com.itnear.entity.Address" >
< constructor-arg name = " city" value = " 深圳市" > </ constructor-arg>
< constructor-arg name = " district" value = " 宝安区" > </ constructor-arg>
</ bean>
< bean id = " user" class = " com.itnear.entity.User" >
< property name = " name" value = " Near" />
< property name = " address" ref = " address" />
< property name = " hobbies" >
< list>
< value> 阅读</ value>
< value> 代码</ value>
</ list>
</ property>
< property name = " likeMusic" >
< list>
< value> 天空之城</ value>
< value> 卡农</ value>
</ list>
</ property>
< property name = " scores" >
< map>
< entry key = " english" value = " 100" />
< entry key = " math" value = " 100" />
</ map>
</ property>
< property name = " others" >
< props>
< prop key = " loginTime" > 2020-01-10 02:19:00</ prop>
</ props>
</ property>
</ bean>
</ beans>
注解方式
1、类相关注解
注解 作用 @Component 标识类为组件 @Controller 标识WEB层中的类为组件 @Service 标识业务层中的类为组件 @Repository 标识持久层中的类为组件 @Scope 设置对象被创建的类型,如:singleton、prototype @ComponentScan 指定创建容器时要扫描的包
2、属性相关注解
注解 作用 @Configuration 配置文件 @Import 用于导入其他配置类 @PropertySource 用于导入.properties配置文件 @Value 基本数据类型和字符串 @Resource 根据name属性值设置属性值 @Autowired 自动装载
3、方法相关注解
注解 作用 @Bean 把当前方法的返回值做为bean对象,存储spring容器中 @Pre 启动服务器前执行方法 @Pos 启动服务器后执行方法
4、案例
@Repository ( "userDao" )
public class UserDao {
public void show ( ) {
System. out. println ( "UserDao..........." ) ;
}
}
@Service ( "userService" )
@Scope ( value= "prototype" )
public class UserService {
@Autowired
private UserDao userDao;
public void show ( ) {
System. out. println ( userDao + "............." ) ;
}
}
public class TestDemo {
@Test
public void test ( ) {
ApplicationContext context =
new ClassPathXmlApplicationContext ( "applicationContext.xml" ) ;
UserService user = ( UserService) context. getBean ( "userService" ) ;
user. show ( ) ;
}
}
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= " http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
< context: component-scan base-package = " cn.itcast" > </ context: component-scan>
</ beans>
整合WEB项目原理
1、描述
:把加载配置文件和创建对象的过程,在服务器启动时完成
2、相关技术
ServletContext对象 ServletContextListents对象
3、操作过程
:当服务器启动时,为每个项目创建一个ServletContext对象,当监听器监听到该对象被创建时,会加载Spring配置文件,和创建相关对象,并且把创建的对象存储在ServletContext域对象中