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 的創(chuàng)建。在該 JavaConfig 的方法上使用@Bean,將會使一個普通方法所返回的結果變?yōu)橹付Q的 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配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Springboot Retry組件@Recover失效問題解決方法
在使用springboot的retry模塊時,你是否出現過@Recover注解失效的問題呢?不用擔心,這篇文章就來告訴你解決@Recover失效的辦法,需要的小伙伴可以參考一下2021-11-11
SpringBoot Security整合JWT授權RestAPI的實現
這篇文章主要介紹了SpringBoot Security整合JWT授權RestAPI的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
MyBatis Plus 中 update_time 字段自動填充失效的原因分析
在使用MyBatis Plus時,通常我們會在數據庫表中設置create_time和update_time兩個字段,借助 MyBatis Plus 的自動填充功能來維護這些時間字段,下面通過本文介紹MyBatis Plus 中 update_time 字段自動填充失效的原因分析及解決方案,感興趣的朋友一起看看吧2025-06-06
SpringCloud環(huán)境搭建過程之Rest使用小結
這篇文章主要介紹了SpringCloud環(huán)境搭建之Rest使用,本文通過實例代碼圖文相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08

