spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
- 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.2.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <context:component-scan base-package="SpringStudy.Model">
- </context:component-scan>
- </beans>
二、定义一个配置类
用@Configuration注解该类,等价 与XML中配置beans;用@Bean标注方法等价于XML中配置bean。
代码如下:
- package SpringStudy;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import SpringStudy.Model.Counter;
- import SpringStudy.Model.Piano;
- @Configuration
- public class SpringConfig {
- @Bean
- public Piano piano(){
- return new Piano();
- }
- @Bean(name = "counter")
- public Counter counter(){
- return new Counter(12,"Shake it Off",piano());
- }
- }
三、基础类代码
Counter:
- package SpringStudy.Model;
- public class Counter {
- public Counter() {
- }
- public Counter(double multiplier, String song,Instrument instrument) {
- this.multiplier = multiplier;
- this.song = song;
- this.instrument=instrument;
- }
- private double multiplier;
- private String song;
- @Resource
- private Instrument instrument;
- public double getMultiplier() {
- return multiplier;
- }
- public void setMultiplier(double multiplier) {
- this.multiplier = multiplier;
- }
- public String getSong() {
- return song;
- }
- public void setSong(String song) {
- this.song = song;
- }
- public Instrument getInstrument() {
- return instrument;
- }
- public void setInstrument(Instrument instrument) {
- this.instrument = instrument;
- }
- }
Piano类
- package SpringStudy.Model;
- public class Piano {
- private String name="Piano";
- private String sound;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getSound() {
- return sound;
- }
- public void setSound(String sound) {
- this.sound = sound;
- }
- }
四、调用测试类
- package webMyBatis;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.annotation.AnnotationConfigApplicationContext;
- import SpringStudy.Model.Counter;
- public class SpringTest {
- public static void main(String[] args) {
- //ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
- ApplicationContext annotationContext = new AnnotationConfigApplicationContext("SpringStudy");
- Counter c = annotationContext.getBean("counter", Counter.class);// 创建bean的引用对象
- System.out.println(c.getMultiplier());
- System.out.println(c.isEquals());
- System.out.println(c.getSong());
- System.out.println(c.getInstrument().getName());
- }
- }
注意:如果是在xml中配置beans和bean的话,或者使用自动扫描调用的话,代码为
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/bean.xml");// 读取bean.xml中的内容
Counter c = ctx.getBean("counter", Counter.class);// 创建bean的引用对象
五、运行结果
12.0
false
Shake it Off
Piano
公告
本文转自左正博客园博客,原文链接:http://www.cnblogs.com/soundcode/p/6477938.html如需转载请自行联系原作者
【推荐】腾讯云新用户域名抢购1元起,抓紧抢购
· 摩拜发布共享单车与城市可持续发展报告:上海起最早、抗霾最积极
· 保温杯+枸杞:究竟是中年人的孤单,还是年轻人的狂欢?
· 滴滴与美团将在哪些领域火力全开?
· 盖茨J.P.摩根健康大会演讲:生命科学研究可挽救数百万人的生命
· 微博云剪2.0版本上线 2 分钟制作一个短视频
» 更多新闻...
· 以操作系统的角度述说线程与进程
· 软件测试转型之路
· 门内门外看招聘
· 大道至简,职场上做人做事做管理
2015-02-28 存储过程中执行动态Sql语句