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() {
return car.price+" "+car.brand;
}
}
采用属性注入,所以对于注入的类或者变量需要提供setter方法
XML配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<bean id="car" class="com.testa.Car">
<property name="price" value="22"></property>
<property name="brand" value="bmw"></property>
</bean>
<bean id="mycar" class="com.testa.MyCar">
<property name="car">
<ref bean="car"/>
</property>
</bean>
</beans>
启动类:
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 {
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() {
return car.brand+" "+car.price;
}
}
@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作,此处可以将Car实例注入进MyCar
此时还需要配置扫描包
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
">
<context:component-scan base-package="com.testb"></context:component-scan>
</beans>
启动类
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
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;
@Configuration
public 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() {
return car.price+" "+car.brand;
}
}
将Car进行注入
启动类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
MyCar myCar = context.getBean(MyCar.class);
System.out.println(myCar.toString());
}
}