按 ‘ Spring ’ 分类归档

Spring中方法注入之方法替代

最近开始看《Spring in action》,看到方法注入部分,说可以实现类似Ruby给一个类动态添加方法的功能。即method replacement. 书中的例子用的是魔术师大变老虎的技巧生动描述Spring的这一功能。
首先定义一个Performer接口(Spring中提倡使用接口来隐藏实现,从而实现应用组件的弱耦合)。

public interface Performer {
    public void perform() throws PerformanceException;
}

猛击阅读全文

Spring配置错误

最近在配置Spring的时候经常出现各种错误,从网上摘取做个总结:

问题1:Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.springframework.util.ClassUtils.<clinit>(ClassUtils.java:67) at org.springframework.core.io.DefaultResourceLoader.<init>(DefaultResourceLoader.java:52) at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:184) at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:80) at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:58) at
需要加上:commons-logging.jar log4j-1.2.11.jar

问题2:Exception in thread “main” org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [text.xml]; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
Caused by: java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldException
at java.lang.Class.forName0(Native Method)

需要加上:aspectjweaver.jar 猛击阅读全文

Spring 中设置依赖注入

代码示例:

package org.sixsun.spring;

public class HelloBean {
    private String name;
    private String helloWord;

    public HelloBean() {
    }

    public HelloBean(String name, String helloWord) {
        this.name = name;
        this.helloWord = helloWord;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }

    public void setHelloWord(String helloWord) {
        this.helloWord = helloWord;
    }
    public String getHelloWord() {
        return helloWord;
    }
}

猛击阅读全文

Spring中ClassPathResource路径

Spring例子中有一行代码:

 BeanFactory factory =
        new XmlBeanFactory(new ClassPathResource("./info/leyond/test/hello.xml"));

这里的ClassPathResource(String path):

Parameters:
path the absolute path within the class path

我已经把hello.xml和该java文件放在同个目录,而把path写成“hello.xml”,结果显示无法找到该xml文件,后来干脆写成绝对路径“c:/xxxxx/hell.xml”还是同样的效果。

根据英文的within the class path解释是不是指的是src所在的目录,所以后来把xml路径如上面代码书写方式,却可以访问了。

Intellij IDEA10开发Spring

昨天利用Eclipse配置Spring开发环境,有点费力。但是混乱博客说他正在利用Intellij IEDA来开发Spring。我也想尝试下,早就听说这个IDE,智能化,其宗旨就是享受编程。

一、软件准备

1. 下载Intellij IDEA 10,下载地址:http://www.jetbrains.com/idea/,下载Ultimate Edition版本,Free版本不知道是否支持Spring开发。

2. 下载Spring Framework,下载地址:http://www.springsource.org/download (最新为3.0.5)

二、软件安装

1. 直接双击安装Intellij IDEA,安装之后启动,初次使用需要设置JDK,设定目录为你的Java Jdk安装路径即可。

2. 准备好Spring类库,这将下载的spring-framework-2.5.5-with-dependencies.zip解压。将其中的spring.jar(dist 目录中)、commons-logging.jar(lib\jakarta-commons目录)、log4j-1.2.15.jar(lib \log4j目录)这三个文件复制到的”D:\java\Spring\lib” 目录中,然后在Eclipse中建立一个“Springlib”库,将那三个文件添加进“Springlib”库中。准备下一步使用。

3. 如果是付费版本,需要破解,破解软件下载地址:IntelliJ IDEA 10.0 注册机,或者自行搜索。

猛击阅读全文

Eclipse搭建Spring开发环境

这篇文章简单介绍下如何利用Eclipse搭建Spring开发环境。

一、软件准备

1. Eclipse, 下载地址:http://www.eclipse.org,可下载3.6版本

2. SpringIde, 有两种安装方法,官网:http://www.springsource.org/node/489

3. Spring Framework: 下载地址:http://www.springsource.org/download (这里使用的是2.5.5,最新为3.0.5)

二、软件安装

1. 安装Eclipse,直接解压到某个目录即可,比如我的:E:\SpringDev\eclipse。(注意:使用Eclipse需要安装Java)

2.安装SpringIDE,这里介绍两种方法,一种在线更新:Help->intall new software,更新地址为:http://springide.org/updatesite

猛击阅读全文