1.基于xml的配置
2.基于java类的配置
@Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。
@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。
1。bean类
package dc.xx;
public class Helloword {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
2。配置类
package dc.xx;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;
@Configuration
@Import(Helloword.class) //注入一个 Bean
public class HelloworldConfig {
@Bean
@Scope("prototype&