Spring注解(生命周期)
对于上面的知识图解,需要一点一点的研究。
首先核心容器:
控制反转 和 依赖注入
创建工程:
maven仓库搜索 spring context :
引入后
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
以前是通过 application.xml 进行配置设置
配置类 等同于以前的配置文件:
package com.toov5.bean;
public class Person {
int age;
String name;
public Person() {
}
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
}
config:
package com.toov5.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.toov5.bean.Person;
@Configuration
public class config {
@Bean //给容器注册一个Bean,类型为返回值的类型。xml中的id是用方法作为id
public Person person() {
return new Person(1, "toov5");
}
}
测试类:
package com.toov5.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.toov5.bean.Person;
import com.toov5.config.config;
public class test {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext applicationContex = new AnnotationConfigApplicationContext(config.class); // 之前是传递配置文件的位置
// 现在是我们设计的配置类的位置
Person bean = applicationContex.getBean(Person.class); // 通过类型去获取
System.out.println(bean);
String[] beanNamesForType = applicationContex.getBeanNamesForType(Person.class); // 根据类型找到bean的名字
for (String name : beanNamesForType) {
System.out.println(name); // 返回bean的名字 我们可以在cofig中配置@Bean的名字
}
}
}
在xml配置的时候,我们使用的是包扫描的方式
<context: component-scan base-package= "com.toov5"> </context: conponent-scan>
包扫描: 只要标注了 @Controller @Service @Repository @Component
用注解搞定: @ComponentScan(value="com.toov5.bean")
可以指定要扫描的包
排除的包,包括可以按照 名字 按照类型等去排除 excludeFilters
指定的包,指定扫描的时候只需要包含哪些组件 includeFilters
还可以通过自定义规则 CUSTOM 通过 implements TypeFilter 重写match方法
metadataReader : 读取到的当前正在扫描的类的信息
metadataReaderFactory: 可以获取到其他任何类
config:
package com.toov5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
@Configuration //标记@Service 和 @Controller的
@ComponentScan(value="com.toov5",excludeFilters = {
@Filter(type=FilterType.ANNOTATION,classes= { Controller.class,Service.class })})
//@ComponentScan(value="com.toov5")
public class config {
}
controller
package com.toov5.controller;
import org.springframework.stereotype.Controller;
@Controller
public class BookController {
}
Service
package com.toov5.service;
import org.springframework.stereotype.Service;
@Service
public class BookService {
}
Dao
package com.toov5.dao;
import org.springframework.stereotype.Repository;
@Repository
public class BookDao {
}
测试:
package com.toov5.test;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.toov5.config.config;
public class test {
@SuppressWarnings("resource")
@Test
public void test01(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(config.class);
String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
for(String name : beanDefinitionNames) {
System.out.println(name);
}
}
}
config类:
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
@Configuration //标记@Service 和 @Controller的
@ComponentScan(value="com.toov5",excludeFilters = {
// @Filter(type=FilterType.ANNOTATION,classes= { Controller.class,Service.class }),
@Filter(type=FilterType.CUSTOM, classes= {MyTypeFilter.class})
})
//@ComponentScan(value="com.toov5")
public class config {
}
自定义的:
package com.toov5.config;
import java.io.IOException;
import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
public class MyTypeFilter implements org.springframework.core.type.filter.TypeFilter{
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
throws IOException {
//获取当前类注解的信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
//获取当前正在扫描的类的类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
//获取当前资源(类