spring 配置bean的方法及依赖注入发方式
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的方法及依赖注入发方式的更多相关文章
- Spring配置bean的方法(工厂方法和Factorybean)
通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声 ...
- Spring学习(十八)Bean 的三种依赖注入方式介绍
依赖注入:让调用类对某一接口实现类的依赖关系由第三方注入,以移除调用类对某一接口实现类的依赖.接下来将详细的向大家介绍Spring容器支持的三种依赖注入的方式以及具体配置方法:• 属性注入方法• ...
- Spring(八):Spring配置Bean(一)BeanFactory&ApplicationContext概述、依赖注入的方式、注入属性值细节
在Spring的IOC容器里配置Bean 配置Bean形式:基于xml文件方式.基于注解的方式 在xml文件中通过bean节点配置bean: <?xml version="1.0&qu ...
- Spring4学习回顾之路03—XML配置Bean ,依赖注入的方式
配置Bean的形式可以基于XML文件的方式,也可以基于注解的方式,而Bean的配置方式可以通过全类名(反射),通过工厂方式和FactoryBean. XML形式 <?xml version=&q ...
- Spring依赖注入的方式、类型、Bean的作用域、自动注入、在Spring配置文件中引入属性文件
1.Spring依赖注入的方式 通过set方法完成依赖注入 通过构造方法完成依赖注入 2.依赖注入的类型 基本数据类型和字符串 使用value属性 如果是指向另一个对象的引入 使用ref属性 User ...
- [转载]Spring下IOC容器和DI(依赖注入) @Bean及@Autowired
Spring下IOC容器和DI(依赖注入) @Bean及@Autowired自动装配 bean是什么 bean在spring中可以理解为一个对象.理解这个对象需要换一种角度,即可将spring看做一门 ...
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现(转)
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- Spring 依赖注入的方式
Spring 支持3中依赖注入的方式 1.属性注入 通过setter 方法注入Bean的属性或依赖的对象. <bean id = " " class = " &q ...
- 轻松了解Spring中的控制反转和依赖注入(二)
紧接上一篇文章<轻松了解Spring中的控制反转和依赖注入>讲解了SpringIOC和DI的基本概念,这篇文章我们模拟一下SpringIOC的工作机制,使我们更加深刻的理解其中的工作. 类 ...
随机推荐
- 简明 MongoDB 入门教程
MongoDB 是免费开源的跨平台 NoSQL 数据库,命名源于英文单词 humongous,意思是「巨大无比」,可见开发组对 MongoDB 的定位.与关系型数据库不同,MongoDB 的数据以类似 ...
- REQUEST_TIMEOUT 解决方案
you need to pass an npmrc file when you install the business network onto the peers. For more info s ...
- 深入JVM系列(二)之GC机制、收集器与GC调优
一.回想JVM内存分配 须要了解很多其它内存模式与内存分配的,请看 深入JVM系列(一)之内存模型与内存分配 1.1.内存分配: 1.对象优先在EDEN分配 2.大对象直接进入老年代 3.长期存活的 ...
- Home Assistant 安装Samba和SSH server 插件
安装 Samba 插件 Samba 是 SMB/CIFS 网络协议的重新实现, 它作为 NFS 的补充使得在 Linux 和 Windows 系统中进行文件共享.打印机共享更容易实现. Smaba 服 ...
- Count and Say leetcode java
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...
- 屌丝就爱尝鲜头——java8再判断
这节,我们来通过具体的实例来看看Java8的具体用法. 首当其冲,就是lambda用法. 这里的案例,就是用lambda来实现runnable接口,我们知道以前用匿名内部类的方式来实现runnable ...
- 【google面试题】求1到n的正数中1出现的次数的两种思路及其复杂度分析
问题描写叙述: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.比如输入12,从1到12这些整数中包括1 的数字有1.10.11和12.1一共出现了5次. 这是一道广为流传的googl ...
- Kernel Memory Layout on ARM Linux
这是内核自带的文档,讲解ARM芯片的内存是如何布局的!比较简单,对于初学者可以看一下!但要想深入理解Linux内存管理,建议还是找几本好书看看,如深入理解Linux虚拟内存,嵌入系统分析,Linux内 ...
- SVN迁移部署
迁移svn并修改目录结构的方法 http://blog.csdn.net/jianxin1009/article/details/8220461 测试成功的命令: svnadmin –parent-d ...
- [Canvas]RPG游戏雏形 (地图加载,英雄出现并移动)
源码请点此下载并用浏览器打开index.html观看 图例: 代码: <!DOCTYPE html> <html lang="utf-8"> <met ...