您的位置:首页 > 编程语言 > Java开发

Spring学习-helloworld使用annotation配置

2010-08-28 16:32 68 查看

1、使用annotation注解需要asm,cglib两个jar包,分别从http://forge.ow2.org/project/showfiles.php?group_id=23&release_id=3334和http://cglib.sourceforge.net/下载并加入build path中

2、新建Spring3HelloWorld类,代码如下:

package spring3annotation;

public class Spring3HelloWorld {

public void sayHello(){
System.out.println("Hello world~!!");
}

}

3、新建Spring3HelloWorldConfig,代码如下:

package spring3annotation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Spring3HelloWorldConfig {

public @Bean Spring3HelloWorld spring3HelloWorld(){
return new Spring3HelloWorld();
}

}

 4、新建Spring3HelloWorldConfigTest,代码如下:

package spring3annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Spring3HelloWorldConfigTest {

/**
* @param args
*/
public static void main(String[] args) {
// Initialize IoC Container
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
Spring3HelloWorldConfig.class);
System.out.println("Calling Bean method: sayHello()");
// Retrieve the bean from Container
Spring3HelloWorld myBean = (Spring3HelloWorld) context
.getBean("spring3HelloWorld");
myBean.sayHello();
}

}

 5、运行Spring3HelloWorldConfigTest

 6、英文教程http://www.roseindia.net/spring/spring3/configuration-spring.shtml

 7、asm是轻量级的字节码处理框架,日后再仔细研究?

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: