spring使用JavaConfig进行配置的方法

作者:望穿秋水见伊人 时间:2023-08-22 20:52:46 

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进行配置的方法

来源:https://blog.csdn.net/peng86788/article/details/81188049

标签:spring,JavaConfig
0
投稿

猜你喜欢

  • 关于Springboot的日志配置

    2022-12-16 10:32:33
  • VsCode配置java环境的详细图文教程

    2022-03-29 00:56:32
  • Android实战教程第八篇之短信备份

    2021-07-17 23:31:43
  • C# 特殊的string类型详解

    2022-02-10 14:11:59
  • c# 几个常见的TAP异步操作

    2021-11-09 21:33:36
  • android开机自启动APP及使用adb命令测试方法

    2022-03-07 06:28:54
  • 详解Java 连接MongoDB集群的几种方式

    2022-10-03 17:50:47
  • 详解Android 获取手机中微信聊天记录方法

    2021-10-25 02:32:38
  • Jaxb2实现JavaBean与xml互转的方法详解

    2023-11-25 11:51:38
  • java中struts2实现简单的文件上传与下载

    2022-12-23 22:53:21
  • Proxy实现AOP切面编程案例

    2023-07-23 06:44:52
  • SpringBoot如何优雅的处理全局异常

    2021-06-09 21:28:13
  • Android如何从实现到封装一个MVP详解

    2023-02-12 10:44:40
  • RocketMQ生产者一个应用不能发送多个NameServer消息解决

    2022-05-18 15:56:11
  • c# WPF中CheckBox样式的使用总结

    2023-07-17 15:44:46
  • Java实现考试系统

    2023-11-18 04:15:03
  • 浅谈Java方法调用的优先级问题

    2023-07-01 13:40:08
  • java按字节截取带有汉字的字符串的解法(推荐)

    2022-10-08 23:49:49
  • C#使用OpenCv图像批处理并改变图片大小并且重命名

    2023-12-20 11:10:29
  • C#图书管理系统 附源码下载

    2023-10-19 18:30:13
  • asp之家 软件编程 m.aspxhome.com