AnnotationConfigApplicationContext是spring中利用注解配置的方式构建spring上下文的类。
第一种方式:构造函数传入一个或者多个类。可以是注解类,也可以是普通类,传入的类都会纳入到spring容器中。如下:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.edu.spring</groupId> <artifactId>spring</artifactId> <version>1.0.0</version> <name>spring</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.2.RELEASE</version> </dependency> </dependencies> </project>
User.java、Cat.java、Dog.java、UserDao.java、UserService.java、MyConfig.java如下:
package com.edu.spring; public class User { } package com.edu.spring; public class Cat { } package com.edu.spring; public class Dog { } package com.edu.spring; import org.springframework.stereotype.Component; @Component public class UserDao { } package com.edu.spring; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Service; @Service public class UserService { @Bean public Cat createCat(){ return new Cat(); } } package com.edu.spring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfig { @Bean public Dog createDog(){ return new Dog(); } }
App.java
package com.edu.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(User.class,UserDao.class,UserService.class,MyConfig.class); System.out.println(context.getBean(User.class)); System.out.println(context.getBean(Cat.class)); System.out.println(context.getBean(Dog.class)); System.out.println(context.getBean(UserDao.class)); System.out.println(context.getBean(UserService.class)); System.out.println(context.getBean(MyConfig.class)); context.close(); } }
运行结果如下:
第二种方式:构造函数传入一个包名,该包下的类都会创建bean,并且纳入spring容器中
App.java,User.class没有加注解,不会创建bean
package com.edu.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext("com.edu.spring"); // System.out.println(context.getBean(User.class)); System.out.println(context.getBean(Cat.class)); System.out.println(context.getBean(Dog.class)); System.out.println(context.getBean(UserDao.class)); System.out.println(context.getBean(UserService.class)); System.out.println(context.getBean(MyConfig.class)); context.close(); } }
运行结果如下:
第三种方式:构造函数传入一个加了@ComponentScan注解的类,该注解指明了扫描的包和排除掉的类
AnnotationScan.java
package com.edu.spring; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.FilterType; @ComponentScan(basePackages="com.edu.spring", excludeFilters=@Filter(type=FilterType.ASSIGNABLE_TYPE,classes={MyConfig.class,UserService.class})) public class AnnotationScan { }
App.java 代码中注销掉的类都不会创建bean,因为排除掉了
package com.edu.spring; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { public static void main( String[] args ) { AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AnnotationScan.class); //System.out.println(context.getBean(User.class)); //System.out.println(context.getBean(Cat.class)); //System.out.println(context.getBean(Dog.class)); System.out.println(context.getBean(UserDao.class)); //System.out.println(context.getBean(UserService.class)); //System.out.println(context.getBean(MyConfig.class)); context.close(); } }
运行结果如下: