Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure spring using JavaConfig

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to configure spring using JavaConfig, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

JavaConfig was incorporated into Spring from a separate project since Spring 3. 0. JavaConfig can be thought of as a Spring configuration file used to complete the Bean assembly, that is, the Spring container, except that the container is not a XML file, but a Java class written by the programmer himself using Java.

Entity class:

Package com.lzl.spring.entity; public class Car {private String brand;// brand private String type;// model private double speed;// maximum 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 + "]";}}

Defining a JavaConfig class for a POJO class, using the @ Configuration annotation on the class will make the current class use as a container for Spring to complete the creation of the Bean. Using @ Bean on the method of this JavaConfig will change the result returned by a normal method to an instance of Bean with the specified name.

Package com.lzl.spring.entity; import org.springframework.beans.factory.annotation.Autowire;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration / / this annotation indicates that this class is a java configuration class @ Configurationpublic class MyConfig {/ / this annotation says: register an object called myCar with the container @ Bean ("myCar") public Car getCar () {return new Car ("Porsche", "911,300") } / / this note says: register an object called person in the container / / and inject car @ Bean (name= "person", autowire=Autowire.BY_TYPE) public Person getPerson () {return new Person (1001) into the container by byType;}}

Xml files only need to add automatic scan package configuration

Test class

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 () {/ / read configuration file 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);}}

Console output

Thank you for reading this article carefully. I hope the article "how to configure spring with JavaConfig" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report