spring深度解析:组件注册

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35165632/article/details/82750176

1.注入方式

一丶传统的配置方式注入

随着时代不断地变迁,曾经用的最多的加载方式配置文件方式,已经渐渐的远离大众的视野,所以在这个章节里也不做过多介绍。

二丶注解的方式注入

随着spring boot 快速开发,以及越来越多人开始使用springcloud。大家也逐渐舍弃了传统的spring mvc 模式。注解方式的注入的使用就逐渐广泛起来。本章节讲简单介绍配置注入的方式和基本参数。

创建实体类Person,并补充get set 方法,或者用 lombok注解替代 getset方法,这里就列举传统的实体类方式。

package com.demo.bean;

public class Person {
    private String name;

    private String id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}

创建spring注入类 demoConfiguration 

package com.demo.config;

import com.demo.bean.Person;
import com.demo.service.BookService;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Configuration
public class demoConfiguration {
    @Bean()
    public Person person(){
        Person person = new Person();
        person.setId("1");
        person.setName("lsl");
        return person;
    }
}

创建spring环境测试类 

package com.demo.test;

import com.demo.bean.Person;
import com.demo.config.MianConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class  demoTest{


    public static void main(String[]agrs){

        /**
         * 传统的配置方式的注入
         */
//    ApplicationContext applicationContext 
//    = new ClassPathXmlApplicationContext("spring-bean.xml");
//        Person person = (Person) applicationContext.getBean("person");
//        System.out.println(person.getId());
//        System.out.println(person.getName());

        /**
         * 注解的方式注入
         */
        ApplicationContext applicationContext
        = new AnnotationConfigApplicationContext(MianConfiguration.class);
        Person person1 = (Person)applicationContext1.getBean("person");
        System.out.println(person1.getId());
        System.out.println(person1.getName());
    }
}

下一步想要延续spring mvc 传统的三层架构 

我们都知道,spring mvc 传统需要配置包扫描的路径那么,注解的方式如何配置呢?

package com.demo.config;

import com.demo.bean.Person;
import com.demo.service.BookService;
import org.springframework.context.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Configuration
@ComponentScan(value = { "com.demo.controller", "com.demo.service", "com.demo.dao"})
public class demoConfiguration {
    @Bean()
    public Person person(){
        Person person = new Person();
        person.setId("1");
        person.setName("lsl");
        return person;
    }
}

按照如上配置就可以了。那么如何配置包含过滤器和除去过滤器呢?其实两者配置几近相同,直有某个参数需要注意。

//配置类==配置文件
@Configuration  //告诉Spring这是一个配置类

@ComponentScans(
		value = {
    @ComponentScan(value="com.atguigu",includeFilters = {
/*	@Filter(type=FilterType.ANNOTATION,classes={Controller.class}),
	@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={BookService.class}),*/
	@Filter(type=FilterType.CUSTOM,classes={MyTypeFilter.class})},useDefaultFilters =false)	
		}
		)
//@ComponentScan  value:指定要扫描的包
//excludeFilters = Filter[] :指定扫描的时候按照什么规则排除那些组件
//includeFilters = Filter[] :指定扫描的时候只需要包含哪些组件,在包含的时候需要添加参数 
//useDefaultFilters = false 过滤默认的过滤器 
//FilterType.ANNOTATION:按照注解
//FilterType.ASSIGNABLE_TYPE:按照给定的类型;
//FilterType.ASPECTJ:使用ASPECTJ表达式
//FilterType.REGEX:使用正则指定
//FilterType.CUSTOM:使用自定义规则(需要实现TypeFilter接口)
public class MainConfig {
	
	//给容器中注册一个Bean;类型为返回值的类型,id默认是用方法名作为id
	@Bean("person")
	public Person person01(){
		return new Person("lisi", 20);
	}

}

基本的扫描完成了,那么bean的类型有哪些呢?

Bean的类型总共有四种,其中常用的两种分别是 prototype 以及 singleton

prototype 类型 是在获取bean实例的时候对bean进行初始化,同一个spring环境下会有多个同名bean

singleton 类型 实在注册的时候就对bean进行初始化(不使用懒加载的情况下)

使用的注解是  @scope

而提到的懒加载就是为了节省系统资源在bean实例被调用时才使用  注解为 @lazy

猜你喜欢

转载自blog.csdn.net/qq_35165632/article/details/82750176
今日推荐