Spring(八)之基于Java配置
基于 Java 的配置
到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean。如果你熟悉使用 XML 配置,那么我会说,不需要再学习如何进行基于 Java 的配置是,因为你要达到相同的结果,可以使用其他可用的配置。
基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大多数的 Spring,但是一些有帮助的基于 Java 的注解,解释如下:
@Configuration 和 @Bean 注解
带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。
演示示例:
(1).编写HelloWorldConfig.java
package com.tutorialspoint;
import org.springframework.context.annotation.*;
@Configuration
public class HelloWorldConfig {
@Bean
public HelloWorld helloWorld(){
return new HelloWorld();
}
}
上面代码相当于
<beans>
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld" />
</beans>
(2)编写HelloWorld.java
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
(3)编写MainApp.java
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MainApp {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(HelloWorldConfig.class); HelloWorld helloWorld = ctx.getBean(HelloWorld.class); helloWorld.setMessage("Hello World!");
helloWorld.getMessage();
}
}
运行结果如下:

Spring(八)之基于Java配置的更多相关文章
- spring实战六之使用基于java配置的Spring
之前接触的都是基于XML配置的Spring,Spring3.0开始可以几乎不使用XML而使用纯粹的java代码来配置Spring应用.使用基于java配置的Spring的步骤如下: 1. 创建基于ja ...
- Spring入门(8)-基于Java配置而不是XML
Spring入门(8)-基于Java配置而不是XML 本文介绍如何应用Java配置而不是通过XML配置Spring. 0. 目录 声明一个简单Bean 声明一个复杂Bean 1. 声明一个简单Bean ...
- Spring IOC之基于JAVA的配置
基础内容:@Bean 和 @Configuration 在Spring中新的支持java配置的核心组件是 @Configuration注解的类和@Bean注解的方法. @Bean注解被用于表明一个方法 ...
- Spring完全基于Java配置和集成Junit单元测试
要点: 配置继承WebApplicationInitializer的类作为启动类,相当于配置web.xml文件 使用@Configuration注解一个类,在类中的方式使用@Bean注解,则表名该方法 ...
- springmvc基于java配置的实现
该案例的github地址:https://github.com/zhouyanger/demo/tree/master/springmvc-noxml-demo 1.介绍 之前搭建SpringMvc项 ...
- Spring Security基于Java配置
Maven依赖 <dependencies> <!-- ... other dependency elements ... --> <dependency> < ...
- Spring MVC 5 + Thymeleaf 基于Java配置和注解配置
Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...
- 更加优雅地配置Spring Securiy(使用Java配置和注解)
Spring Security 借助一系列Servlet Filter 来提供安全性功能,但是借助Spring的小技巧,我们只需要配置一个Filer就可以了,DelegatingFilterProxy ...
- Spring(七)之基于注解配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
随机推荐
- Java基础-基于《Thinking In Java》
摘要 本文是对一些java基础知识的整理,把之前印象笔记里面的全部慢慢搬到这个blog来 为了方便就按照<Thinking In Java>的目录来编辑. 这里面的内容均为面试题相关,可能 ...
- 【SSH网上商城项目实战09】添加和更新商品类别功能的实现
转自:https://blog.csdn.net/eson_15/article/details/51347734 上一节我们做完了查询和删除商品的功能,这一节我们做一下添加和更新商品的功能. 1. ...
- html5 转义实体字符 元数据 跳转 全局属性 id class lang style
实体 Html 实体就是把特殊字符通过代码显示出来, 比如, <>在浏览器会识别为标签,不能正常显示, 这是你就需要安如<去表达左尖括号. 元数据 2. 声明字符编码 3.模 ...
- 使用input做简单的上传图片
css 代码: .container{ width: 200px; height: 200px; border: 1px solid #666; } HTML 代码: <input type=& ...
- GetModuleFileName
原文:http://www.cnblogs.com/dongzhiquan/archive/2009/07/28/1994776.html GetModuleFileName HMODULE hMod ...
- Android 录制视频
Activity代码: package eoe.demo.Media; import java.io.File; import java.io.IOException; import android. ...
- 浅谈count(*)、count(1)、count(列名)
count(*) 和 count(1)和count(列名)区别 执行效果上: count(*)包括了所有的列,相当于行数,在统计结果的时候,不会忽略列值为NULL count(1)包括了所有列, ...
- Azure 虚拟机上的 SQL Server 常见问题
本主题提供有关运行 Azure 虚拟机中的 SQL Server 时出现的一些最常见问题的解答. 如果本文未解决你的 Azure 问题,请访问 MSDN 和 CSDN 上的 Azure 论坛. 你可以 ...
- Linux Kernel 4.11首个候选版本开放下载
Linus Torvalds宣布了即将到来的Linux Kernel 4.11内核分支的首个候选(RC)版本,用户可下载.编译并在自己的GNU/Linux发行版本中进行测试.Linus Torvald ...
- ASC日志保存时间更改
连接到数据库,选择 OperationsManagerAC,修改dtConfig表即可,新建查询: select * from dtConfig Update dtConfig set value=2 ...