Spring的核心思想之一:IOC(Inversion Of Control)控制反转
控制反转表达的意思是什么?
对象的创建交给外部的容器完成,这个就是控制反转。
Spring使用控制反转来实现对象的使用,不用在程序中写死。
控制反转解决对象处理问题(把对象交给别人来创建)
那么对象和对象之间的依赖关系在Spring中是如何处理的?-》依赖注入(Dependency injection)
Spring中使用依赖注入来实现对象之间的依赖关系
在创建对象之后,对象的关系处理就是依赖注入
控制反转是通过外部容器完成的,而Spring中提供了一个容器,称该容器为:IOC容器
无论对象的创建,处理对象之间的依赖关系,对象创建的时间,对象创建的数量,都是在Spring所提供的IOC容器上来配置对象的信息。
IOC的思想核心在于:资源不再使用资源方来管理,而是将资源交给第三方管理(IOC容器),这样处理带来很多好处:
Spring容器是如何管理对象的?这里来讲解Spring的操作步骤
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--Spring核心依赖jar包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!--log4j日志-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--注解相关jar包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
以上是Spring最核心的相关依赖
<?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-3.0.xsd">
</beans>
public class User {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以前通过new User的方法创建对象
User user = new User();
现在使用IOC容器,可以让IOC容器帮忙创建对象,在applicationContext.xml文件中添加User的对应信息
<!--使用bean节点来创建对象 id属性表示对象 class属性表示要交给容器管理的对象的全路径-->
<bean id="user" class="com.tulun.bean.User"/>
public static void main( String[] args )
{
//获取IOC的容器
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过容器来获取当前的对象
User user = (User) applicationContext.getBean("user");
System.out.println(user);
}
问:Spring容器如何获取到对象的呢?
Spring中的core模块:IOC容器,解决对象的创建和对象之间的依赖关系
如何得到IOC容器?
这个是Spring IOC容器中接口的继承关系,其中ApplicationContext是BeanFactory的子接口之一,换句话说,BeanFactory是IOC容器中最底层的接口,而ApplicationContext是其高级接口之一,在于对BeanFactory做了许多的功能上的扩展。在大部分的应用场景下,都是使用ApplicationContext作为Spring IOC的容器。
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
读取classpath的资源文件使用该类
FileSystemXmlApplicationContext xmlApplicationContext = new FileSystemXmlApplicationContext("c:/java/applicationContext.xml");
读取指定路径下的资源文件
XmlWebApplicationContext wa = new XmlWebApplicationContext(); //并没有初始化容量
wa.setServeletContext(servletcontext); //需要指定serveletContext对象
wa.setConfigLocation("applicationContext.xml"); //指定配置文件的路径,开头是以web为根目录的路径
wa.refresh(); //初始化容量
在web环境下读取配置信息
Spring容器中配置Bean的方式主要有两种
基于配置形式
基于注解形式
bean基于配置方式实例化有三种形式
上面的demo就是基于无参构造来实例化对象
<!--使用bean节点来创建对象 id属性表示对象 class属性表示要交给容器管理的对象的全路径-->
<bean id="user" class="com.tulun.bean.User"/>
注意:如果不指定构造函数,系统会生成默认的无参构造函数
如果指定有参构造函数,必须显性的指定一个无参构造函数,否则实例化会抛出异常
首先使用一个工厂的静态方法返回一个对象
public class Factory {
//创建一个User对象的静态方法
public static User getUserBean(){
return new User();
}
}
在配置文件中使用静态方法返回对象
<!--通过静态工厂方法创建对象,直接使用class来指定静态类,Factory-method指定方法就行-->
<bean id="user1" class="com.tulun.Factory" factory-method="getUserBean"/>
通过工厂的非静态方法得到当前的一个对象
public class Factory {
//创建一个User对象的静态方法
public static User getUserBean(){
return new User();
}
}
配置文件中使用工厂的非静态方法返回对象
<!--通过非静态工厂访问对象-->
<!--首先创建工厂对象-->
<bean id="factory" class="com.tulun.Factory"/>
<!--指定工厂对象和工厂方法-->
<bean id="user2" class="com.tulun.bean.User" factory-bean="factory" factory-method="getUserBean1"/>
基于注解的形式要比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"
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">
</beans>
<!--开启注解扫描:base-package(必填),指定包路径:会扫描类方法、属性上是否有注解-->
<context:component-scan base-package="com.tulun.bean"></context:component-scan>
<!--扫描属性上的注解(不建议使用)-->
<context:annotation-config></context:annotation-config>
注:必须要引入context约束,否则无法出现context标签
@Component(value = "user")
//
public class User {
private String id;
private String name;
}
Spring中主要提供了4个注解来标注Bean
@Component 通用的标注形式
@Repository 对Dao实现类进行标注
@Service 对Service 实现类进行标注
@Controller 对Controller实现类进行标注
后端的业务分层,一般业务请求顺序:Controller(进行URL结束和返回) -》Service(业务逻辑)-》Dao(访问数据层)
@Component是Spring提供的通用的组件注解
@Controller、@Service 、@Repository是@Component的衍生,功能一样,是可以互换的。
使用不同的注解主要是为了区分被注解的类处于不同的业务层,使逻辑更加清晰
这四个注解主要定义bean,创建bean,使用上是要标注在类上,@Component(value=“user”)或者@Component(“user”)
id属性:给当前对象取名字,可以使用特殊字符
name属性:给当前对象取名字,不可以使用特殊字符
class属性:表示要交给容器管理的对象的全路径
scope属性:表示当前对象是单例还是多例 singleton:单例 prototype:多例 注解形式: @Scope("")