大半天都在看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. java web 程序---留言板

    思路:一个form表单,用户提交留言 一个页面显示留言内容.用到Vector来存取信息并显示 cas.jsp <body> <form action="fei.jsp&qu ...

  2. python + docker, 实现天气数据 从FTP获取以及持久化(二)-- python操作MySQL数据库

    前言 在这一节中,我们主要介绍如何使用python操作MySQL数据库. 准备 MySQL数据库使用的是上一节中的docker容器 “test-mysql”. Python 操作 MySQL 我们使用 ...

  3. LIS系列总结

    此篇博客总结常见的LIS模型变形的解法. ------------------------------------------------------------------- 〇.LIS的$O(Nl ...

  4. C#中的参数关键字params

    class 参数 { public void doSome(string str,params int[] values){ ) { ; i < values.Length; i++) { Co ...

  5. django rest_framework 框架的使用03

    rest_framework的 数据解析器 首先需要知道前端发给后端的数据格式头有哪些: media_type = 'application/json' media_type = 'applicati ...

  6. 33_java之类加载器和反射

    01类加载器 * A.类的加载 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. * a 加载 * 就是指将class文件读入内存,并为之 ...

  7. Selenium Webdriver——Table类封装

    WebTable.java import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebEl ...

  8. Java——jxl读取Excel文件

    1.创建文件流,打开EXCEL文件(jxi不支持.xlsx文件,支持.xls) FileInputStream excelFile = new FileInputStream(excelPath); ...

  9. 7.25 7figting!

    TEXT 82 Proton 马来西亚宝腾汽车 A fork in the road 何去何从?(陈继龙编译) Nov 30th 2006 | HONG KONG From The Economist ...

  10. 解决no-session延迟加载问题

    解决no-session延迟加载问题 说明:dao层使用Hibernate从数据库查询数据信息返回给web层,在web层打印信息报no-Session错误, 产生原因 关联对象默认都是采用延迟加载 事 ...