Spring Ioc
控制反转(IoC,Inversion of Control)是Spring 框架中的核心,而这核心技术在他的官方文档"/docs/spring-framework-reference/core.html#spring-core"之中,第一句话标题就是”核心技术“。通常IoC也可以称之为依赖注入(DI,De),在此过程中,对象会通过构造函数参数,通过工厂参数在构造或从工厂方法中返回后的对象实例上设置的属性来定义其依赖项。 也就是说,当某个Java对象需要调用另一个Java对象时,在正常且传统的模式下,通常会使用new来实例化该类,而在之后出现的Spring时,则对象的实例不需要有你来创建,而是由Spring容器来创建,控制程序之间的关系。
Spirng 常用注解 声明Bean 注解 Spring框架中,使用XML配置文件可以简单的配置Bean,但如果项目庞大需要Bean装配时,将会导致XML配置文件过于庞大,不方便以后的维护和拓展,因此衍生出了注解(annotaion)的方式去配置Bean:
ID
DA
FA
@Component
一个泛化的概念,仅仅表示一个组件对象(bean),可以作用在让你和层次之上,没有明确的角色
Bean
@Repository
用于数据访问层(DAO)的类标识为Bean,即注解数据访问层Bean,功能与@Component 相同
DAO
@Service
用于标注一个业务逻辑组件类(Service层),功能与@Component相同
Service
@Controller
该注解用于标注一个控制器组件类(Spring MVC的Controller),功能与@Component相同
Spring MVC
注入Bean 注解
ID
DA
FA
@Autowired
可对成员变量、方法及构造方法进行标注,完成自动装配工作,通过@Autowired可消除set和get方法,默认按照Bean的类型进行装配
@Resource
与@Autowired的功能一样,区别与该注解默认按照名称来装配注入,只有当找不到名称匹配的Bean时才会按照类型来装配注入。可结合@Qualifier注解一起搭配食用可以按照名称来进行装配
@Resource
@Resource有两个属性,分别为name和type,Name属性指定Bean实例名称,而按照名称来装配注入;Type属性则指定Bean类型,即安装Bean的类型进行装配
@Qualifier
可与@Autowired注解配合使用,当@Autowired注解需要按照名称来进行装配注入时 ,则需要结合该注解进行使用,而@Qualifier注解可以指定Bean实例名
Java 配置注解
ID
DA
FA
@Configuration
声明一个Java的配置类,相当于Spring中的XML配置文件
XML
@ComponentScan
扫描使用的注解包,并创建指定的ConfigAnnotation的配置类
XML
基于注解的依赖注入 本文主要使用```@Override、@Resource、@Repository、@Controller、@Autowired、@Configuration、@ComponentScan等注解来实现Spring xml配置文件来实现配置、注解、依赖、注入等方式,而这也是Spring Boot所推荐的配置方式:
所需的外部Jar开发包 本文需实现基于注解的依赖注入,需要使用commons-logging-1.2.jar、spring-aop-5.1.4-RELEASE.jar、spring-beans-5.1.4.RELEASE.jar、spring-context-5.1.4.RELEASE.jar、spring-core-5.1.4.RELEASE.jar、spring-expression-5.1.4.RELEASE.jar等jar开发包引入至项目文件中WebContent/WEB-INF/lib目录下即可。
ID
DA
FA
TestDao.java
Dao
TestDaoImpl.java
@Repository
TestService.java
Service
TestServiceImpl.java
@Service、@Resource、@Override
TestController.java
@Controller、@Autowired
Controller
ConfigAnnotation.java
@Configuration、@ComponentScan
annotation
TestAnnotation.java
@Controller、@Autowired
Dao TestDao.java 1 2 3 4 5 package annotation.dao;public interface TestDao { public void save () ; }
TestDaoImpl.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package annotation.dao;import org.springframework.stereotype.Repository;@Repository("testDao") public class TestDaoImpl implements TestDao { @Override() public void save () { System.out.println("testdao save" ); } }
Service TestService.java 1 2 3 4 package annotation.service;public interface TestService { public void save () ; }
TestServiceImpl.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package annotation.service;import javax.annotation.Resource;import org.springframework.stereotype.Service;import annotation.dao.TestDao;@Service("testService") public class TestServiceImpl implements TestService { @Resource(name = "testDao") private TestDao testDao; @Override public void save () { testDao.save(); System.out.println("testService save" ); } }
Controller TestController.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package annotation.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import annotation.service.TestService;@Controller public class TestController { @Autowired private TestService testService; public void save () { testService.save(); System.out.println("testController save" ); } }
annotation ConfigAnnotation.java 1 2 3 4 5 6 7 8 9 10 11 12 13 package annotation;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration @ComponentScan("annotation") public class ConfigAnnotation { }
TestAnnotation.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package annotation.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import annotation.service.TestService;@Controller public class TestController { @Autowired private TestService testService; public void save () { testService.save(); System.out.println("testController save" ); } }
⬅️ Go back