没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐

Spring 源码深度解析与注解驱动开发
第1节 文档结构
1.1 目录视图
Spring 注解驱动开发
(1)容器
·AnnotationConfigApplicationContext
·组件添加
·组件赋值
·组件注入
·AOP
·声明式事务
(2)扩展原理
·BeanFactoryPostProcessor
·BeanDefinitionRegistryPostProcessor
·ApplicationListener
·Spring 容器创建过程
(3)Web
·Servlet3.0
·异步请求
第 2 节 组件注册
2.1 @Configuration&@Bean 给容器中注册组件
<?xml version="1.0" encoding="UTF-8"?>
<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="com.byf.bean.Person">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
</bean>
</beans>
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("beans.xml");
Person person = (Person) applicationContext.getBean("person");
System.out.println(person);
@Configuration
public class PersonConfig {
@Bean("lisi")
public Person person(){
return new Person("李四", 21);
}
}
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(PersonConfig.class);
Person person = (Person) applicationContext.getBean("lisi");
System.out.println(person);
2.2 @ComponentScan-自动扫描组件&指定扫描规则
@Configuration
@ComponentScan(value = "com.byf", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes =
{Controller.class})
},useDefaultFilters = false)
@ComponentScans(value = {@ComponentScan(value = "com.byf", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})
},useDefaultFilters = false)})
public class PersonConfig {
@Bean("lisi")
public Person person(){
return new Person("李四", 21);
}
}

public class IOCTest {
@Test
public void testIOC(){
ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(PersonConfig.class);
String[] beanNames = applicationContext.getBeanDefinitionNames();
for(String name : beanNames){
System.out.println(name);
}
}
}
personConfig
bookController
bookService
lisi
2.3 自定义 TypeFilter 指定过滤规则
@Configuration
@ComponentScans(value = {@ComponentScan(value = "com.byf", includeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes =
{Controller.class}),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes =
{BookService.class}),
@ComponentScan.Filter(type = FilterType.CUSTOM, value = {MyTypeFilter.class})
},useDefaultFilters = false)})
//FilterType.ANNOTATION:根据注解类注入
//FilterType.ASSIGNABLE_TYPE: 根据 class 名注入
//FilterType.ASPECTJ: 根据 ASPECTJ 表达式
//FilterType.REGEX:根据正则表达式
//FilterType.CUSTOM:根据自定义规则
/*@ComponentScans(value = {@ComponentScan(value = "com.byf", excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})
})})*/
public class PersonConfig {
@Bean("lisi")
public Person person(){
return new Person("李四", 21);

}
}
public class MyTypeFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader, MetadataReaderFactory
metadataReaderFactory) throws IOException {
AnnotationMetadata annotationMetadata =
metadataReader.getAnnotationMetadata();
ClassMetadata classMetadata = metadataReader.getClassMetadata();
Resource resource = metadataReader.getResource();
String name = classMetadata.getClassName();
System.out.println("-->" + name);
if ("er".equals(name.substring(name.length()-2,name.length()))){
return true;
}
return false;
}
}
-->com.byf.AppTest
-->com.byf.bean.IOCTest
-->com.byf.App
-->com.byf.bean.Person
-->com.byf.config.MyTypeFilter
-->com.byf.controller.BookController
-->com.byf.dao.BookDao
-->com.byf.service.BookService
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
.....
personConfig
myTypeFilter
bookController
lisi

2.4@Scope-设置组件作用域
@Configuration
public class MainConfig2 {
// 默认是单实例的
/**
* @see ConfigurableBeanFactory#SCOPE_PROTOTYPE
* @see ConfigurableBeanFactory#SCOPE_SINGLETON
* prototype:多实例
* singleton:单实例(默认值):ioc 容器启动会调用方法创建对象放到 ioc 容器中。
* 以后每次获取就是直接从容器(map.get())中拿
* request:同一个请求创建一个实例
* session:同一个 session 创建一个实例
*/
@Scope(value = "prototype")
@Bean("person")
public Person person(){
System.out.println("给容器添加 Person...");
return new Person("李四", 21);
}
}
@Test
public void testIOC2(){
ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(MainConfig2.class);
System.out.println("ioc 容器创建完成....");
Object p1 = applicationContext.getBean("person");
Object p2 = applicationContext.getBean("person");
System.out.println(p1 == p2);
/*String[] beanNames = applicationContext.getBeanDefinitionNames();
for(String name : beanNames){
System.out.println(name);
}
Object p1 = applicationContext.getBean("person");
Object p2 = applicationContext.getBean("person");
System.out.println(p1 == p2);*/
剩余87页未读,继续阅读


















臭人鹏
- 粉丝: 6
- 资源: 329

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制

评论0