咱们常常见到的控制翻转(Inversion of Control-IOC) 和依赖注入(dependency injection-DI),在Spring 环境下等同的概念,控制翻转是经过依赖注入实现的。所谓依赖注入指的是容器负责建立对象和维护对象间的依赖关系,而不是经过对象自己负责本身的建立和解决本身的依赖。java
Spring IOC容器(ApplicationContext)负责建立Bean ,并经过容器将功能类Bean注入到你须要的Bean中。Spring框架提供使用Xml、注解、java配置、groovy配置实现Bean 的建立和注入。spring
不管是 Xml 配置、注解配置仍是 java 配置,都被称为元数据,所谓元数据即描述数据的数据。元数据自己不具有任何可执行能力,只能经过外界代码来对这些元数据行解析后进行一些有意义的操做。Spring容器解析这些配置的元数据进行Bean初始化、配置和管理依赖。编程
声明Bean的注解:app
@Component 没有明确的角色
@Service 在业务逻辑层(service层)使用
@Repository 在数据访问层(dao层)使用
@Controller 在表现层(MVC-->SpringMVC)使用
注入Bean的注解,通常状况下通用框架
@Autowired Spring提供的注解
@Inject JSR-330提供的注释
@Resource JSR-250提供的注释
@Autowired 、@Inject 、@Resource可注解在set方法上或者属性上,建议注解在属性上,有点是代码更少、层次更清晰。
通常注入规则:学习
从逻辑图中咱们能够看出,Spring容器包含了Model层、Service层、Dao层 。 他们每个都是Spring容器管理的一个实体类。能够经过Spring容器将一个实体类须要的另外实体类传给它。 而不采用在实体类中经过new的方式来获取实体类,由于new的方式不方便咱们的使用。咱们经过注入的方式,传给他须要的实体类,也就是一个实体类须要什么对象,只要被须要的对象在Spring容器管理下,我就给它什么对象,再也不须要经过new的形式获得他,这样在实体类中咱们就能够直接使用。测试
依赖注入:①注解注入this
[java] view plain copy设计
(model层)
code
package com.dependencyInjection_Di;
import org.springframework.stereotype.Component;
@Component //声明Model是Spring管理的一个bean
public class Model {
public String ModelName;
}
(service层)
package com.dependencyInjection_Di;
import com.dependencyInjection_Di.ModelMapper;
import com.dependencyInjection_Di.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(value = "modelService") //声明ModelService是Spring管理的一个bean
public class ModelService {
@Autowired ModelMapper modelMapper; @Autowired Model model; public void SayHello(){ System.out.println("进入Service层"); model.ModelName="测试完毕"; modelMapper.SayHello(model); }
}
(dao层)
package com.dependencyInjection_Di;
import org.springframework.stereotype.Repository;
@Repository
public class ModelMapper {
public void SayHello(Model model){ System.out.print("数据持久层insert "+"\n"+model.ModelName); }
}
声明配置类
package com.dependencyInjection_Di;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //声明当前类是一个配置类
@ComponentScan("com.dependencyInjection_Di") //使用ComponentScan自动扫描包下全部的@Service、@Component、@Repository、@Controller
public class Diconfig {
}
测试类
package com.dependencyInjection_Di;
import com.dependencyInjection_Di.Diconfig;
import com.dependencyInjection_Di.ModelService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class RunningTest {
/** * 用到jar包 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.12.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.13.RELEASE</version> </dependency> * Spring框架自己四大原则 * ⑴使用POJO进行轻量级和最小侵入式开发 * ⑵经过依赖注入和基于接口编程实现松耦合 * ⑶经过AOP和默认习惯进行声明式编程 * ⑷使用AOP和模板(template)减小模式化代码 * Spring全部功能的设计和实现都是基于此四大原则 * @Configuration 声明类是配置类 * @ComponentScan 自动扫描目录下全部带有注解的Bean * @Service、@Controller、@Component、@Repository 是等效的,能够根据层面选择进行标识使用 */ public static void main(String arg[]){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Diconfig.class); ModelService modelService = (ModelService) context.getBean("modelService"); modelService.SayHello(); }
}
输出信息
。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。蛮多信息
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@63ee4826: startup date [Thu Jan 18 18:14:29 CST 2018]; root of context hierarchy
进入Service层
数据持久层insert
测试完毕
Process finished with exit code 0
依赖注入:②java配置注入
(model层)
package com.JavaDependencyInjection_Di;
public class Model
{
public String modelName;
}
(service层)
package com.JavaDependencyInjection_Di;
import com.JavaDependencyInjection_Di.ModelMapper;
import com.JavaDependencyInjection_Di.Model;
public class ModelService {
Model model; ModelMapper modelMapper; public void setModel(Model model){ this.model=model; } public void setModelMapper(ModelMapper modelMapper) { this.modelMapper = modelMapper; } public void SayHello(){ System.out.println("service层"); model.modelName="Hello"; modelMapper.SayHello(model); }
}
(dao层)
package com.JavaDependencyInjection_Di;
public class ModelMapper {
public void SayHello(Model model){ System.out.print("Dao层 \n"); System.out.print(model.modelName); System.out.print("\n 测试完毕!"); }
}
(配置类)
package com.JavaDependencyInjection_Di;
import com.JavaDependencyInjection_Di.ModelMapper;
import com.JavaDependencyInjection_Di.Model;
import com.JavaDependencyInjection_Di.ModelService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration //①
public class JavaConfig {
@Bean //② public Model model(){ return new Model(); } @Bean public ModelMapper modelMapper(){ return new ModelMapper(); } @Bean public ModelService modelService(){ ModelService modelService = new ModelService(); modelService.setModelMapper(modelMapper()); //③ modelService.setModel(model()); return modelService; } @Bean public ModelService modelService(Model model,ModelMapper modelMapper){ ModelService modelService = new ModelService(); modelService.setModelMapper(modelMapper); //④ modelService.setModel(model); return modelService; } /** * ①使用@Configuration 声明当前类是一个配置类,这觉得着这个类里可能含有 0 个或者多个 @Bean注解, * 此处没有使用包扫描,是由于全部的 Bean 都在此类中定义了 * ②使用@Bean声明当前方法 model() 返回值是一个Bean,Bean名称是方法名 * ③注入 ModelMapper 的 Bean 时候直接调用 modelMapper() * ④另外一种注入方式,直接将 ModelMapper 做为参数给 modelService(),这也是Spring容器提供的极好的功能, * 在 Spring 容器中,只要容器中存在某个 Bean,就能够在另外一个 Bean 的声明方法参数中写入 */
}
(测试类)
package com.JavaDependencyInjection_Di;
import com.JavaDependencyInjection_Di.JavaConfig;
import com.JavaDependencyInjection_Di.ModelService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class RunningTest {
public static void main(String arg[]){ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class); ModelService modelService = context.getBean(ModelService.class); modelService.SayHello(); }
}
(运行结果)
一月 18, 2018 6:31:05 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7956b7c1: startup date [Thu Jan 18 18:31:05 CST 2018]; root of context hierarchy
service层
Dao层
Hello
测试完毕!
Process finished with exit code 0
此乃学习笔记,记录下来但愿以备本身忘记,很差地方,望多加指点。