Spring学习-helloworld使用annotation配置

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是轻量级的字节码处理框架,日后再仔细研究?

你可能感兴趣的:(spring,bean,PHP,.net,IOC)