温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

spring如何使用JavaConfig进行配置

发布时间:2021-09-13 13:00:56 来源:亿速云 阅读:176 作者:小新 栏目:开发技术

这篇文章主要介绍了spring如何使用JavaConfig进行配置,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

JavaConfig,是在 Spring 3.0 开始从一个独立的项目并入到 Spring 中的。JavaConfig 可以看成一个用于完成 Bean 装配的 Spring 配置文件,即 Spring 容器,只不过该容器不是 XML文件,而是由程序员使用 Java 自己编写的 Java 类。

实体类:

package com.lzl.spring.entity;   public class Car {	private String brand;//品牌	private String type;//型号	private double speed;//最大时速	public Car() {	}	public Car(String brand, String type, double speed) {	this.brand = brand;	this.type = type;	this.speed = speed;	}	public String getBrand() {	return brand;	}	public void setBrand(String brand) {	this.brand = brand;	}	public String getType() {	return type;	}	public void setType(String type) {	this.type = type;	}	public double getSpeed() {	return speed;	}	public void setSpeed(double speed) {	this.speed = speed;	}	@Override	public String toString() {	return "Car [brand=" + brand + ", type=" + type + ", speed=" + speed + "]";	} }
package com.lzl.spring.entity;   public class Person {	private Integer id;	private String name;	private Car car;	public Person(Integer id, String name) {	this.id = id;	this.name = name;	}	public Person() {	}	public Person(Integer id, String name, Car car) {	this.id = id;	this.name = name;	this.car = car;	}	public Integer getId() {	return id;	}	public void setId(Integer id) {	this.id = id;	}	public String getName() {	return name;	}	public void setName(String name) {	this.name = name;	}	public Car getCar() {	return car;	}	public void setCar(Car car) {	this.car = car;	}	@Override	public String toString() {	return "Person [id=" + id + ", name=" + name + ", car=" + car + "]";	} }

定义 JavaConfig  类 对于一个 POJO 类,在类上使用@Configuration 注解,将会使当前类作为一个 Spring 的容器来使用,用于完成 Bean 的创建。在该 JavaConfig 的方法上使用@Bean,将会使一个普通方法所返回的结果变为指定名称的 Bean 实例。

package com.lzl.spring.entity;   import org.springframework.beans.factory.annotation.Autowire; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;   //该注解表示这个类为javaConfig类 @Configuration public class MyConfig {	//该注解表示:向容器中注册一个叫做myCar的对象	@Bean("myCar")	public Car getCar() {	return new Car("保时捷","911",300);	}	//该注解表示:向容器中注册一个叫做person的对象	//并且通过byType的方式注入car	@Bean(name="person",autowire=Autowire.BY_TYPE)	public Person getPerson() {	return new Person(1001,"望穿秋水见伊人");	} }

xml文件只需要添加自动扫描包配置

<?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:context="http://www.springframework.org/schema/context"	xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context.xsd     ">  	<context:component-scan base-package="com.lzl.spring" />   </beans>

测试类

package com.lzl.spring.test;   import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;   import com.lzl.spring.entity.Car; import com.lzl.spring.entity.Person;   public class SpringTest {	@Test	public void test1() {	//读取配置文件         ApplicationContext ctx=new ClassPathXmlApplicationContext("spring-config.xml");         Car car = ctx.getBean("myCar", Car.class);         System.out.println(car);         Person person = ctx.getBean("person", Person.class);         System.out.println(person);	} }

控制台输出

spring如何使用JavaConfig进行配置

感谢你能够认真阅读完这篇文章,希望小编分享的“spring如何使用JavaConfig进行配置”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI