2. Spring4.1-Java Config
来源:互联网 发布:病假条淘宝暗语大全 编辑:程序博客网 时间:2024/05/21 06:18
2.1 java config
- spring的java config主要使用@Configuration和@Bean两个注解;
- 使用@Configuration注解在类上声明是一个配置类(相当于一个spring的配置xml);
- 使用@Bean注解在方法上,返回值是一个类的实例,并声明这个返回值是spring的一个bean,bean的name是方法名;
2.2 关于@Bean和@Component,@Service,@Repository,@Controller
@Component,@Service,@Repository,@Controller
注解在一个类上之后,这个类也成为spring容器中的bean,使用@Bean注解也是,感觉使用@Bean注解是不是更麻烦呢?既然效果是等同的,那什么时候使用@Bean什么时候使用
@Component,@Service,@Repository,@Controller
系列呢?这个原则就和我们当初混用xml配置和
@Component,@Service,@Repository,@Controller
时候一样:系统的全局配置(数据库配置,spring mvc配置,spring security配置等)使用java config(xml),业务相关的bean使用@Component,@Service,@Repository,@Controller
系列。在后面我们讲到一些全局配置的时候我们就会使用Spring的java config
2.3 演示
2.3.1 创建一个properties(test.properties)文件作为配置
wisely.word = World
2.3.2 创建一个java class
package com.wisely.javaconfig;public class DemoService { private String word; public String getWord() { return word; } public void setWord(String word) { this.word = word; } public String sayHello(){ return "Hello "+this.word; }}
2.3.3 创建java config配置类
package com.wisely.javaconfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;@Configuration //声明是一个配置类@PropertySource("com/wisely/javaconfig/test.properties")public class DemoConfig { @Bean //声明是一个bean public DemoService demoBean(Environment environment){ DemoService demoService = new DemoService(); demoService.setWord(environment.getProperty("wisely.word")); return demoService; }}
2.3.4 测试-初始化spring容器
package com.wisely.javaconfig;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Main { public static void main(String[] args) { //设定此包下的类被注册成spring的bean, //包含@Configuration,@Component,@Service,@Repository,@Controller AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wisely.javaconfig"); DemoService demoService = context.getBean(DemoService.class); System.out.println(demoService.sayHello()); context.close(); }}
输出结果:Hello World
0 0
- 2. Spring4.1-Java Config
- Spring4.1.5+Mybatis3.2.8基于java config整合实现(Maven)
- 基于 spring4 java config(配置)的servlet3 文件上传于下载
- ssh环境搭建(2)-config篇(spring4.2.6+struts2.5+hibernate5.1 )
- Java.Web学习笔记 Spring4
- SPRING4学习日志1
- spring4整合hibernate5.1
- Spring4.1 cache 应用
- Spring4-1 Spring初始
- spring4整合hibernate5.1
- spring4
- spring4
- spring4
- 2. Spring4.0环境搭建
- linux config java path
- java ini config parser
- fedora alternatives --config java
- java environment config
- 关于web工程中java类如何获取webapp路径下的文件
- Android常见的两种启动服务的方式
- android的actionBar函数
- ubuntu14.04,由于安装软件造成磁盘空间不足,无法登入Gnome解决办法!!
- unity3d 5.2 添加广点通广告
- 2. Spring4.1-Java Config
- 学习PMP第一步
- Linux sublime text3输入中文的问题.
- Codeforces Round #331 (Div. 2) C. Wilbur and Points
- 免费赠与c/c++海量视频 学习资料的
- 利用xShell实现windows与虚拟机的文件交换
- 用java实现计算String类型的四则运算——用栈和后缀表达式实现
- APP安全测试-数据安全性/通讯安全性/人机接口安全性
- 南大软院大神养成计划-第三天