Fiter information is as follows:
The filter types are: annotation (this is spring default), ASSIGNABLE,ASPECTJ, Regex,custom
First look at the directory structure of my demo:
All the red circles are circled by this demo to write the code:
The code for AppConfig is as follows:
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 I Nstead. */@Configuration @componentscan (basepackageclasses= Master.class, Includefilters= @ComponentScan. Filter (type = Filtertype.regex, pattern =". *stub.*repository"), Excludefilters= @ComponentScan. Filter (Repository.class)) Public classAppConfig {}
Equivalent to writing in XML: The Appconfig.xml code is as follows:
<?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/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context.xsd "><!--<context:annotation-config/> only looks forAnnotations on BeansinchThe same application contextinchwhich it isdefined Requiredannotationbeanpostprocessor autowiredannotaionbeanpostprocessor Commonannotationbeanpostprocessor Persistenceannotaionbeanpostprocessor-<context:component-scanBase-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 under the Bag
The code for Dog is as follows:
Package Com.timo.entity;import Org.springframework.beans.factory.annotation.value;import Org.springframework.stereotype.repository;import Org.springframework.stereotype.Service;/** * Here can write @component, @Service, @Controller annotations, write @repository comments will be error * The reason for the error is to exclude the @repository in the configuration file or annotations.*/@Service Public classDog {@Value ("Pug") PrivateString name; @Value (" -") PrivateInteger age; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; }}
The code for Master is as follows:
Package Com.timo.entity;import Org.springframework.beans.factory.annotation.autowired;import org.springframework.stereotype.Component; @Component Public classMaster {@AutowiredPrivateDog Dog; PublicDog Getdog () {returnDog; } Public voidSetdog (dog dog) { This. Dog =Dog; } Public voidShowdoginfo () {System. out. println ("name="+dog.getname ()); System. out. println ("age="+dog.getage ()); }}
The code for the test XML is as follows:
The code for TEST4 is as follows:
Package Com.timo.test2;import Com.timo.entity.dog;import Org.springframework.context.support.ClassPathXmlApplicationContext; Public classTest4 { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext ApplicationContext=NewClasspathxmlapplicationcontext ("Appconfig.xml"); Dog Dog= Applicationcontext.getbean (Dog.class); System. out. println ("name="+dog.getname ()); System. out. println ("age="+dog.getage ()); }}
The code for testing the TEST3 written with annotations is as follows:
Package Com.timo.test2;import Com.timo.entity.dog;import com.timo.resourcejavaconfig.appconfig;import Org.springframework.context.annotation.AnnotationConfigApplicationContext; Public classTest3 { Public Static voidMain (string[] args) {Annotationconfigapplicationcontext ApplicationContext=NewAnnotationconfigapplicationcontext (AppConfig.class); Dog Dog= Applicationcontext.getbean (Dog.class); System. out. println ("dog="+dog); }}
Custom spring package scanning with filters