spring注解开发AnnotationConfigApplicationContext的使用

博观而约取,厚积而薄发。这篇文章主要讲述spring注解开发AnnotationConfigApplicationContext的使用相关的知识,希望能为你提供帮助。
【spring注解开发AnnotationConfigApplicationContext的使用】使用AnnotationConfigApplicationContext可以实现基于java的配置类加载Spring的应用上下文。避免使用application.xml进行配置。相比XML配置,更加便捷。
示例

spring注解开发AnnotationConfigApplicationContext的使用

文章图片

 
AppConfig.java
spring注解开发AnnotationConfigApplicationContext的使用

文章图片
package com.myapp.config; import com.myapp.Entitlement; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean(name="entitlement") public Entitlement entitlement() { Entitlement ent= new Entitlement(); ent.setName("Entitlement"); ent.setTime(1); return ent; }@Bean(name="entitlement2") public Entitlement entitlement2() { Entitlement ent= new Entitlement(); ent.setName("Entitlement2"); ent.setTime(2); return ent; } }

spring注解开发AnnotationConfigApplicationContext的使用

文章图片
注:
@Configuration可理解为用spring的时候xml里面的< beans> 标签
@Bean可理解为用spring的时候xml里面的< bean> 标签
Entitlement.java
spring注解开发AnnotationConfigApplicationContext的使用

文章图片
package com.myapp; public class Entitlement { private String name; private int time; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } }

spring注解开发AnnotationConfigApplicationContext的使用

文章图片
测试
JavaConfigTest.java
spring注解开发AnnotationConfigApplicationContext的使用

文章图片
package com.myapp; import com.myapp.config.AppConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class JavaConfigTest { public static void main(String[] arg) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); Entitlement ent = (Entitlement)ctx.getBean("entitlement"); System.out.println(ent.getName()); System.out.println(ent.getTime()); Entitlement ent2 = (Entitlement)ctx.getBean("entitlement2"); System.out.println(ent2.getName()); System.out.println(ent2.getTime()); ctx.close(); } }

spring注解开发AnnotationConfigApplicationContext的使用

文章图片
结果
Entitlement 1 Entitlement2 2

使用AnnotationConfigApplicationContext可以实现基于Java的配置类加载Spring的应用上下文。避免使用application.xml进行配置。相比XML配置,更加便捷。 示例
spring注解开发AnnotationConfigApplicationContext的使用

文章图片

 
AppConfig.java
spring注解开发AnnotationConfigApplicationContext的使用

文章图片
package com.myapp.config; import com.myapp.Entitlement; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean(name="entitlement") public Entitlement entitlement() { Entitlement ent= new Entitlement(); ent.setName("Entitlement"); ent.setTime(1); return ent; }@Bean(name="entitlement2") public Entitlement entitlement2() { Entitlement ent= new Entitlement(); ent.setName("Entitlement2"); ent.setTime(2); return ent; } }

spring注解开发AnnotationConfigApplicationContext的使用

文章图片
注:
@Configuration可理解为用spring的时候xml里面的< beans> 标签
@Bean可理解为用spring的时候xml里面的< bean> 标签
Entitlement.java
spring注解开发AnnotationConfigApplicationContext的使用

文章图片
package com.myapp; public class Entitlement { private String name; private int time; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } }

spring注解开发AnnotationConfigApplicationContext的使用

文章图片
测试
JavaConfigTest.java
spring注解开发AnnotationConfigApplicationContext的使用

文章图片
package com.myapp; import com.myapp.config.AppConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class JavaConfigTest { public static void main(String[] arg) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); Entitlement ent = (Entitlement)ctx.getBean("entitlement"); System.out.println(ent.getName()); System.out.println(ent.getTime()); Entitlement ent2 = (Entitlement)ctx.getBean("entitlement2"); System.out.println(ent2.getName()); System.out.println(ent2.getTime()); ctx.close(); } }

spring注解开发AnnotationConfigApplicationContext的使用

文章图片
结果
Entitlement 1 Entitlement2 2


    推荐阅读