本文共 6909 字,大约阅读时间需要 23 分钟。
Spring IoC
1、 Spring IoC的基本概念 2、Spring的常用注解 3、 基于注解的依赖注入 4、 Java配置
1、 Spring IoC的基本概念
IOC(Inversion of Control) 其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源;而应用了IOC之后,则是容器主动的将资源推送给它所管理的组件,组件需要做的仅是选择一种合适的方式(属性注入[set函数]、构造器注入…)来接受资源,这种行为成为查找的被动形式。当Spring框架出现后,对象的实例不再由调用者来创建,而是由Spring容器(比如面包店)来创建。Spring容器会负责控制程序之间的关系(比如面包店负责控制您与面包的关系),而不是由调用者的程序代码直接控制。这样,控制权由调用者转移到Spring容器,控制权发生了反转,这就是Spring的控制反转。
从Spring容器角度来看,Spring容器负责将被依赖对象赋值给调用者的成员变量,相当于为调用者注入它所依赖的实例,这就是Spring的依赖注入,主要目的是为了解耦,体现一种“组合”的理念。 综上所述,控制反转是一种通过描述(在Spring中可以是XML或注解)并通过第三方去产生或获取特定对象的方式。在Spring中实现控制反转的是IoC容器,其实现方法是依赖注入。2、Spring的常用注解
2.1、声明Bean的注解 (1)@Component 该注解是一个泛化的概念,仅仅表示一个组件对象(Bean),可以作用在任何层次上,没有明确的角色。 (2)@Repository 该注解用于将数据访问层 DAO的类标识为Bean,即注解数据访问层Bean,其功能与@Component()相同。 (3)@Service 该注解用于标注一个业务逻辑组件类(Service层),其功能与@Component()相同。 (4)@Controller 该注解用于标注一个控制器组件类(Spring MVC的Controller),其功能与@Component()相同。 2.2、注入Bean的注解 (1)@Autowired 该注解可以对类成员变量、方法及构造方法进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除setter 和getter方法。默认按照Bean的类型进行装配。 (2)@Resource 该注解与@Autowired功能一样。区别在于,该注解默认是按照名称来装配注入的,只有当找不到与名称匹配的Bean才会按照类型来装配注入;而@Autowired默认按照Bean的类型进行装配,如果想按照名称来装配注入,则需要结合@Qualifier注解一起使用。 @Resource注解有两个属性:name和type。name属性指定Bean实例名称,即按照名称来装配注入;type属性指定Bean类型,即按照Bean的类型进行装配。 (3)@Qualifier 该注解与@Autowired注解配合使用。当@Autowired注解需要按照名称来装配注入,则需要结合该注解一起使用,Bean的实例名称由@Qualifier注解的参数指定。3、 基于注解的依赖注入
Spring IoC容器(ApplicationContext)负责创建和注入Bean。Spring提供使用XML配置、注解、Java配置以及groovy配置实现Bean的创建和注入。本书尽量使用注解(@Component、@Repository、@Service以及@Controller等业务Bean的配置)和Java配置(全局配置如数据库、MVC等相关配置)完全代替XML配置,这也是Spring Boot推荐的配置方式。下面通过一个简单实例【例1-2】向读者演示基于注解的依赖注入的使用过程,【例1-2】具体步骤如下。
1.使用Eclipse创建Web应用并导入JAR包
2.创建DAO层 在ch1_2应用的src中,创建annotation.dao包,该包下创建TestDao接口和TestDaoImpl实现类,并将实现类TestDaoImpl使用@Repository注解标注为数据访问层。
TestDao的代码如下:
package annotation.dao;public interface TestDao { public void save();} TestDaoImpl的代码如下:
package annotation.dao;import org.springframework.stereotype.Repository;@Repository("testDao")/**相当于@Repository,但如果在service层使用@Resource(name="testDao")注入Bean,testDao不能省略。**/public class TestDaoImpl implements TestDao{ @Override public void save() { System.out.println("testDao save"); }} 3.创建Service层
在ch1_2应用的src中,创建annotation.service包,该包下创建TestService接口和TestSeviceImpl实现类,并将实现类TestSeviceImpl使用@Service注解标注为业务逻辑层。
TestService的代码如下:
package annotation.service;public interface TestService { public void save();} TestSeviceImpl的代码如下:
package annotation.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import annotation.dao.TestDao;@Service("testService")//相当于@Servicepublic class TestSeviceImpl implements TestService{ @Resource(name="testDao") /**相当于@Autowired,@Autowired默认按照Bean类型注入Bean**/ private TestDao testDao; @Override public void save() { testDao.save(); System.out.println("testService save"); }} 4.创建Controller层
在ch1_2应用的src中,创建annotation.controller包,该包下创建TestController类,并将TestController类使用@Controller注解标注为控制器层。
TestController的代码如下: package annotation.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import annotation.service.TestService;@Controllerpublic class TestController { @Autowired private TestService testService; public void save() { testService.save(); System.out.println("testController save"); }} 5.创建配置类:
本书尽量不使用Spring的XML配置文件,而使用注解和Java配置。因此,在此需要使用@Configuration创建一个Java配置类(相当于一个Spring的XML配置文件),并通过@ComponentScan扫描使用注解的包(相当于在Spring的XML配置文件中使用<context:component-scan base-package=“Bean所在的包路径”/>语句)。 在ch1_2应用的src中,创建annotation包,该包下创建ConfigAnnotation的配置类
ConfigAnnotation的代码如下: package annotation;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration//声明当前类是一个配置类(见1.3.4节),相当于一个Spring的XML配置文件。@ComponentScan("annotation")//自动扫描annotation包下使用的注解,并注册为Bean。//相当于在Spring的XML配置文件使用 语句功能一样。public class ConfigAnnotation { } 6.创建测试类
在ch1_2应用的src中,创建annotation包,该包下创建TestAnnotation的配置类 TestAnnotation的代码如下:package annotation;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import annotation.controller.TestController;public class TestAnnotation { public static void main(String[] args) { //初始化Spring容器ApplicationContext AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(ConfigAnnotation.class); TestController tc = appCon.getBean(TestController.class); tc.save(); appCon.close(); }}} 7.运行结果
执行测试类的TestAnnotation的main方法
4、 Java配置
Java配置是Spring4.x推荐的配置方式,它是通过@Configuration和@Bean来实现的。@Configuration声明当前类是一个配置类,相当于一个Spring配置的XML文件。@Bean注解在方法上,声明当前方法的返回值为一个Bean。下面通过实例【例1-3】演示Java配置的使用过程,具体步骤如下。 1.使用Eclipse创建Web应用并导入JAR包
TestDao的代码如下:
package dao;//此处没有使用@Repository声明Beanpublic class TestDao { public void save() { System.out.println("TestDao save"); }} 3.创建Service层
在ch1_3应用的src中,创建service包,该包下创建TestService类,此类中没有使用@Service注解为业务逻辑层,具体代码如下:
TestService的代码如下:
package service;import dao.TestDao;//此处没有使用@Service声明Beanpublic class TestService { //此处没有使用@Autowired注入testDao TestDao testDao; public void setTestDao(TestDao testDao) { this.testDao = testDao; } public void save() { testDao.save(); }} 4.创建Controller层
在ch1_3应用的src中,创建controller包,该包下创建TestController类。此类中没有使用@Controller注解为控制器层,具体代码如下:
TestController的代码如下: package controller;import service.TestService;//此处没有使用@Controller声明Beanpublic class TestController { //此处没有使用@Autowired注入testService TestService testService; public void setTestService(TestService testService) { this.testService = testService; } public void save() { testService.save(); }} 5.创建配置类:
在ch1_3应用的src中,创建javaConfig包,该包下创建JavaConfig配置类。此类中使用@Configuration注解该类为一个配置类,相当于一个Spring配置的XML文件。在配置类中使用@Bean注解定义0个或多个Bean,具体代码如下:
ConfigAnnotation的代码如下: @Configuration//一个配置类,相当于一个Spring配置的XML文件;//此处没有使用包扫描,是因为所有Bean都在此类中定义了。public class JavaConfig { @Bean public TestDao getTestDao() { return new TestDao(); } @Bean public TestService getTestService() { TestService ts = new TestService(); //使用set方法注入testDao ts.setTestDao(getTestDao()); return ts; } @Bean public TestController getTestController() TestController tc = new TestController(); //使用set方法注入testService tc.setTestService(getTestService()); return tc; }} 6.创建测试类
在ch1_3应用的javaConfig中,创建TestConfig包,代码如下:package javaConfig;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import controller.TestController;public class TestConfig { public static void main(String[] args) { //初始化Spring容器ApplicationContext AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(JavaConfig.class); TestController tc = appCon.getBean(TestController.class); tc.save(); appCon.close(); }} 7.运行结果
执行测试类的TestAnnotation的main方法

1、CSDN 技术博客累计 3 篇
转载地址:http://sjrp.baihongyu.com/