1, the configuration dependent
xml scan and automatically detect packet and register the class definitions [used] Bean
<? XML Version = "1.0" encoding = "UTF-. 8" ?> < Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " xmlns: context =" http://www.springframework.org/schema/context " xsi: schemaLocation =" http://www.springframework.org/schema/beans HTTPS: // the WWW. springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd " > <! - - scan base-package and automatically detect and register the class definitions Bean -> <context:component-scan base-package="com.doubleh.pojo"/> </beans>
2 comment
@Component 2.1 - defined in the class, the bean is registered; equivalent @ Service, @ Controller, @ Repository
@Scope 2.2 - defined scope of the bean, acting on the class
@Value 2.3 () - defined on attributes assigned
@Autowired 2.4 - set the properties and methods defined in the class
@ComponentScan 2.5 - scan package, with a configuration using Java beans manner.
You can be modified and expanded by the application of this behavior custom filter, adding them to includeFilters
or comment excludeFilters
attributes @ComponentScan
(or XML configuration element <context:include-filter />
or <context:exclude-filter />
sub-element <context:component-scan>
). Each filter element needs type
and expression
properties.
Filter Type | Examples of expression | description |
---|---|---|
Notes (default) |
|
In the level of the target assembly type on the presence or element present annotations . |
Assignable |
|
It can be assigned to the target component (or extended implementation) class (or interface). |
aspect |
|
AspectJ type expressions to match the target component. |
Regular Expressions |
|
To the class name of the target component matches the regular expression. |
custom |
|
|
@Configuration @ComponentScan(basePackages = "org.example", includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"), excludeFilters = @Filter(Repository.class)) public class AppConfig { ... }
<beans> <context:component-scan base-package="org.example"> <context:include-filter type="regex" expression=".*Stub.*Repository"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> </beans>
3, entirely in Java configuration Spring fashion
Even Spring xml configuration files are not needed.
3.1 class defines a configuration:
// This will be hosted Spring container, registered to container, as is a @Configuration @Component // @Configuration represent this class is a configuration equivalent to <Beans> </ Beans> .xml @Configuration // scan All packets in com.doubleh.pojo @ComponentScan ( "com.doubleh.pojo" )
// Import @ (other configurations based) public class AppConfig { // equivalent to <the bean> </ the bean> // method name is ID the bean // the return value class is the bean // @Bean // public Cat CAT () { // return new new Cat (); // } }
@ComponentScan("com.doubleh.pojo")
等价于 beans.xml 的
<context:component-scan base-package="com.doubleh.pojo"/>
3.2 If a bean:
Use @ComponentScan scan manner must have an annotation on the bean class declaration, the default bean name is the first letter lowercase class name; if @Bean declared in the configuration class, you can not write notes on the class, and is generally not omitted in the configuration class write.
//注册为一个bean @Component public class Cat { private int id; private String name; @Value("2") public void setId(int id) { this.id = id; } @Override public String toString() { return "Cat{" + "id=" + id + ", name='" + name + '\'' + '}'; } @Value("李白") public void setName(String name) { this.name = name; } }
3.3 使用AnnotationConfigApplicationContext:
// If used to configure class way to do it, we use AnnotationConfigApplicationContext to get the context
ApplicationContext applicationContext = new new AnnotationConfigApplicationContext (appconfig. Class );
Cat getCat = to applicationContext.getBean ( "CAT", Cat. Class );
System.out.println (getCat);
// through the register to the configuration register class acquire context
AnnotationConfigApplicationContext CTX = new new AnnotationConfigApplicationContext ();
ctx.register (AppConfig.class, OtherConfig.class);
ctx.register (AdditionalConfig.class);
ctx.refresh ();
// third @Component
or with JSR-330 -based annotation provided as input to the constructor
ApplicationContext ctx =new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
//第四种 用的时候再去扫描
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.acme");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);