JavaConfig 使用Java代码进行显示配置
从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。
需要先加载spring-context 包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.6.RELEASE</version> </dependency>
代码:
package com.company; /** * Created by Administrator on 2017/2/22 0022. */ public interface ISay { void say(String msg); }
package com.company; /** * Created by Administrator on 2017/2/22 0022. */ public class SayImpl implements com.company.ISay { public void say(String msg){ System.out.println("Person Say:"+msg); } }
使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。
package com.company; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Created by Administrator on 2017/2/22 0022. */ @Configuration public class JavaConfig { @Bean(name="GetSay") public com.company.ISay GetSay(){ return new com.company.SayImpl(); } }
package com.company; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context =new AnnotationConfigApplicationContext(com.company.JavaConfig.class); com.company.ISay obj=(com.company.ISay) context.getBean("GetSay"); obj.say("hongdada"); } }
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Person Say:hongdada
多个Bean
package com.company; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; interface ISay { String Country="China"; void say(String msg); } class SayImpl implements com.company.ISay { public void say(String msg){ System.out.println("Person Say:"+msg); } } class SayImpl2 implements com.company.ISay { public void say(String msg){ System.out.println(com.company.ISay.Country+" Person2 Say:"+msg); } } @Configuration class JavaConfig { @Bean(name="GetSay") public com.company.ISay GetSay(){ return new com.company.SayImpl(); } @Bean(name="GetSay2") public com.company.ISay GetSayTwo(){ return new com.company.SayImpl2(); } } public class Main { public static void main(String[] args) { ApplicationContext context =new AnnotationConfigApplicationContext(com.company.JavaConfig.class); com.company.ISay obj=(com.company.ISay) context.getBean("GetSay"); obj.say("hongdada"); com.company.ISay obj2=(com.company.ISay) context.getBean("GetSay2"); obj2.say("hongdada"); } }
Person Say:hongdada
China Person2 Say:hongdada
另外一种获取Bean的方式:
package com.company; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; interface ISay { String Country="China"; void say(String msg); } class SayImpl implements com.company.ISay { public void say(String msg){ System.out.println("Person Say:"+msg); } } class SayImpl2 implements com.company.ISay { public void say(String msg){ System.out.println(com.company.ISay.Country+" Person2 Say:"+msg); } } @Configuration class JavaConfig { @Bean(name="GetSay") public com.company.ISay GetSay(){ return new com.company.SayImpl(); } // @Bean(name="GetSay2") // public com.company.ISay GetSayTwo(){ // return new com.company.SayImpl2(); // } } public class Main { public static void main(String[] args) { // ApplicationContext context =new AnnotationConfigApplicationContext(com.company.JavaConfig.class); // com.company.ISay obj=(com.company.ISay) context.getBean("GetSay"); // obj.say("hongdada"); // // com.company.ISay obj2=(com.company.ISay) context.getBean("GetSay2"); // obj2.say("hongdada"); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(JavaConfig.class); ctx.refresh(); ISay obj=ctx.getBean(ISay.class); obj.say("hongdada"); } }
Person Say:hongdada
可以发现,这里面直接根据ISay.class来获取对应的Bean,那么JavaConfig里面必须只有一个返回值是ISay的Bean,不然就会报错。
Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.company.ISay' available: expected single matching bean but found 2: GetSay,GetSay2 at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1041) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093) at com.company.Main.main(Main.java:49) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
========================
@ImportClass
http://www.manongjc.com/article/1603.html