大半天都在看spring,以前总是看不下去,这次慢慢来,慢慢看。

看那些基础的,倒是还不错。好多都是关于helloworld的,写完helloworld,觉得不怎么形象。于是写了动物,作为接口。

(1)动物接口方法:move(),say()。

(2)cat和dog ,实现接口。

package Animal;

/**
* Created with IntelliJ IDEA.
* User: wang
* Date: 13-10-22
* Time: 下午5:12
* To change this template use File | Settings | File Templates.
*/
public class Cat implements Animal {
@Override
public void move() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("4 legs");
} @Override
public void say() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("miao miao");
}
}

(3)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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="Animal.Cat" ></bean>
<bean id="dog" class="Animal.Dog"></bean>
<bean id="bird" class="Animal.Bird">
<constructor-arg index="0" value="gegegege"></constructor-arg>
<constructor-arg index="1" value="gogogo"></constructor-arg>
</bean>
</beans>

xml里面,有个constructor-arg ,这个是bird里面,构造方法参数。

(4) Bird.java

package Animal;

/**
* 带有参数的spring bean
* Created with IntelliJ IDEA.
* User: wang
* Date: 13-10-22
* Time: 下午6:09
* To change this template use File | Settings | File Templates.
*/
public class Bird implements Animal { private String message;
private String movess;
public Bird(String message,String movess){
this.message=message;
this.movess=movess;
} @Override
public void say() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println(message);
} @Override
public void move() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println(movess);
}
}

构造方法里面,有两个参数,xml对应的有设置。

(5) 然后测试一下,ATest.java

package Animal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created with IntelliJ IDEA.
* User: wang
* Date: 13-10-22
* Time: 下午5:19
* To change this template use File | Settings | File Templates.
*/
public class ATest {
public static void main(String args[]){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml");
Animal animal=applicationContext.getBean("cat",Cat.class);
animal.say();
animal.move();
Animal animal1=applicationContext.getBean("dog",Dog.class);
animal1.say();
animal1.move();
Animal animal2=applicationContext.getBean("bird",Bird.class);
animal2.say();
animal2.move();
}
}

这突出了面向接口编程了。

总之还不错,看了一些,挺不错的。

IDEA配置spring的更多相关文章

  1. 如何用Java类配置Spring MVC(不通过web.xml和XML方式)

    DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...

  2. 事务管理(下) 配置spring事务管理的几种方式(声明式事务)

    配置spring事务管理的几种方式(声明式事务) 概要: Spring对编程式事务的支持与EJB有很大的区别.不像EJB和Java事务API(Java Transaction API, JTA)耦合在 ...

  3. Spring 4 官方文档学习(十一)Web MVC 框架之配置Spring MVC

    内容列表: 启用MVC Java config 或 MVC XML namespace 修改已提供的配置 类型转换和格式化 校验 拦截器 内容协商 View Controllers View Reso ...

  4. 更加优雅地配置Spring Securiy(使用Java配置和注解)

    Spring Security 借助一系列Servlet Filter 来提供安全性功能,但是借助Spring的小技巧,我们只需要配置一个Filer就可以了,DelegatingFilterProxy ...

  5. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  6. JAVAEE——spring02:使用注解配置spring、sts插件、junit整合测试和aop演示

    一.使用注解配置spring 1.步骤 1.1 导包4+2+spring-aop 1.2 为主配置文件引入新的命名空间(约束) 1.3 开启使用注解代替配置文件 1.4 在类中使用注解完成配置 2.将 ...

  7. eclipse中配置spring环境

    初识Spring框架 1.简单使用 eclipse中配置Spring环境,如果是初学的话,只需要在eclipse中引入几个jar包就可以用了, 在普通java project项目目录下,建一个lib文 ...

  8. 配置spring的监听器 让spring随项目的启动而启动

    <!-- 配置spring的监听器 让spring随项目的启动而启动 --> <listener> <listener-class>org.springframew ...

  9. 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建三:配置spring并测试

    这一部分的主要目的是 配置spring-service.xml  也就是配置spring  并测试service层 是否配置成功 用IntelliJ IDEA 开发Spring+SpringMVC+M ...

  10. 注解配置spring

    1.为什么使用注解配置Spring基于注解配置的方式也已经逐渐代替xml.这个是不可逆的潮流,所以我们必须要掌握使用注解的方式配置Spring 总结:(1)使用注解配置Spring,注解的作用就是用于 ...

随机推荐

  1. ThinkJava-File类

    1.1目录列表器: package com.java.io; import java.io.File; import java.io.FilenameFilter; import java.util. ...

  2. 一、jdk工具之jps(JVM Process Status Tools)命令使用

    目录 一.jdk工具之jps(JVM Process Status Tools)命令使用 二.jdk命令之javah命令(C Header and Stub File Generator) 三.jdk ...

  3. python3调用阿里云语音服务

    步骤 1 创建阿里云账号,包括语音服务里的企业实名 为了访问语音服务,您需要有一个阿里云账号.如果没有,可首先按照如下步骤创建阿里云账号: 访问阿里云 官方网站,单击页面上的 免费注册 按钮. 按照屏 ...

  4. java之压缩流(ZipOutputStream)

    一.文件压缩,是很有必要的,我们在进行文件,传输过程中,很多时候都是,都是单个文件单个文件发送接收,但是当数据量特别大,或者文件数量比较多的时候,这个时候就可以考虑文件压缩. 二.优势:文件压缩过后, ...

  5. httpclient跳过https请求的验证

    一.因为在使用https发送请求的时候会涉及,验证方式.但是这种方式在使用的时候很不方便.特别是在请求外部接口的时候,所以这我写了一个跳过验证的方式.(供参考) 二.加入包,这里用的是commons- ...

  6. 20165233 2017-2018-2 《Java程序设计》第七周学习总结

    20165233 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 ch11 下载XAMPP 基本操作:连接数据库 基础:查询操作,更新.添加与删除操作 重点: ...

  7. @JsonIgnore的源码说明

    @JsonIgnore不仅仅是在getter上有影响,也对setter方法有影响: 所在包:com.fasterxml.jackson.annotation; 源码: import java.lang ...

  8. MySQL GTID (二)

    MySQL GTID 系列之二 三.在线将GTID转化为传统模式 环境见上篇系列文章 关闭GTID,不用停止服务,不影响线上业务 3.1 关闭GTID复制,调整为传统复制 #SLVAE实例上停止复制 ...

  9. query简洁弹出层代码

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  10. 7.25 9figting!

    TEXT 87 Fund management基金管理   A Miller's tale 米勒传奇(陈继龙编译) Dec 7th 2006 From The Economist print edit ...