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]

时间: 2024-08-05 06:23:00

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

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

通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声明通过静态方法创建的bean,需要在bean的class属性里指定拥有该工厂的方法的类,通知在factory-method属性里指定工厂方法的名称, 最后,使用<constructor-arg>元素为该方法传递方法参数 通过调用实例工厂方法创建bean 实例工厂方法:将对象的创建过程封装到另外一个

Spring配置bean的方法(工厂方法和Factorybean)【转】

通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声明通过静态方法创建的bean,需要在bean的class属性里指定拥有该工厂的方法的类,通知在factory-method属性里指定工厂方法的名称, 最后,使用<constructor-arg>元素为该方法传递方法参数 通过调用实例工厂方法创建bean 实例工厂方法:将对象的创建过程封装到另外一个

Spring配置bean的方法

通过工厂方法配置bean 通过调用静态工厂方法创建bean 通过静态工厂方法创建bean是将对象创建的过程封装到静态方法中.当客户端需要对象时,只需要简单地调用静态方法,而不关心创建对象的细节. 要声明通过静态方法创建的bean,需要在bean的class属性里指定拥有该工厂的方法的类,通知在factory-method属性里指定工厂方法的名称, 最后,使用<constructor-arg>元素为该方法传递方法参数 通过调用实例工厂方法创建bean 实例工厂方法:将对象的创建过程封装到另外一个

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

使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository 注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean.具体只需将该注解标注在 DAO 类上即可.同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用 Bean

spring框架学习(二)依赖注入

转自:http://blog.csdn.net/lishuangzhe7047/article/details/20740835 ———————————————————————————————————————————— spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.接口注入不作要求,下面介绍前两种方式. 1,set注入 采用属性的set方法进行初始化,就成为set注入. 1)给普通字符类型赋值. [java] view plaincopyprint? pub

Spring配置bean

基本 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/sch

Spring之ioc控制反转(依赖注入)

个人感觉依赖注入比控制反转更好理解,所以下面就只说依赖注入: spring的整体结构示意图: 一.spring 中的概念 beanFactory容器 1.容器是spring框架的核心,容器使用ioc依赖注入来管理所有组成应用系统的组件. 2.spring中的两种容器: beanFactory  这个容器提供了基础的依赖注入支持,而且是延迟加载的,而 applicationcontext是对beanFactory 这个容器的扩展, 3.beanFactory :beanFactory就是一个Bea

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

紧接上一篇文章<轻松了解Spring中的控制反转和依赖注入>讲解了SpringIOC和DI的基本概念,这篇文章我们模拟一下SpringIOC的工作机制,使我们更加深刻的理解其中的工作. 类之间的结构图如下 以下是代码 BeanFactor接口:在Spring源码中的定义是:持有对一定数量的Bean的定义,同时每个Bean都被唯一标识的对象(类),需要实现这个接口.根据对Bean的定义,该工厂将会返回一个包含Bean定义的对象的独立实例(原型设计模式),或者单例共享(一个不错的单例设计模式,)范

详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载

使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的开发.@Repository 注解便属于最先引入的一批,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean.具体只需将该注解标注在 DAO 类上即可.同时,为了让 Spring 能够扫描类路径中的类并识别出 @Repository 注解,需要在 XML 配置文件中启用 Bean