欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring使用JavaConfig進(jìn)行配置的方法

 更新時(shí)間:2021年09月13日 11:31:50   作者:望穿秋水見伊人  
這篇文章主要介紹了spring使用JavaConfig進(jìn)行配置的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下

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實(shí)現(xiàn)經(jīng)典游戲超級(jí)瑪麗的示例代碼

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

    在你的童年記憶里,是否有一個(gè)蹦跳、頂蘑菇的小人?本文將用java語言實(shí)現(xiàn)經(jīng)典游戲《超級(jí)瑪麗》,文中采用了swing技術(shù)進(jìn)行了界面化處理,需要的可以參考一下
    2022-02-02
  • Java異或運(yùn)算應(yīng)用場(chǎng)景詳解

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

    這篇文章主要給大家介紹了關(guān)于Java異或運(yùn)算應(yīng)用場(chǎng)景的相關(guān)資料,異或運(yùn)算會(huì)應(yīng)用在很多算法題中,這里整理了幾個(gè)最常見的應(yīng)用場(chǎng)景,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

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

    IDEA自帶的注釋模板不是太好用,我本人到網(wǎng)上搜集了很多資料系統(tǒng)的整理了一下制作了一份比較完整的模板來分享給大家,下面這篇文章主要給大家介紹了IDEA個(gè)性化設(shè)置注釋模板的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Java經(jīng)典面試題匯總:Spring Boot

    Java經(jīng)典面試題匯總:Spring Boot

    本篇總結(jié)的是Spring-Boot框架相關(guān)的面試題,后續(xù)會(huì)持續(xù)更新,希望我的分享可以幫助到正在備戰(zhàn)面試的實(shí)習(xí)生或者已經(jīng)工作的同行,如果發(fā)現(xiàn)錯(cuò)誤還望大家多多包涵,不吝賜教,謝謝
    2021-07-07
  • 最新評(píng)論