【P说】Spring基础知识

Maven Spring

Spring的Maven依赖:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>

各个版本的依赖可以在以下链接查看:
https://mvnrepository.com/artifact/org.springframework/spring-context

通过XML配置文件注入bean

src/main/resources下新建Spring的配置文件,名称随意,这里就叫beans.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="person" class="cap1.Person">
		<property name="name" value="Chovy">property>
		<property name="age" value="18">property>
	bean>
beans>

这里配置了一个Person对象,通过一些代码可以就配置文件配置到AppcalitionContext。通过getBean就可以获得容器中的对象。

public class Test {
	public static void main(String[] args) {
		ApplicationContext app = new ClassPathXmlApplicationContext("beans.xml"); //加载配置文件,生成ApplicationContext 
		System.out.println(app.getBean("person"));
	}
}

注释注入

使用注释注释java类作为context的配置。

@Configuration  //表示该类为配置类
public class BeanConfig {

	@Bean  //将方法返回的对象添加到容器(MAP),容器中该对象的key默认为方法名,@Bean(value="key")可以用这种方式修改key值
	public Person person() {
		return new Person("chris",18);
	}
}

使用AnnotationConfigApplicationContext生成ApplicationContext:

public class Test2 {
	public static void main(String[] args) {
		AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(BeanConfig.class); //加载配置类,生成ApplicationContext
		System.out.println(app.getBean("person"));
	}
}

@ComponentScan扫描

通过@ComponentScan可以指定在那个包(与其子包)扫描带有对应注释的类,将这些类生成的对象放入容器(类需要提供无参构造函数)。被以下注释注释的类将会被扫描:

  • @Component
  • @Service
  • @Repository
  • @Controller
@Configuration
@ComponentScan(basePackages = {"cap3"}) //将会扫描cap3包与其子包下的类
public class BeanConfig {

	@Bean
	public Person person() {
		return new Person("chris",18);
	}
}

includeFilters

includeFilters可以用来指定符合特定条件的类才会被扫描进容器。

@Configuration
@ComponentScan(basePackages = {"cap3"},includeFilters = {
		@Filter(type = FilterType.ANNOTATION,value=Component.class) //FilterType.ANNOTATION表示扫描注解,value=Component.class表示Component注释。
},useDefaultFilters = false) //useDefaultFilters设置为false表示不使用默认的过滤器,使得我们的includeFilters生效
public class BeanConfig {

	@Bean
	public Person person() {
		return new Person("chris",18);
	}
}

Filter的类型还有
【P说】Spring基础知识_第1张图片
当然,也可以自己定义过滤器:

public class MyFilter implements TypeFilter {

	public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException {
		//metadataReader可以获取当前正在被扫描类的信息
		//metadataReaderFactory可以获取全部类的信息
		return false;//返回true表示通过,false表示不通过
	}

}

使用:

@ComponentScan(basePackages = {"cap3"},includeFilters = {
		@Filter(type = FilterType.CUSTOM,value=MyFilter.class)
},useDefaultFilters = false)

excludeFilters

excludeFilters的作用与includeFilters相反,用来排除一下特定的类进入到容器。

@ComponentScan(basePackages = {"cap3"},excludeFilters  = {
		@Filter(type = FilterType.CUSTOM,value=MyFilter.class)
})

@Scope

Spring默认扫描生成的对象都是单例模式的,通过@Scope注释可以指定通过单例还是其他模式生成对象。

 * @see ConfigurableBeanFactory#SCOPE_PROTOTYPE //多例模式
 * @see ConfigurableBeanFactory#SCOPE_SINGLETON //单例模式
 * @see org.springframework.web.context.WebApplicationContext#SCOPE_REQUEST //请求级别
 * @see org.springframework.web.context.WebApplicationContext#SCOPE_SESSION //会话级别

使用:

@Configuration
public class BeanConfig {

	@Bean
	@Scope("prototype") //使用多例模式,只有在获取对象的时候才会新建对象
	public Person person() {
		return new Person("chris",18);
	}
}

单例与多例

  • singleton(单例):只有一个共享的实例存在,所有对这个bean的请求都会返回这个唯一的实例。
  • prototype(多例):对这个bean的每次请求都会创建一个新的bean实例,类似于new。

@lazy

在单例模式下,默认在容器创建时就会把单例对象新建好并放入到容器中,使用@lazy注释可以使用懒加载的方式,只有获取对象的时候才会新建。使用:

@Configuration
public class BeanConfig {

	@Bean
	@Lazy
	public Person person() {
		return new Person("chris",18);
	}
}

@Conditional

@Conditional可以用来控制容器注入满足条件的bean。
使用:

  1. 条件控制类实现Condition接口
public class MyConditional implements Condition {

//AnnotatedTypeMetadata:可以获得扫描类的注解信息
//ConditionContext: 可以获得上下文环境
	public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
		// TODO Auto-generated method stub
		return true; //返回true时注入,false不注入
	}
}
  1. 注入bean时,加上条件
@Configuration
public class BeanConfig {

	@Bean
	@Conditional(MyConditional.class) //引入条件判断
	public Person person() {
		return new Person("chris",18);
	}
}

你可能感兴趣的:(Spring)