从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。
但是,仍然允许使用经典的XML方式来定义bean和配置,JavaConfig是另一种替代解决方案。
下面是JavaConfig简单例子
-
1 定义一个简单的Spring Bean
JavaConfig.java 接口
package com.gp6.service;
public interface JavaConfig {
void testJavaConfig(String msg);
}
JavaConfigImpl.java
package com.gp6.service.impl;
import com.gp6.service.JavaConfig;
public class JavaConfigImpl implements JavaConfig {
@Override
public void testJavaConfig(String msg) {
System.out.println("这里是JavaConfig:::" + msg);
}
}
-
2. JavaConfig 注解
使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。
package com.gp6.help;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gp6.service.JavaConfig;
import com.gp6.service.impl.JavaConfigImpl;
@Configuration
public class JavaConfigTest {
@Bean(name="javaConfig")
public JavaConfig helloWorld() {
return new JavaConfigImpl();
}
}
-
3 执行结果
使用 AnnotationConfigApplicationContext 加载您的JavaConfig类
package com.gp6.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.gp6.help.JavaConfigTest;
import com.gp6.service.JavaConfig;
public class SpringTest04 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfigTest.class);
JavaConfig obj = (JavaConfig) context.getBean("javaConfig");
obj.testJavaConfig("Spring Java Config");
}
}
输出结果
这里是JavaConfig:::Spring Java Config
JavaConfig @Import实例
-
1 Spring Beans
File : JavaConfigImport01.java
package com.gp6.bean;
public class JavaConfigImport01 {
public void printMsg(String msg) {
System.out.println("JavaConfigImport01 " + msg);
}
}
File : JavaConfigImport02.java
package com.gp6.bean;
public class JavaConfigImport02 {
public void printMsg(String msg) {
System.out.println("JavaConfigImport02 " + msg);
}
}
-
2 @Configuration示例
现在,使用JavaConfig @Configuration声明上述Bean类。
File : JavaConfigImportTest01.java
package com.gp6.help;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gp6.bean.JavaConfigImport01;
import com.gp6.service.JavaConfig;
import com.gp6.service.impl.JavaConfigImpl;
@Configuration
public class JavaConfigImportTest01 {
@Bean(name="import01")
public JavaConfigImport01 customerBo(){
return new JavaConfigImport01();
}
}
File : JavaConfigImportTest02.java
package com.gp6.help;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gp6.bean.JavaConfigImport01;
import com.gp6.bean.JavaConfigImport02;
import com.gp6.service.JavaConfig;
import com.gp6.service.impl.JavaConfigImpl;
@Configuration
public class JavaConfigImportTest02 {
@Bean(name="import02")
public JavaConfigImport02 customerBo(){
return new JavaConfigImport02();
}
}
-
3 @Import示例
使用@Import加载多个配置文件。
File : JavaConfigImportTestAll.java
package com.gp6.help;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Configuration
@Import({ JavaConfigImportTest01.class, JavaConfigImportTest02.class })
public class JavaConfigImportTestAll {
}
-
4 执行程序
加载主配置文件,并进行测试。
package com.gp6.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.gp6.bean.JavaConfigImport01;
import com.gp6.bean.JavaConfigImport02;
import com.gp6.help.JavaConfigImportTestAll;
public class SpringTest05 {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
JavaConfigImportTestAll.class);
JavaConfigImport01 import01= (JavaConfigImport01) context.getBean("import01");
customer.printMsg("Hello 11");
JavaConfigImport02 import02= (JavaConfigImport02) context.getBean("import02");
scheduler.printMsg("Hello 22");
}
}
输出结果:
JavaConfigImport01 Hello 11
javaConfigImport02 Hello 22
网友评论