Spring入门(8)-基于Java配置而不是XML
Spring入门(8)-基于Java配置而不是XML
本文介绍如何应用Java配置而不是通过XML配置Spring。
0. 目录
- 声明一个简单Bean
- 声明一个复杂Bean
1. 声明一个简单Bean
还是需要简单的XML配置的,如下:
<?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/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<context:component-scan base-package="com.chzhao.springtest"/>
</beans>
声明一个配置类
package com.chzhao.springtest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public App app1() {
return new App();
}
}
通过@Configuration声明配置类,通过@Bean声明Bean。其中app1就是Bean的ID。
可以通过这个app1值机获得Bean
package com.chzhao.springtest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext act = new ClassPathXmlApplicationContext(
"applicationContext.xml");
App a = (App) act.getBean("app1");
a.showMsg();
}
}
2. 声明一个复杂Bean
如果一个Bean的定义基于另外一个Bean,也很容易实现。
首先看app定义
package com.chzhao.springtest;
public class App {
public App(IPersonBll personBll) {
this.personBll = personBll;
}
private IPersonBll personBll;
public void showMsg() {
this.personBll.show();
}
}
package com.chzhao.springtest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyConfig {
@Bean
public IPersonBll bll1() {
return new PersonBll();
}
@Bean
public App app1() {
return new App(bll1());
}
}
Spring入门(8)-基于Java配置而不是XML的更多相关文章
- Spring(八)之基于Java配置
基于 Java 的配置 到目前为止,你已经看到如何使用 XML 配置文件来配置 Spring bean.如果你熟悉使用 XML 配置,那么我会说,不需要再学习如何进行基于 Java 的配置是,因为你要 ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- spring实战六之使用基于java配置的Spring
之前接触的都是基于XML配置的Spring,Spring3.0开始可以几乎不使用XML而使用纯粹的java代码来配置Spring应用.使用基于java配置的Spring的步骤如下: 1. 创建基于ja ...
- 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 ...
随机推荐
- C#使用sharppcap实现网络抓包
sharppcap dll的下载地址: http://sourceforge.net/directory/os:windows/?q=sharppcap 具体使用详细步骤: http://www.co ...
- bash shell 合并文件
# 按列合并文件 paste file1 file2 file3 > file4 # 要先 sort, 再 file1 file2 paste格式为: paste -d -s -file1 fi ...
- HDU 4746 (莫比乌斯反演) Mophues
这道题看巨巨的题解看了好久,好久.. 本文转自hdu4746(莫比乌斯反演) 题意:给出n, m, p,求有多少对a, b满足gcd(a, b)的素因子个数<=p,(其中1<=a<= ...
- poj 1986 Distance Queries(LCA:倍增/离线)
计算树上的路径长度.input要去查poj 1984. 任意建一棵树,利用树形结构,将问题转化为u,v,lca(u,v)三个点到根的距离.输出d[u]+d[v]-2*d[lca(u,v)]. 倍增求解 ...
- codeforces 333A - Secrets
题意:保证不能正好配齐n,要求输出可以用的最大硬币数. 注意如果用到某种硬币,那么这种硬币就有无穷多个.所以11=3+3+3+3,12=9+9,13=3+3+3+3+3 #include<cst ...
- 编译pure-ftpd时提示错误Your MySQL client libraries aren't properly installed
如果出现类似configure: error: Your MySQL client libraries aren’t properly installed 的错误,请将mysql目录下的 includ ...
- Myeclipse中相同变量高亮显示
不小心搞不显示了,解决: windows/MyEclipse-> preferences-> java-> Editor-> Mark Occurences 勾选即可
- Spring AOP (上)
工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大说过:“开发人员不可能一天到晚只有工作,肯定是需要自我学习.第一:为了更充实自己,保持进步状态.第二:为了提升技术,提高开发能力.第三:保 ...
- java正则表达式Pattern和Matcher
java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. 1.简介: java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包. ...
- mybatis中的变量#与$
ibatis中使用select top #num# * from tableName出现错误.由于初次用ibatis还不知道在它里边拼写SQL语句的一些规则,导致一些自认为很平常的SQL语句,在它这里 ...