spring使用JavaConfig進(jìn)行配置的方法
JavaConfig,是在 Spring 3.0 開始從一個(gè)獨(dú)立的項(xiàng)目并入到 Spring 中的。JavaConfig 可以看成一個(gè)用于完成 Bean 裝配的 Spring 配置文件,即 Spring 容器,只不過(guò)該容器不是 XML文件,而是由程序員使用 Java 自己編寫的 Java 類。
實(shí)體類:
package com.lzl.spring.entity;
public class Car {
private String brand;//品牌
private String type;//型號(hào)
private double speed;//最大時(shí)速
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 類 對(duì)于一個(gè) POJO 類,在類上使用@Configuration 注解,將會(huì)使當(dāng)前類作為一個(gè) Spring 的容器來(lái)使用,用于完成 Bean 的創(chuàng)建。在該 JavaConfig 的方法上使用@Bean,將會(huì)使一個(gè)普通方法所返回的結(jié)果變?yōu)橹付Q的 Bean 實(shí)例。
package com.lzl.spring.entity;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//該注解表示這個(gè)類為javaConfig類
@Configuration
public class MyConfig {
//該注解表示:向容器中注冊(cè)一個(gè)叫做myCar的對(duì)象
@Bean("myCar")
public Car getCar() {
return new Car("保時(shí)捷","911",300);
}
//該注解表示:向容器中注冊(cè)一個(gè)叫做person的對(duì)象
//并且通過(guò)byType的方式注入car
@Bean(name="person",autowire=Autowire.BY_TYPE)
public Person getPerson() {
return new Person(1001,"望穿秋水見伊人");
}
}
xml文件只需要添加自動(dòng)掃描包配置
<?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>
測(cè)試類
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);
}
}
控制臺(tái)輸出

到此這篇關(guān)于spring使用JavaConfig進(jìn)行配置的文章就介紹到這了,更多相關(guān)spring JavaConfig配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot Retry組件@Recover失效問題解決方法
在使用springboot的retry模塊時(shí),你是否出現(xiàn)過(guò)@Recover注解失效的問題呢?不用擔(dān)心,這篇文章就來(lái)告訴你解決@Recover失效的辦法,需要的小伙伴可以參考一下2021-11-11
java搜索無(wú)向圖中兩點(diǎn)之間所有路徑的算法
這篇文章主要介紹了java搜索無(wú)向圖中兩點(diǎn)之間所有路徑的算法2019-01-01
SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot Security整合JWT授權(quán)RestAPI的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Mybatis 一級(jí)緩存與二級(jí)緩存的實(shí)現(xiàn)
mybatis作為一個(gè)流行的持久化工具,緩存必然是缺少不了的組件。通過(guò)這篇文章,就讓我們來(lái)了解一下一級(jí)緩存與二級(jí)緩存的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
mybatis如何根據(jù)表逆向自動(dòng)化生成代碼實(shí)例
逆向工程是一個(gè)專門為 MyBatis 框架使用者設(shè)計(jì)的代碼生成器,可以根據(jù)數(shù)據(jù)庫(kù)中的表字段名,自動(dòng)生成 POJO 類,mapper 接口與 SQL 映射文件,這篇文章主要給大家介紹了關(guān)于mybatis如何根據(jù)表逆向自動(dòng)化生成代碼的相關(guān)資料,需要的朋友可以參考下2021-08-08
MyBatis Plus 中 update_time 字段自動(dòng)填充失效的原因分析
在使用MyBatis Plus時(shí),通常我們會(huì)在數(shù)據(jù)庫(kù)表中設(shè)置create_time和update_time兩個(gè)字段,借助 MyBatis Plus 的自動(dòng)填充功能來(lái)維護(hù)這些時(shí)間字段,下面通過(guò)本文介紹MyBatis Plus 中 update_time 字段自動(dòng)填充失效的原因分析及解決方案,感興趣的朋友一起看看吧2025-06-06
SpringCloud環(huán)境搭建過(guò)程之Rest使用小結(jié)
這篇文章主要介紹了SpringCloud環(huán)境搭建之Rest使用,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

