基于Spring 爪哇的配置示例
基于Spring 爪哇的配置步骤:
以下项目的项目结构:
1.创建一个 简单的Java Maven项目。
2. Maven依赖
将spring和cglib maven依赖项放在pom.xml中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<依存关系> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-核心</artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-webmvc</artifactId> <版>${弹簧.版}</版> </依赖> <!- cglib 需要 对于 @组态 注解 -> <依赖> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <版>3.0</版> </依赖> </依存关系> <属性> <弹簧.版>4.2.1。发布</弹簧.版> </属性> |
因此,您的pom.xml将如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<项目 XML文件ns="http://maven.apache.org/POM/4.0.0" XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" si:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"< <模型Version<4.0.0</模型Version> <groupId>组织.Arpit.爪哇2blog</groupId> <artifactId>弹簧HelloWorldJavaBasedConfiguration</artifactId> <版>0.0.1-快照</版> <名称>弹簧HelloWorldJavaBasedConfiguration</名称> <描述>它 提供 您好 世界 程序 对于 爪哇 基于 配置 </描述> <依存关系> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-核心</artifactId> <版>${弹簧.版}</版> </依赖> <依赖> <groupId>组织.弹簧框架</groupId> <artifactId>弹簧-webmvc</artifactId> <版>${弹簧.版}</版> </依赖> <!- cglib 需要 对于 @组态 注解 -> <依赖> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <版>3.0</版> </依赖> </依存关系> <属性> <弹簧.版>4.2.1。发布</弹簧.版> </属性> </项目> |
3.创建Bean类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
包 组织.Arpit.爪哇2blog.模型; 上市 类 国家 { 串 国家的名字; 上市 国家(串 国家的名字) { 这个.国家的名字=国家的名字; } 上市 串 getCountryName() { 返回 国家的名字; } 上市 虚空 setCountryName(串 国家的名字) { 这个.国家的名字 = 国家的名字; } } |
4.创建应用程序配置类
此类将具有@Configuaration和@Bean批注。
在包中创建名为应用配置.java的类 组织.arpit.java2blog.config
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
包 组织.Arpit.爪哇2blog.配置; 进口 组织.Arpit.爪哇2blog.模型.国家; 进口 组织.弹簧框架.语境.注解.豆; 进口 组织.弹簧框架.语境.注解.组态; @组态 上市 类 应用配置 { @豆(名称=“ countryObj”) 上市 国家 getCountry() { 返回 新 国家(“印度”); } } |
上面的文件等效于下面的spring配置xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?XML文件 版="1.0" 编码方式=“ UTF-8”?< <豆子 XML文件ns="http://www.springframework.org/schema/beans" XML文件ns:si="http://www.w3.org/2001/XMLSchema-instance" XML文件ns:语境="http://www.springframework.org/schema/context" si:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"< <语境:注解-配置/< <豆 ID =“ countryObj” 类=“ 组织.arpit.java2blog.Country” < <属性 名称=“国家的名字” 值=“印度”/< </豆< </豆子< |
5.创建主类以运行程序
创建名为SpringJavaConfigMain.java的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
包 组织.Arpit.爪哇2blog.主要; 进口 组织.弹簧框架.语境.ApplicationContext; 进口 组织.弹簧框架.语境.注解.AnnotationConfigApplicationContext; 进口 组织.Arpit.爪哇2blog.配置.应用配置; 进口 组织.Arpit.爪哇2blog.模型.国家;; 上市 类 弹簧JavaConfigMain { 上市 静态的 虚空 主要(串[] args) { @禁止警告(“资源”) ApplicationContext appContext = 新 AnnotationConfigApplicationContext(应用配置.类); 国家 countryObj = (国家) appContext.getBean(“ countryObj”); 串 国家的名字=countryObj.getCountryName(); 系统.出.打印(“国家的名字: ”+ 国家的名字); } } |
6.运行以上程序
当您在程序上方运行时,将得到以下输出。
1 2 3 4 5 |
二月 19, 2016 4:09:08 下午 组织.弹簧框架.语境.注解.AnnotationConfigApplicationContext 准备刷新 信息: 提神醒脑 组织.弹簧框架.语境.注解.AnnotationConfigApplicationContext@2096ed8b: 启动 日期 [周五 二月 19 16:09:08 IST 2016]; 根 的 语境 等级制 国家 名称: 印度 |
7.源代码
答对了!!我们已经完成了基于Spring 爪哇的配置。