Spring框架学习第一天–SpringIOC(控制反转)与DI(依赖注入)
- Spring Framework的一些学习网站
① github 网站—代码托管 码云-代码托管网站
② StackOverflow 代码社区–一些外国代码高手会解决一些问题(全英文)
一、Spring介绍
** Spring是轻量级的控制反转(IOC)和 面向切面(AOP)的容器框架。
①什么是轻量级的?
答:项目整合你需要浪费多大的资源叫开销。浪费资源少的叫轻量级,浪费资源多的叫重量级。
②什么是控制反转
答:Spring通过一种称作控制反转的技术促进了松耦合。
③什么是反转
答:把之间需要用户手动创建的实例的过程交给容器去完成。
**
二、需要为spring导入的jar包–以eclipse202003版本为例(现阶段)
三、开始配置Spring(注解开发配置)
①先在src下创建 applicationContext.xml 配置文件
在 applicationContext 配置文件中这样配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
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
">
<!-- 扫描这个com.hk.spring包下所有的加了注解的bean都放在这个容器中-->
<context:component-scan base-package="com.hk.spring"></context:component-scan>
</beans>
接着咱们再创建容器有两种方式我们先来最常用的最常用的 一、 ApplicationContext
//测试类
public class UserTest {
private ApplicationContext cont;
@Before
public void init() {
//创建容器--加载你的配置类
cont = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@Test
public void ceshi() {
UserServlet userservlet = (UserServlet) cont.getBean("servlet");
userservlet.find();
}
获取bean有2种方式
第一种
Student bean = context.getBean(studnet.class);//通过类点class获得
第二种
Student bean = context.getBean("stu");//通过bean中起的别名id 获得
第二种创建容器的方式(不常用)Resource
public class UserTest {
private BeanFactory factory;
@Before
public void init() {
//创建容器--加载你的配置类
Resource res = new FileSystemResource(new File("src/applicationContext.xml"));
factory = new XmlBeanFactory(resource);
}
@Test
public void ceshi() {
Studnet stu = (Studnet) factory.getBean("stu");
stu.hello();
}
四、这两种创建容器的区别
Spirng使用ApplicationContext定义容器对象是在容器-默认懒加载为false时,初始化时就创建好bean的实例!!
Spring使用beanFactory创建容器生成bean实例是在调用getBean的时候才会去实例化bean!!
五、四种创建bean的方式
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<!-- 1最常用的创建对象的方式 -->
<bean id="user" class="com.hk.spring.entity.User"></bean>
<!-- 2创建对象,使用静态工厂方法实例化(简单的工厂模式) -->
<bean id="apple" class="com.hk.spring.file.BeanFactory" factory-method="getBean"></bean>
<!-- 3创建对象 使用实例工厂方法实例化(工厂方法模式)-->
<bean id="apple2" factory-bean="bf2" factory-method="getBean"></bean>
<bean id="bf2" class="com.hk.spring.file.BeanFactory2"></bean>
<!-- 4set赋值的方式特殊字符处理 -->
<bean id="user2" class="com.hk.spring.entity.User">
<property name="name" >
<value> <![CDATA["《贺凯》"]]> </value>
</property>
<property name="sex" value="24"></property>
</bean>
<!-- 5简写操作 -->
<bean id="user5" class="com.hk.spring.entity.User" p:name="贺" p:sex="20">
</bean>
<!-- 6数组与list赋值 -->
<bean id="stu" class="com.hk.spring.entity.Student" p:sname="凯" p:age="1"></bean>
<bean id="user6" class="com.hk.spring.entity.User" p:name="贺" p:sex="20">
<property name="hobby">
<list>
<value>1</value>
<value>贺</value>
</list>
</property>
<property name="studen" ref="stu"></property>
<!-- map赋值 -->
<property name="Studnets">
<map>
<entry key="123" value="321"></entry>
<entry key="1凯" value="1ppijh和"></entry>
</map>
</property>
</bean>
</beans>