博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring三种配置注入方式
阅读量:2441 次
发布时间:2019-05-10

本文共 4335 字,大约阅读时间需要 14 分钟。

Spring三种配置注入方式

1.基于XML注入

Car类

public class Car {		double price;		String brand;		public double getPrice() {			return price;		}		public void setPrice(double price) {			this.price = price;		}		public String getBrand() {			return brand;		}		public void setBrand(String brand) {			this.brand = brand;		}		}

定义了价格和品牌

MyCar类

public class MyCar {		Car car;		public Car getCar() {			return car;		}		public void setCar(Car car) {			this.car = car;		}		@Override		public String toString() {			// TODO Auto-generated method stub			return car.price+"   "+car.brand;		}				}

采用属性注入,所以对于注入的类或者变量需要提供setter方法

XML配置文件:

启动类:

import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import org.springframework.core.io.support.ResourcePatternResolver;public class Test {	/**	 * @param args	 */	public static void main(String[] args) {		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();		Resource resource = resolver.getResource("classpath:com/testa/beans.xml");		BeanFactory beanFactory = new XmlBeanFactory(resource);		MyCar myCar = beanFactory.getBean("mycar", MyCar.class);		System.out.println(myCar.toString());	}}

2.注解配置

首先Car类

import org.springframework.stereotype.Component;@Component("car")public class Car {		double price;		String brand;		public Car() {			this.brand = "bmw";			this.price=55;		}		public double getPrice() {			return price;		}		public void setPrice(double price) {			this.price = price;		}		public String getBrand() {			return brand;		}		public void setBrand(String brand) {			this.brand = brand;		}		}

@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

myCar类

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Component("mycar")public class MyCar {		@Autowired		Car car;					public Car getCar() {			return car;		}		public void setCar(Car car) {			this.car = car;		}		@Override		public String toString() {			// TODO Auto-generated method stub			return  car.brand+"  "+car.price;		}		}

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作,此处可以将Car实例注入进MyCar

此时还需要配置扫描包

启动类

import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {	/**	 * @param args	 */	public static void main(String[] args) {		 AbstractApplicationContext context = new ClassPathXmlApplicationContext("com/testb/beans.xml");		 MyCar myCar = (MyCar) context.getBean("mycar");		 System.out.println(myCar.toString());		 	}}

3.基于Java类进行配置

Car类:

public class Car {			double price;	String brand;	public Car() {		this.price=333;		this.brand="bmw";	}	public double getPrice() {		return price;	}	public void setPrice(double price) {		this.price = price;	}	public String getBrand() {		return brand;	}	public void setBrand(String brand) {		this.brand = brand;	}	}

配置类:

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class Config {	@Bean	public Car getCar() {		return new Car();	}	@Bean	public MyCar getMyCar(){		return new MyCar();	}	}

指定配置信息的类上加上 @Configuration 注解,以明确指出该类是 Bean 配置的信息源。并且 Spring 对标注

Configuration 的类有如下要求:

配置类不能是 final 的;配置类不能是本地化的,亦即不能将配置类定义在其他类的方法内部;配置类必须有一个无参构造函数。AnnotationConfigApplicationContext 将配置类中标注了 @Bean 的方法的返回值识别为 Spring Bean,并注册到容器中,受 IoC 容器管理。

MyCar类:

import org.springframework.beans.factory.annotation.Autowired;public class MyCar {	@Autowired	Car car;	@Override	public String toString() {		// TODO Auto-generated method stub		return car.price+"  "+car.brand;	}				}

将Car进行注入

启动类:

import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {	/**	 * @param args	 */	public static void main(String[] args) {		ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);		MyCar myCar = context.getBean(MyCar.class);		System.out.println(myCar.toString());	}}

转载地址:http://xvcqb.baihongyu.com/

你可能感兴趣的文章
JSR227:J2EE数据绑定及数据访问标准 (转)
查看>>
Jbuilder8开发J2ee学习笔记(2) (转)
查看>>
Makefile编写小说(一) (转)
查看>>
ORACLE SQL性能优化系列 (二) (转)
查看>>
控件treeview的使用 (转)
查看>>
运用VC或Java对Office进行编程操作 (转)
查看>>
Linux Shell 裡一些很少用到卻很有用的指令 (转)
查看>>
第10章 模型管理视图 (转)
查看>>
第7章 活 动 视 图 (转)
查看>>
“管家婆”软件用于维修管理 (转)
查看>>
第13章 术 语 大 全 (8) (转)
查看>>
第13章 术 语 大 全 (9) (转)
查看>>
人月神话读书笔记(二) (转)
查看>>
附录 UML元模 (转)
查看>>
A Brief Look at C++ 中文版 (转)
查看>>
JBuilder Editor中光标不能正确定位问题的解决 (转)
查看>>
XML加ASP实现网页“本地化” (转)
查看>>
Java中的异步网络编程 (转)
查看>>
用于核心模式驱动程序的网络体系结构(1) (转)
查看>>
More Effective C++ 条款20 (转)
查看>>