Spring中为了减少XML配置,可以声明一个配置类类对bean进行配置,主要用到两个注解@Configuration和@bean

例子:

  • 首先,XML中进行少量的配置来启动java配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<context:component-scan base-package="com.demo.config"/>
</beans>
  • 定义一个配置类,用@Configuration注解该类,等价于XML里的<beans>,用@Bean注解方法,等价于XML配置的<bean>,方法名等于beanId。代码如下:
@Configuration
public class SpringConfig {
@Bean
public Service service(){
return new Service();
}
@Bean
public Client client(){
return new Client();
}
}
  • 其他Bean代码:
public class Service {
public String sayHello(){
return "HelloWord!";
}
}
public class Client {
@Autowired
Service service;
public void invokeService(){
System.out.println("client invoke :" + service.sayHello());
}
}
  • 测试类
public class Test {

    public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
Client client = context.getBean("client",Client.class);
client.invokeService();
}
}

加载XML中配置的beans和bean用:

ApplicationContext ctx = new ClassPathXmlApplicationContext("config/bean.xml");// 读取bean.xml中的内容
Counter c = ctx.getBean("client", Client.class);// 创建bean的引用对象

  • 运行结果

  • 写在最后

SpringBean的创建和注入有三种,XML、注解、java配置文件。

因为XML配置较为繁琐,现在大部分开始用注解和java配置,一般什么时候用注解或者java配置呢?

基本原则是:全局配置用java配置(如数据库配置,MVC,redis等相关配置),业务Bean的配置用注解(@Service @Component@Repository@Controlle)。

Spring中基于java的配置的更多相关文章

  1. 【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件

    Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  2. Spring中基于Java的配置@Configuration和@Bean用法

    spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  3. Spring中基于Java的配置@Configuration和@Bean用法 (转)

    spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...

  4. Spring IOC之基于JAVA的配置

    基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...

  5. Spring Security基于Java配置

    Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...

  6. Spring入门学习笔记(2)——基于Java的配置

    目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...

  7. Spring基于Java的配置

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...

  8. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  9. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

随机推荐

  1. visual studio git 忽略文件配置模板

    ## Ignore Visual Studio temporary files, build results, and## files generated by popular Visual Stud ...

  2. AIX中文件系统管理

    1.文件系统类型 AIX主要支持的文件系统有: JFS(Journaled  File  Systems)   日志型文件系统     JFS2(Enhanced  Journaled  File S ...

  3. elk搭建的详细步骤以及说明

    一:准备工作 1.准备一台虚拟机 192.168.175.222      elk-node2 2.关闭防火墙以及selinux 命令:systemctl stop firewalld       # ...

  4. .NET CORE学习笔记系列 开篇介绍

    ASP.NET Core学习和使用了一段时间了,好记性不如烂笔头,通过查阅官网学习文档和一些大神们的博客总结一下.主要路线先总结一下ASP.NET Core的基础知识,然后是ASP.NET Core  ...

  5. 从输入一个URL到页面渲染的流程简介

    首先说明以下是我参考网上答案和自己的思考,给出自己的想法,如果有问题,欢迎大家吐槽从用户在浏览器中输入一个URL,到整个页面渲染,这个过程中究竟发生了什么呢?今天先简单写下整个过程,后面再一点点完善. ...

  6. spring 定时器时间配置

    Spring--quartz中cronExpression The '*' character is used to specify all values. For example, "*& ...

  7. 安装RabbitMQ 后配置遇到问题解决方案

    http://blog.csdn.net/j_shine/article/details/78833456

  8. CodeForces-721B-Passwords

    链接: https://vjudge.net/problem/CodeForces-721B 题意: Vanya is managed to enter his favourite site Code ...

  9. ESP8266-Station模式--我想连上谁

    Station模式又叫做站点工作模式,类似于无线终端 处于Station模式下的ESP8266,可以连接到AP.通过Station(简称为“STA”)模式,ESP8266作为客户端连接到路由的wifi ...

  10. java 流操作对文件的分割和合并的实例详解_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 java 流操作对文件的分割和合并的实例详解 学习文件的输入输出流,自己做一个小的示例,对文件进行分割和合并. 下面是代 ...