Spring三种配置注入方式

来源:互联网 发布:人工智能不会取代人类 编辑:程序博客网 时间:2024/06/18 04:57

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;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn 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 {/** * @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 {@AutowiredCar car;public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn  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 {/** * @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 {@Beanpublic Car getCar() {return new Car();}@Beanpublic 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 {@AutowiredCar car;@Overridepublic String toString() {// TODO Auto-generated method stubreturn 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());}}
原创粉丝点击