Bean 的配置方式:通过全类名(反射)、通过工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean

这里依据全类名配置bean

<bean id="helloWord" class="com.spring.HelloWord">

 <property name="userName" value="springsss"></property>

</bean>

依赖注入发方式:

属性注入:

applicationContext.xml配置文件为:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:p="http://www.springframework.org/schema/p"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 配置bean -->

<bean id="helloWord" class="com.spring.HelloWord">

 <property name="userName" value="springsss"></property>

</bean>

</beans>

package com.spring;

public class HelloWord {

private String userName;

public void setUserName(String userName) {

this.userName = userName;

}

public void hello() {

System.out.println("hello:" + userName);

}

public HelloWord(){

System.out.println("construct!!!!!!!!!!");

}

}

package com.spring;





import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;





public class Test {

public static void main(String[] args) {

/**

* 创建对象及为对象赋值交给spring完毕

*/

// HelloWord helloWord = new HelloWord();

// helloWord.setUserName("hello");

// helloWord.hello();



//1.创建spring IOC容器

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");





//2从容器中获得Bean

HelloWord helloWord = (HelloWord) ctx.getBean("helloWord");





//3.调用方法

helloWord.hello();



}

}

输出结果为:

construct!!!!!!!!!!

hello:springsss

构造器注入:

applicationContext.xml文件为

<?xml version="1.0" encoding="UTF-8"?

>

<beans xmlns="http://www.springframework.org/schema/beans"

   xmlns:p="http://www.springframework.org/schema/p"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

  http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 配置bean -->

<bean id="car" class="com.spring.Car">

<constructor-arg value="green"></constructor-arg>

<constructor-arg value="22"></constructor-arg>

</bean>

</beans>

public class Car {

 private String name;

 private String color;

 private int num;

public Car(String color, int num) {

super();

this.color = color;

this.num = num;

}

@Override

public String toString() {

return "Car [name=" + name + ", color=" + color + ", num=" + num + "]";

}





}

package com.spring;





import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;





public class Test {

public static void main(String[] args) {





//1.创建spring IOC容器

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");





Car car = (Car) ctx.getBean("car");

System.out.println(car.toString());



}

}

输出结果为:

Car [name=null, color=green, num=22]

spring 配置bean的方法及依赖注入发方式的更多相关文章

  1. Spring配置bean的方法(工厂方法和Factorybean)

    通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声 ...

  2. Spring学习(十八)Bean 的三种依赖注入方式介绍

    依赖注入:让调用类对某一接口实现类的依赖关系由第三方注入,以移除调用类对某一接口实现类的依赖.接下来将详细的向大家介绍Spring容器支持的三种依赖注入的方式以及具体配置方法:•    属性注入方法• ...

  3. Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节

    在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...

  4. Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式

    配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean. XML形式 <?xml version=&q ...

  5. Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件

    1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...

  6. [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired

    Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...

  7. 详解 Spring 3.0 基于 Annotation 的依赖注入实现(转)

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  8. Spring 依赖注入的方式

    Spring 支持3中依赖注入的方式 1.属性注入  通过setter 方法注入Bean的属性或依赖的对象. <bean id = " " class = " &q ...

  9. 轻松了解Spring中的控制反转和依赖注入(二)

    紧接上一篇文章<轻松了解Spring中的控制反转和依赖注入>讲解了SpringIOC和DI的基本概念,这篇文章我们模拟一下SpringIOC的工作机制,使我们更加深刻的理解其中的工作. 类 ...

随机推荐

  1. 低版本系统兼容的ActionBar(四)添加Tab+添加自定义的Tab视图+Fragment

    在ActionBar中添加Tab是很有用的技巧.在support V7库的支持下,我们几乎可以用和之前一样的方式来添加Tab,对于Tab来说,我们可以和MenuItem一样,给他定义自己的视图.我这里 ...

  2. Caffe中deploy.prototxt 和 train_val.prototxt 区别

    之前用deploy.prototxt 还原train_val.prototxt过程中,遇到了坑,所以打算总结一下 本人以熟悉的LeNet网络结构为例子 不同点主要在一前一后,相同点都在中间 train ...

  3. js转义和反转义html htmlencode htmldecode

    文章目录 JS实现HTML标签转义及反转义 用Javascript进行HTML转义 1.HTML转义 2.反转义 3.一个有意思的认识 4.完整版本的代码 其他 [转义字符]HTML 字符实体< ...

  4. Xilinx FFT IP v9.0 使用

    该ip用于实现N=2**m(m=3~16)点FFT的变换, 实现的数学类型包含: A)      定点全精度 B)      定点缩减位宽 C)      块浮点 每一级蝶型运算后舍入或者取整.对于N ...

  5. linux 比较两个文件夹不同 (diff命令, md5列表)

    比较文件夹diff,可以直接使用diff命令 [root@~]# diff -urNa dir1 dir2 -a Treat all files as text and compare them li ...

  6. CSS-页面滑屏滚动原理

    现在的网站有的时候为了简洁就是很多的单页滑屏滚动介绍,主要呈现方式有两种,一种是整体的元素一直排列下去,假设有五个需要展示的全屏页面,那么高度是500%,只是展示100%,剩下的可以通过transfo ...

  7. 算法 数组中出现次数最多的数字 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. Angular CLI 使用教程指南参考

    Angular CLI 使用教程指南参考 Angular CLI 现在虽然可以正常使用但仍然处于测试阶段. Angular CLI 依赖 Node 4 和 NPM 3 或更高版本. 安装 要安装Ang ...

  9. centos7 开机自动连网

    新安装好的CentOS7桌面版,默认的网络都是关闭的,在图形页面中,开启后,重启后网络又关闭了.下面配置开机自动连网. 工具/原料   CentOS7.2 方法/步骤     首先打开终端,用ifco ...

  10. CentOS6 安装并破解Jira 7

    CentOS6 安装并破解Jira 7 JIRA软件是为您的软件团队的每个成员构建的,用来规划,跟踪和发布优秀的软件. https://confluence.atlassian.... 最低硬件要求及 ...