Spring 3 JavaConfig example
2015-08-21 14:04
489 查看
Since Spring 3,
But, you are still allow to use the classic XML way to define beans and configuration, the JavaConfig is just another alternative solution.
See the different between classic XML definition and JavaConfig to define a bean in Spring container.
Spring XML file :
Equivalent configuration in JavaConfig :
Output
JavaConfigfeatures are included in core Spring module, it allow developer to move bean definition and Spring configuration out of XML file into Java class.
But, you are still allow to use the classic XML way to define beans and configuration, the JavaConfig is just another alternative solution.
See the different between classic XML definition and JavaConfig to define a bean in Spring container.
Spring XML file :
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="helloBean" class="com.mkyong.hello.impl.HelloWorldImpl"> </beans>
Equivalent configuration in JavaConfig :
package com.mkyong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.mkyong.hello.HelloWorld; import com.mkyong.hello.impl.HelloWorldImpl; @Configuration public class AppConfig { @Bean(name="helloBean") public HelloWorld helloWorld() { return new HelloWorldImpl(); } }
Spring JavaConfig Hello World
Now, see a full Spring JavaConfig example.1. Directory Structure
See directory structure of this example.2. Dependency Library
To use JavaConfig (@Configuration), you need to include CGLIB library. See dependencies :
<!-- Spring 3 dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- JavaConfig need this library --> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency>
3. Spring Bean
A simple bean.package com.mkyong.hello; public interface HelloWorld { void printHelloWorld(String msg); }
package com.mkyong.hello.impl; import com.mkyong.hello.HelloWorld; public class HelloWorldImpl implements HelloWorld { @Override public void printHelloWorld(String msg) { System.out.println("Hello : " + msg); } }
4. JavaConfig Annotation
Annotate with@Configurationto tell Spring that this is the core Spring configuration file, and define bean via
@Bean.
package com.mkyong.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.mkyong.hello.HelloWorld; import com.mkyong.hello.impl.HelloWorldImpl; @Configuration public class AppConfig { @Bean(name="helloBean") public HelloWorld helloWorld() { return new HelloWorldImpl(); } }
5. Run it
Load your JavaConfig class withAnnotationConfigApplicationContext.
package com.mkyong.core; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.mkyong.config.AppConfig; import com.mkyong.hello.HelloWorld; public class App { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); HelloWorld obj = (HelloWorld) context.getBean("helloBean"); obj.printHelloWorld("Spring3 Java Config"); } }
Output
Hello : Spring3 Java Config
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- MyEclipse Web Project转Eclipse Dynamic Web Project
- Windows Clang开发环境备忘
- Ubuntu 安装 JDK 问题
- 让Flex Builder 3.0与Eclipse3.4整合起来
- Eclipse配置Javascript开发环境图文教程
- 使用zend studio for eclipse不能激活代码提示功能的解决办法
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- 图解Eclipse j2ee开发环境的搭建过程
- struts2 spring整合fieldError问题
- ext for eclipse插件安装方法
- 解决Fedora14下eclipse进行android开发,ibus提示没有输入窗口的方法详解
- eclipse导入jquery包后报错的解决方法
- Eclipse 格式化代码时不换行与自动换行的实现方法
- Eclipse下jQuery文件报错出现错误提示红叉
- eclipse如何忽略js文件报错(附图)
- eclipse3.2.2 + MyEclipse5.5 + Tomcat5.5.27 配置数据库连接池