Spring IOC
Spring 框架介绍
Spring 是众多开源java项目中的一员,基于分层的javaEE应用一站式轻量级开源框架,主要核心是
IOC(控制反转/依赖注入)与 AOP(面向切面)两大技术,实现项目在开发过程中的轻松解耦,提高项
目的开发效率。
在项目中引入 Spring 立即可以带来下面的好处 降低组件之间的耦合度,实现软件各层之间的解耦。可
以使用容器提供的众多服务,如:事务管理服务、消息服务等等。当我们使用容器管理事务时,开发人
员就不再需要手工控制事务.也不需处理复杂的事务传播。 容器提供单例模式支持,开发人员不再需要自
己编写实现代码。 容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能。
Spring 框架环境搭建
- 建立maven项目
- 引入Spring 依赖
- 在main方法下的Resourses里添加spring.xml配置文件
- 写bean对象并再配置文件中添加
- 加载文件,并且实例化
Spring IOC 配置文件
-
加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
-
spring 多配置文件加载
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml","dao.xml");
-
通过在spring.xml中导包来实现多文件配置
<?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"> <!--导入需要包含的资源文件--> <import resource="service.xml"/> <import resource="dao.xml"/> </beans>
Spring IOC 容器 bean对象实例化
-
通过构造器调用
注意默认调用空构造器
-
配置文件
<!--通过构造器创建--> <bean id="user" class="com.yjxxt.bean.User"> <property name="uname" value="张三"></property> <property name="id" value="001"></property> </bean>
-
调用
User user = ac.getBean("user", User.class); System.out.println(user);
-
-
静态工厂
-
静态工厂
public static Car getCar(){ return new Car(100,"BMW",500000.0); }
-
配置文件
<!--通过静态工厂创建--> <bean id="car" class="com.yjxxt.factory.CarFactory" factory-method="getCar"></bean>
-
调用
Car car = ac.getBean("car", Car.class); System.out.println(car);
-
-
实例化工厂实例化
-
非静态工厂
public Car getCar(){ return new Car(101,"BMW",500000.0); }
-
配置文件
<bean id="car" class="com.yjxxt.factory.CarFactory" factory-method="getCar"></bean>
-
调用
Car car02 = ac.getBean("car02", Car.class); System.out.println(car02);
-
Spring IOC 注入
set注入
-
javabean 注入
-
属性字段必须需要提供set方法
<bean id="user" class="com.yjxxt.bean.User"> <property name="uname" value="张三"></property> <property name="id" value="001"></property> </bean>
如上User类里面uname,id的注入必须有相对应得方法
-
-
集合类型注入
-
bean对象
private Integer eid; private String ename; private Double salary; private Dept dept; private Date birth; private String[] hobby; private List<String> elist; private Set<String> eset; private Map<String,Object> emap; private Properties properties;
-
配置文件
<bean id="Emp" class="com.yjxxt.bean.Emp"> <property name="eid" value="001"></property> <property name="salary" value="1500"></property> <property name="ename" value="张三"></property> <property name="elist"> <list> <value>上海</value> <value>北京</value> <value>广州</value> </list> </property> <property name="hobby"> <array> <value>吸烟</value> <value>喝酒</value> <value>烫头</value> </array> </property> <property name="eset"> <set> <value>语文</value> <value>数学</value> <value>英语</value> </set> </property> <property name="emap"> <map> <entry key="黄伟" value="18"></entry> <entry key="张斌" value="18"></entry> <entry key="知道" value="18"></entry> </map> </property> <property name="properties"> <props> <prop key="driver">com.yjxxt.driver</prop> <prop key="url">url</prop> <prop key="username">root</prop> <prop key="password">root</prop> </props> </property> <property name="birth" ref="date"></property> <property name="dept" ref="dept"></property> </bean> <bean id="date" class="java.util.Date"> <property name="year" value="2021"></property> <property name="month" value="10"></property> <property name="date" value="9"></property> </bean> <bean id="dept" class="com.yjxxt.bean.Dept"> <property name="did" value="100"></property> <property name="dname" value="研发部"></property> </bean>
-
构造器注入
单个构造器注入
-
bean对象
public class Student { private int sid; private String sname; private Car car;
public Car getCar(String brand){ Car car =null ; if ("QQ".equals(brand)){ return new Car(103,brand,60000); } if ("FLL".equals(brand)){ return new Car(104,brand,888888888); } car = new Car(); return car; }
-
配置文件
<bean id="factory" class="com.yjxxt.factory.CarFactory02"></bean> <bean id="car" factory-bean="factory" factory-method="getCar"> <constructor-arg type="java.lang.String" value="QQ"></constructor-arg> </bean> <bean id="stu" class="com.yjxxt.bean.Student"> <property name="sname" value="张三"></property> <property name="sid" value="100"></property> <property name="car" ref="car"> </property> </bean>
Spring扫描器
- 介绍
实际的开发中,bean的数量非常多,采用手动配置bean的方式已无法满足生产需要,Spring这时候
同样提供了扫描的方式,对扫描到的bean对象统一进行管理,简化开发配置,提高开发效率。
-
扫描器的配置
Spring IOC 扫描器 作用:bean对象统一进行管理,简化开发配置,提高开发效率 1、设置自动化扫描的范围 如果bean对象未在指定包范围,即使声明了注解,也无法实例化 2、使用指定的注解(声明在类级别) bean对象的id属性默认是 类的首字母小写 Dao层: @Repository Service层: @Service Controller层: @Controller 任意类: @Component 注:开发过程中建议按照指定规则声明注解
-
设置自动扫描范围(配置文件中)
<!-- 设置自动化扫描的范围 --> <context:component-scan base-package="com.yjxxt"/>