博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2. Spring4.1-Java Config
阅读量:4118 次
发布时间:2019-05-25

本文共 2228 字,大约阅读时间需要 7 分钟。

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

转载地址:http://jjcpi.baihongyu.com/

你可能感兴趣的文章
Remove Element--原地移除重复元素
查看>>
Remove Duplicates from Sorted Array--从有序数组中移除重复元素
查看>>
Count and Say
查看>>
Gas Station
查看>>
Palindrome Partitioning --回文切割 深搜(重重)
查看>>
Valid Palindrome 简单的回文判断
查看>>
Pascal's Triangle -- 生成杨辉三角
查看>>
Pascal's Triangle II 生成杨辉三角中的某行
查看>>
Minimum Depth of Binary Tree -- 二叉树的最小深度 DFS 加剪枝
查看>>
Climbing Stairs 爬楼梯方法 动态规划
查看>>
Merge Two Sorted Lists 合并两个有序链表
查看>>
pow(x,n) 为什么错这么多次
查看>>
Jump Game 动态规划
查看>>
Binary Tree Maximum Path Sum 自底向上求解(重重重重)
查看>>
Subsets 深搜
查看>>
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>