spring 配置bean的方法及依赖注入发方式
来源:互联网 发布:淘宝9.9元天天特价包邮 编辑:程序博客网 时间:2024/05/22 13:08
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学习总结(三):IOC & DI 配置 Bean 之配置形式及依赖注入方式
- Spring-----7、bean实例的创建方式及依赖配置
- Spring-2:bean的两种依赖注入方式
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- Spring的配置、生命周期、依赖注入bean的操作
- Spring依赖注入的过程,ref注入与内部bean方式注入
- Spring的bean注入方式
- spring bean注入的方式
- Bean的两种依赖注入方式
- Spring依赖注入的方式
- spring-依赖注入的方式
- spring依赖注入的方式
- spring依赖注入的方式
- Spring的依赖注入方式
- spring的依赖注入方式
- Spring的依赖注入方式
- Spring装配bean,依赖注入及注入参数
- MongoDB(1)--简介以及安装
- cocos2d-x中CCSprite更换图片
- UDP通信(Red Hat Enterprise Linux 6)
- 想写一个网络游戏(PS:纯粹是为了好玩)
- UVA - 11613 Acme Corporation(最小费用流)
- spring 配置bean的方法及依赖注入发方式
- Opencv + Qt 获取视频中任意一帧
- Android 自定义主菜单
- android ble 4.0实现自动配对
- Suffix Array 后缀数组
- POJ3181--Dollar Dayz
- [Java Concurrency in Practice]第六章 任务执行
- phpstorm通过ftp远程开发服务器上的项目
- IOS 多线程(2) --NSThread