spring使用JavaConfig进行配置的方法
作者:望穿秋水见伊人 发布时间:2023-08-22 20:52:46
标签: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 的创建。在该 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);
}
}
控制台输出
来源:https://blog.csdn.net/peng86788/article/details/81188049
0
投稿
猜你喜欢
- 目录Spring是什么?Spring Boot是什么?Maven依赖项springboot为不同的Spring模块提供了许多启动程序依赖项。
- 最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文
- 初次接触spring-boot的时候,我们经常会看到这样的文章:“
- 本地jvm执行flink带web ui使用StreamExecutionEnvironment executionEnvironment =
- 1、pom.xml配置修改<packaging>jar</packaging>//修改为<packaging&
- 概念Java中的集合就是一种容器,可以容纳不同种类的数据,这些容纳是建立在未知的基础上。优点1.可以动态保存任意多个对象,使用比较方便。2.
- 1、同步消息即时性较强,重要的消息,且必须有回执的消息,例如短信,通知(转账成功)生产者:public class Producer { &
- 前言Spark Sql可以通过UDF来对DataFrame的Column进行自定义操作。在特定场景下定义UDF可能需要用到Spark Con
- 自定义 webflux 容器配置配置代码@Componentpublic class ContainerConfig extends Rea
- 先给大家展示下效果图:1、验证码生成类:import java.util.Random;import java.awt.imag
- Java中List.of()和Arrays.asList()的区别及原因动手写一下,让自己更有印象1.Arrays.asList()可以插入
- 题目要求思路:模拟解题的核心思想在于逆向思维,不考虑每个子数组中的唯一字符个数,转而考虑每个字符可以作为多少个子数组的唯一字符;所以在计算答
- 前言在我们开发过程中,由于主流的架构都是采用前后端分离的方式,我们作为后端开发者需要为前段持续地提供运行在容器中最新代码,虽然可
- 一、背景有时我们在做开发的时候需要记录每个任务执行时间,或者记录一段代码执行时间,最简单的方法就是打印当前时间与执行完时间的差值,一般我们检
- Java单例模式的实现,对java 单例模式的几种实现方法进行了整理:单例模式好多书上都是这么写的:public class SingleT
- 面试题1:说一下抽象类和接口有哪些区别?正经回答:抽象类和接口的主要区别:从设计层面来说,抽象类是对类的抽象,是一种模板设计;接口是行为的抽
- 概述从今天开始, 小白我将带大家开启 Java 数据结构 & 算法的新篇章.字符串匹配字符串匹配 (String Matching)
- 利用Javaweb开发的一个校园服务系统,通过发布自己的任务并设置悬赏金额,有些类似于赏金猎人,在这里分享给大家,有需要可以联系我:2186
- 这篇文章主要介绍了JAVA如何定义构造函数过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可
- 一、JDBC概述1、数据的持久化持久化(persistence):把数据保存到可掉电式存储设备中以供之后使用。大多数情况下,特别是企业级应用