Spring 基于Java的Bean声明

使用@Configuration进行设置;

Xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="com.stono.sprtest"></context:component-scan>
</beans>

JavaConfig:

package com.stono.sprtest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; @Configuration
public class SingerConfig {
@Bean
@Scope("prototype")
// 如果加了这个就会导致有两个Cymbal对象;
public InstrumentI cymbal() {
return new Cymbal();
}
@Bean
public Singer3 singer3() {
// 参数里面的cymbal()不会新建Bean,还是之前那个单例实体;
return new Singer3(cymbal(), "newstr");
}
}

AppBean:

package com.stono.sprtest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppBeans8 {
@SuppressWarnings("resource")
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("appbeans8.xml");
Singer3 singer3 = (Singer3) context.getBean("singer3");
System.out.println(singer3);
Object bean = context.getBean("cymbal");
System.out.println(bean);
}
}

POJO:

package com.stono.sprtest;

import org.springframework.beans.factory.annotation.Autowired;

public class Singer3 {
@Autowired
private InstrumentI instrument;
private String name;
public Singer3(InstrumentI instrument, String name) {
this.instrument = instrument;
this.name = name;
}
@Override
public String toString() {
return "Singer2 [instrument=" + instrument + ", name=" + name + "]";
}
}
package com.stono.sprtest;

public class Cymbal implements InstrumentI {
}

Spring 基于Java的Bean声明的更多相关文章

  1. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean

    Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...

  2. Spring基于Java的配置

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html: 基于Java的配置选项,可以使你在不用 ...

  3. Spring 基于 Java 的配置

    前面已经学习如何使用 XML 配置文件来配置 Spring bean. 基于 Java 的配置可以达到基于XML配置的相同效果. 基于 Java 的配置选项,可以使你在不用配置 XML 的情况下编写大 ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于Annotation装配Bean

    在 Spring 中,尽管使用 XML 配置文件可以实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难. Java 从 J ...

  5. Spring基于Java的JSR-250注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-jsr250-annot ...

  6. Spring学习笔记--使用Spring基于Java的配置

    我们需要使用@Component注解来定义一个配置类,在配置类中我们定义Bean: package com.moonlit.myspring; import org.springframework.c ...

  7. Spring基于xml注入bean的几种方式?

    (1)Set方法注入: (2)构造器注入:①通过index设置参数的位置:②通过type设置参数类型: (3)静态工厂注入: (4)实例工厂:

  8. spring实战六之使用基于java配置的Spring

    之前接触的都是基于XML配置的Spring,Spring3.0开始可以几乎不使用XML而使用纯粹的java代码来配置Spring应用.使用基于java配置的Spring的步骤如下: 1. 创建基于ja ...

  9. [译]17-spring基于java代码的配置元数据

    spring还支持基于java代码的配置元数据.不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下. spring基于java代码的配置元数据,可以通过@Configuration注解 ...

随机推荐

  1. mysql优化---优化工具MySQL performance tuning primer script

    MySQL performance tuning primer script一个简单好用的mysql优化工具,其实一个shell脚本 下载: $ wget http://www.day32.com/M ...

  2. springMVC获取数据--注意post方法会出现中文乱码问题

    1. 新建web project 2. 加入jar 3. 改写web.xml <?xml version="1.0" encoding="UTF-8"?& ...

  3. google-c-style

    http://zhanxw.com/blog/2011/03/learning-and-applying-coding-style-from-google-in-emacs/ http://stack ...

  4. ural1613 For Fans of Statistics

    For Fans of Statistics Time limit: 1.0 secondMemory limit: 64 MB Have you ever thought about how man ...

  5. for和getElementByTagName配合

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. jar2exe 配置jre

    http://www.regexlab.com/zh/jar2exe/support.htm

  7. 一、MongoDB安装及启动

    1 安装 在官网http://www.mongodb.org/downloads下载对应版本,并安装.安装在D:\MongoDB下. 2 设置 1) 新建文件夹,用于存放db数据 D:\MongoDB ...

  8. 6种炫酷的CSS3按钮边框动画特效

    6种炫酷的CSS3按钮边框动画特效Button border animate 用鼠标滑过下面的按钮看看效果! Draw Draw Meet Center Spin Spin Circle Spin T ...

  9. mysql、mysqli、pdo使用

    一.php手册关于MySQL的说明:    This extension is not recommended for writing new code. Instead, either themys ...

  10. Kconfig基本语法

    Linux 内核在2.6版本以后将配置文件由原来的config.in改为Kconfig.当执行make menuconfig时会出现内核的配置界面,所有配置工具都是通过读取arch/$(ARCH)Kc ...