Spring注解开发 浅尝Spring注解开发,基于Spring 4.3.12 包含自定义扫描组件.自定义导入组件.手动注册组件.自动注入方法和参数.使用Spring容器底层组件等 配置 @Configuration配置类 告诉Spring这是一个配置类,代替以前的xml文件,配置类=配置文件 @Configuration public class MainConfig { //给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id @Bean("person") p…
1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring注解开发:ComponentScan组件扫描 2.使用bean注解 主要使用场景:导入第三方包里面的组件,使用到的注解: @Bean 参考:spring注解开发:Configuration&Bean 3.使用@Import注解 使用方式:@Import(要导入到容器中的组件):容器中就会自动注册这个…
给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组件 @Service public class PersonServic { } //持久层组件 @Repository public class PersonDao { } //其他组件 @Component public class Person { } 包扫描(注解或者xml的方式) 1.使用…
2.组件注册-@Configuration&@Bean给容器中注册组件 2.1 创建maven项目 spring-annotation pom.xml文件添加 spring-context 依赖 <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId>…
写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对!另外,每个项目编写大量的XML文件来配置Spring,也大大增加了项目维护的复杂度,往往很多个项目的Spring XML文件的配置大部分是相同的,只有很少量的配置不同,这也造成了配置文件上的冗余. 项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/sp…
1.@Bean 导入第三方的类或包的组件 2.包扫描+组件的标注注解(@ComponentScan: @Controller,@service,@Reponsitory,@Componet), 自己写的类 3.@Import[可以快速给容器中导入一个或者多个组件]        1@Import(要导入到容器中的组件):容器中就会自动注册这个组件,id默认是全类名 @Configuration @Import(value = {Student.class,Teacher.class}) publ…
1.通过xml定义 <bean class=""> <property name="" value=""></property> </bean> 2.通过注解 这种方式比较常见,通常用@Controller.@Component.@Service等等 3.通过@Bean注解 比如下面的代码往容器中注册一个Person对象 @Bean public Person person(){ return ne…
xml配置方式 首先我们创建一个实体类Person public class Person { private String name; private Integer age; private String nickName; // 省略getter and setter ,tostring ,Constructor 1} 以往,我们在spring的配置文件中注册一个bean,采用以下写法: <?xml version="1.0" encoding="UTF-8&qu…
1.Spring容器的创建会经历refresh()方法[创建刷新](以AnnotationConfigApplicationContext为例) public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) { this(); register(annotatedClasses); refresh(); } 2. refresh()会经历的过程: //AbstractApplicationContext.java…
浅尝Spring注解开发_Spring容器创建概述 浅尝Spring注解开发,基于Spring 4.3.12 概述Spring容器创建的过程,包括12个方法的执行 浅尝Spring注解开发_自定义注册组件.属性赋值.自动装配 浅尝Spring注解开发_Bean生命周期及执行过程 浅尝Spring注解开发_AOP原理及完整过程分析(源码) 浅尝Spring注解开发_声明式事务及原理 浅尝Spring注解开发_简单理解BeanFactoryPostProcessor.BeanDefinitionRe…