Spring中基于java的配置
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的配置的更多相关文章
- 【Spring】28、Spring中基于Java的配置@Configuration和@Bean用法.代替xml配置文件
Spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring中基于Java的配置@Configuration和@Bean用法
spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring中基于Java的配置@Configuration和@Bean用法 (转)
spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: <?xml version ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring Security基于Java配置
Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...
- Spring入门学习笔记(2)——基于Java的配置
目录 基于Java的配置 @Configuration & @Bean Annotations Example 注入Bean依赖 @Import注解 Lifecycle Callbacks(声 ...
- Spring基于Java的配置
以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
随机推荐
- springmvc中的数据传递
import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; impo ...
- 剑指offer-2:斐波那契数列
二.斐波那契数列 题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 1.递归法 1). 分析 斐波那契数列的标准公式为 ...
- linux ssh连接超时断连设置
以下均针对redhat6.5系统进行说明. 一.设置ssh超时断连 使用root用户,编辑/etc/profile文件,在 HOSTNAME='/bin/hostname' HISTIZE=30 后增 ...
- install - 复制文件并设置属性
SYNOPSIS[总览] install [options] [-s] [--strip] source dest install [options] [-s] [--strip] source... ...
- info - 阅读 info 文档
SYNOPSIS 总览 info [OPTION]... [MENU-ITEM...] DESCRIPTION 描述 阅读 info 格式的文档. OPTIONS 选项 --apropos=STRIN ...
- javase中的super、this和protected关键字
关键字 this 记住一句话:this是指向对象本身的一个指针. 注意: this只能用于方法体内,且只能在非静态方法中,也就是说只有实例调用的方法中才能出现this,我们可以认为当new了一个实例后 ...
- Spring Framework Part3 IoC and Dynamic Proxy
spring serious of blog edit by 马士兵教育 Maven方式创建Spring工程 工程创建 1.新建项目 选择Maven Project 2.勾选 Create a sim ...
- 彻底解决 TypeScript 报错:“无法重新声明块范围变量”的问题
背景 当使用 TypeScript + TSlint + Babel + Jest 搭建开发环境时,在开发过程中偶尔会被 IDE 提示「无法重新声明块范围变量」,从而导致编译出错,报错图示如下: 相关 ...
- 前后台入门系统搭建详解(springboot+angularjs)
1 . 搭建boot启动框架,这一步骤什么都不用添加,搭建完后框架如下: 2.因为是前后台项目,所以一般是需要有前台页面的,需要在后端maven依赖中加入web依赖包 spring-boot-star ...
- jumpserver部署1.0版本
A. jumpserver概述 跳板机概述: 跳板机就是一台服务器,开发或运维人员在维护过程中首先要统一登录到这台服务器,然后再登录到目标设备进行维护和操作: 跳板机缺点:没有实现对运维人员操作行为的 ...