spring容器获取ApplicationContext几种方法
1、使用@Autowired注解
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
package com.edu.spring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class User { @Autowired private ApplicationContext applicationContext; public void show(){ System.out.println("user: "+applicationContext); } }
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("com.edu.spring"); context.getBean("user",User.class).show(); context.close(); } }
运行结果如下:
2、实现ApplicationContextAware接口,在set方法中赋值
Book.java
package com.edu.spring2; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class Book implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.applicationContext=applicationContext; } public void show(){ System.out.println("book :"+applicationContext.getClass()); } }
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("com.edu.spring"); context.getBean("book",Book.class).show(); context.close(); } }
运行结果如下:
3、在构造函数中赋值(spring4.3中新特性),有局限性,只能有一个构造函数,如果有多个构造函数,会选择默认的无参的构造函数
Bank.java
package com.edu.spring; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class Bank { private ApplicationContext applicationContext; public Bank(ApplicationContext applicationContext){ this.applicationContext=applicationContext; } public void show(){ System.out.println("bank: "+applicationContext.getClass()); } }
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("com.edu.spring"); context.getBean("bank",Bank.class).show(); context.close(); } }
运行结果如下: