spring使用JavaConfig進(jìn)行配置的方法
JavaConfig,是在 Spring 3.0 開始從一個(gè)獨(dú)立的項(xiàng)目并入到 Spring 中的。JavaConfig 可以看成一個(gè)用于完成 Bean 裝配的 Spring 配置文件,即 Spring 容器,只不過該容器不是 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 的容器來使用,用于完成 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ì)象 //并且通過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)文章
java 中Object與Objects的區(qū)別在哪里
這篇文章主要介紹了java 中Object與Objects的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Spring中使用自定義ThreadLocal存儲(chǔ)導(dǎo)致的坑及解決
這篇文章主要介紹了Spring中使用自定義ThreadLocal存儲(chǔ)導(dǎo)致的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能
在早期的網(wǎng)絡(luò)編程中,Socket是很常見的實(shí)現(xiàn)技術(shù)之一,比如早期的聊天室,就是基于這種技術(shù)進(jìn)行實(shí)現(xiàn)的,另外現(xiàn)在有些消息推送,也可以基于Socket實(shí)現(xiàn),本文小編給大家介紹了Java利用Socket實(shí)現(xiàn)網(wǎng)絡(luò)通信功能的示例,需要的朋友可以參考下2023-11-11深入剖析java中String、StringBuffer、StringBuilder的區(qū)別
下面小編就為大家?guī)硪黄钊肫饰鰆ava中String、StringBuffer、StringBuilder的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05

Java實(shí)現(xiàn)經(jīng)典游戲超級(jí)瑪麗的示例代碼

Java異或運(yùn)算應(yīng)用場(chǎng)景詳解

IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版