Fiter的信息如下:

Filter的类型有:annotation(这是spring默认的),assignable,aspectj, regex,custom

首先看一下我这个demo的目录结构:

上图中所有被红色圆圈圈住的都是本demo要写的代码:

AppConfig的代码如下:

package com.timo.resourceJavaConfig;
import com.timo.entity.Master;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Repository; /**
* The following example shows the configuration ignoring all @Repository annotations and using
"stub" repositories instead.
*/
@Configuration
@ComponentScan(basePackageClasses = Master.class,
includeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern =
".*Stub.*Repository"),
excludeFilters = @ComponentScan.Filter(Repository.class))
public class AppConfig {
}

等价于用xml写的:appconfig.xml的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--<context:annotation-config/> only looks for annotations on beans in the same application context in which
it is defined RequiredAnnotationBeanPostProcessor AutowiredAnnotaionBeanPostProcessor
CommonAnnotationBeanPostProcessor PersistenceAnnotaionBeanPostProcessor-->
<context:component-scan base-package="com.timo.entity" annotation-config="true">
<context:include-filter type="regex" expression=".*Sub.*Respository"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
</beans>

com.timo.entity包下

Dog的代码如下:

package com.timo.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; /**
* 这里可以写@Component,@Service,@Controller注解,写@Repository注解会报错
* 报错的原因是在配置文件或者注解排除了@Repository.
*/
@Service
public class Dog {
@Value("pug")
private String name;
@Value("")
private Integer age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

Master的代码如下:

package com.timo.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class Master {
@Autowired
private Dog dog; public Dog getDog() {
return dog;
} public void setDog(Dog dog) {
this.dog = dog;
}
public void showDogInfo(){
System.out.println("name="+dog.getName());
System.out.println("age="+dog.getAge());
}
}

测试用xml的代码如下:

Test4的代码如下:

package com.timo.test2;

import com.timo.entity.Dog;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test4 {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("appconfig.xml");
Dog dog = applicationContext.getBean(Dog.class);
System.out.println("name="+dog.getName());
System.out.println("age="+dog.getAge());
}
}

测试用注解写的Test3的代码如下:

package com.timo.test2;

import com.timo.entity.Dog;
import com.timo.resourceJavaConfig.AppConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Test3 {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
Dog dog = applicationContext.getBean(Dog.class);
System.out.println("dog="+dog);
}
}

最新文章

  1. final 140字评论
  2. psp进度(11月25号-31号)
  3. 【SFTP】使用Jsch实现Sftp文件下载-支持断点续传和进程监控
  4. virtualenv中ImportError: No module named django
  5. CMD规范
  6. Swing(一):JFrame框架窗体
  7. 百度UEditor开发案例(JSP)
  8. NFC简介
  9. 怎样实现多文件上传 在iOS开发中
  10. .NET vs JAVA
  11. MySQL错误:You are using safe update mode and you tried to update a table without a WHERE that uses a K
  12. javascript实现浏览器窗口大小被改变时触发事件的方法
  13. 用js来实现那些数据结构07(链表01-链表的实现)
  14. Nowcoder | [题解-N189]牛客OI赛制测试赛3
  15. Java队列——线程池创建的例子
  16. Django Rest Framework源码剖析(七)-----分页
  17. 百度地图Api进阶教程-基础地图示例1.html
  18. debian卸载旧内核
  19. 获取手机已安装应用的name,bundleIdentitifer
  20. 【淘宝客】PHPMailer-v6.0.5版 发送邮件dome

热门文章

  1. Ubuntu装完后要做的几件事
  2. ABAP CDS ON HANA-(5)テーブル結合ビュー
  3. 关于xampp 集成开发包电脑重启mysql无法启动的问题
  4. linux c 出错集
  5. JQuery中的load()、$.get()和$.post()详解 (转)
  6. 【python3.X】python练习笔记[1]
  7. linux 操作之一 如何在linux将本地数据*.sql文件导入到linux 云服务器上的mysql数据库
  8. find的详细使用
  9. Android Studio Gradle编译时『No resource found that matches the given name』解决方法(windows系统的坑)
  10. Java IO学习--RandomAccessFile