大半天都在看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. 10-28质量监控ELK

    监控业务范围 app崩溃监控(Bugly) 应用性能监控(APM) 业务监控(TalkingData.友盟) 质量监控(缺位) 质量监控平台ELK elk官网 数据构造 线上错误状态分布 故障影响范围 ...

  2. 一.jQuery源码解析之总体架构

    (function (window, undefined) { //构建jQuery对象 var document = window.document, navigator = window.navi ...

  3. setAttribute第三个参数

    object.setAttribute(sName, vValue [, iFlags]) sName参数应是Dom属性而非html中的属性.Dom中Html专有的接口属性应该以小写字母开头,如果属性 ...

  4. easyui制作进度条案例demo

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. mysql 获取一段时间的数据

    用 sql 获取一段时间内的数据: SELECT * FROM EDI.edi_history WHERE timestampdiff(day, SYSDATE(), create_time_loc) ...

  6. 用dwz时, 由于粗心产生的一些问题(记录方便自己查阅)

    在打开"添加" 或 "修改" , 用dialog弹出时 , 点击提交的时候, dialog 不能关闭, 也不能刷新 解决办法: 注意form标签, onsubm ...

  7. ul li的添加 删除 复制 clone()

    语法总结 li=document.createElement('li') li.innerText = tag $('#i1').append(li) $(..).clone()  复制整个文本和Ht ...

  8. vlc相关命令行设置

    1:改变VLC模块参数   http://tianxiaoma.blog.51cto.com/1501174/309519 ====================================== ...

  9. sql server2008本地连接选择windows身份验证无法登陆的解决办法

    1.安装完sqlserver数据库,本地连接登录不了 解决办法:进入cmd,输入net start mssqlserver 服务启动后,再次用windows身份验证就可以登陆本地数据库了

  10. LevelDB 读取记录

    [LevelDB 读取记录] LevelDb是针对大规模Key/Value数据的单机存储库,从应用的角度来看,LevelDb就是一个存储工具.而作为称职的存储工具,常见的调用接口无非是新增KV,删除K ...