[email protected] 和 @Bean

参考:http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html

@Configuration 和 @Bean 注解

带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。最简单可行的 @Configuration 类如下所示:

 1 package org.wzh.di.demo1.congiration;
 2
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5
 6 @Configuration
 7 public class HelloWorldConfig {
 8     @Bean
 9     public HelloWorld helloWorld() {
10         return new HelloWorld();
11     }
12 }
 1 package org.wzh.di.demo1.congiration;
 2
 3 public class HelloWorld {
 4
 5     private String message;
 6
 7     public String getMessage() {
 8         return message;
 9     }
10
11     public void setMessage(String message) {
12         this.message = message;
13     }
14
15 }
 1 package org.wzh.di.demo1.congiration;
 2
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 5
 6 public class Test {
 7
 8     public static void main(String[] args) {
 9         ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class);
10         HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
11         helloWorld.setMessage("Hello World!");
12         String msg = helloWorld.getMessage();
13         System.out.println(msg);
14     }
15
16 }

也可以加载其他配置类,这里只写用法

 1 package org.wzh.di.demo1.congiration;
 2
 3 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 4
 5 public class Test2 {
 6
 7     public static void main(String[] args) {
 8         AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 9         ctx.register(HelloWorldConfig.class);
10         ctx.refresh();
11         HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
12         helloWorld.setMessage("Hello World 2!");
13         String msg = helloWorld.getMessage();
14         System.out.println(msg);
15     }
16
17 }
时间: 2024-08-01 10:45:19

[email protected] 和 @Bean的相关文章

[email protected]和@Bean详解

[email protected]和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Configuration { @AliasFor( annotation = Component.class ) String value() default ""; } 可以看到在@Conf

Spring4 In [email protected]单例、多例Bean

Spring In [email protected]单例.多例Bean 代码下载地址:http://download.csdn.net/download/poiuy1991719/9967494 Singleton:单例    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)@Scope("singleton") Prototype:每次注入都会创建新的   @Scope(ConfigurableBeanFactory.SCOPE_PRO

25、自动装配[email protected]根据环境注册bean

25.自动装配[email protected]根据环境注册bean 指定组件在哪个环境的情况下才能被注册到容器中 加了环境标识的,只有这个环境被激活才能注册到组件中 默认是default环境 写在类上,整个配置类的激活的时候才能生效 没有标注环境标识的bean,在任何环境下都是加载的 package org.springframework.context.annotation; import java.lang.annotation.Documented; import java.lang.a

[email protected]注解与自动装配

1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss 拥有 Office 和 Car 类型的两个属性:       清单 3. Boss.java [java] view plaincopy package com.baobaotao; public class Boss { private Car car; private Office office

[email protected]的心得

今天练习了一下@ResponseBody,主要是对json字符返回有了新的了解. 1.配置@ResponseBody: 在Spring-Servlet.xml中开启注解配置: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.

Spring高级话题[email&#160;protected]***注解的工作原理

出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy注解 激活Aspect自动代理 <aop:aspectj-autoproxy/> 开启对AspectJ自动代理的支持. 在用到AOP的自动代理的时候用,如果你理解了Java的动态代理,很容易的就会熟悉AOP的自动代理的. @EnableAsync @EnableAsync注解开启异步方法的支持. 这个相信大家都比较熟悉的.对于异步

[email&#160;protected] 深入学习之——初探spring mvc

一.简介 Spring MVC是Spring框架的最重要的模块之一,它构建于Spring IoC容器之上,大量使用容器的特性简化其配置.MVC模式消除了业务逻辑与UI的耦合.模式负责封装视图展示的应用数据:视图只显示数据,不包含任何业务逻辑:控制器负责接收用户请求并调用后端服务进行业务处理,处理之后,后端服务可能返回某些数据供视图显示.其核心思想是分离业务逻辑与UI,使系统能够独立修改,互不影响. Spring MVC应用,模式通常由服务层处理和持续层存储的领域对象组成.视图通常是用Java标准

Struts2报错异常Method &quot;setUser&quot; failed for object [email&#160;protected]

在写类型转换的时候发现报错 异常信息: 1 ognl.MethodFailedException: Method "setUser" failed for object [email protected] [java.lang.NoSuchMethodException: com.mikey.action.ConverterAction.setUser([Ljava.lang.String;)] 2 at ognl.OgnlRuntime.callAppropriateMethod(O

19、属性赋值[email&#160;protected]加载外部配置文件

19.属性赋值[email protected]加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明,然后使用${nickName}取值 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"