SlideShare a Scribd company logo
Spring ecosystem
By Vo Van Hai
vovanhai@iuh.edu.vn
https://docs.spring.io/spring-framework/docs/6.0.6/reference/html/
Introduction
• The Spring ecosystem provides a comprehensive
programming and configuration model for modern
Java-based enterprise applications - on any
deployment platform.
• A key element of Spring is infrastructural support at
the application level: Spring focuses on the
"plumbing" of enterprise applications so that teams
can focus on application-level business logic,
without unnecessary ties to specific deployment
environments.
Spring Projects
• https://spring.io/projects
• Consists of 23 projects (March 2023):
Spring History
Version Date Notes
0.9 2003
1.0 March 24, 2004 First production release.
2.0 2006
3.0 2009
4.0 2013
5.0 2017
6.0 November 16, 2022
• Rod Johnson
• “Expert One-on-One J2EE Design and
Development,” Wrox. ISBN 0-7645-4385-
7, 2002
• “Expert One-on-One J2EE Development
without EJB,” Wrox. ISBN 0-7645-5831-5,
2024
Spring Framework Overview
• Although Spring is an ecosystem, the heart of all
other projects is based on the Spring Framework.
• Spring makes it easy to create Java enterprise
applications. It provides everything you need to
embrace the Java language in an enterprise
environment, and with the flexibility to create
many kinds of architectures depending on an
application’s needs.
• As of Spring Framework 6.0, Spring requires Java
17+.
"Spring“: different things in different contexts
Spring Framework components
https://docs.spring.io/spring-framework/docs/6.0.6/reference/html/
Spring Framework Core
• Is the core of the Spring ecosystem.
• Based on it, other projects are developed.
• Foremost amongst these is the Spring Framework’s
Inversion of Control (IoC) container.
Inversion of Control (IoC)
Dependency injection (DI)
• Dependency Inject is a technique (a design pattern)
that removes hard-code dependencies and makes
your application easier to extend and maintain.
Without DI
package vvh.ioc.example;
public class ICEngine {
private float cylinder_capacity;
private String type;
public void start() {
System.out.println("Engine is started");
}
}
package vvh.ioc.example;
public class Car {
private ICEngine engine;
public void start(){
engine =new Engine();
engine.start();
}
}
package vvh.ioc.example;
public class MyApp {
public static void main(String[] args) {
Car c =new Car();
c.start();
}
}
Tightly-coupled
package vvh.ioc.example;
public class ICEngine {
private float cylinder_capacity;
private String type;
public ICEngine(float cylinder_capacity, String type) {
this.cylinder_capacity = cylinder_capacity;
this.type = type;
}
public void start() {
System.out.println("Engine is started");
}
}
What will happen when the Engine class is changed?
With DI
package vvh.ioc.example;
public class ICEngine {
private float cylinder_capacity;
private String type;
public void start() {
System.out.println("Engine is started");
}
}
package vvh.ioc.example;
public class Car {
private ICEngine engine;
public Car(ICEngine engine) {
this.engine = engine;
}
public void start(){
engine.start();
}
}
package vvh.ioc.example;
public class MyApp {
public static void main(String[] args) {
ICEngine engine = new ICEngine();
Car c =new Car(engine);
c.start();
}
}
Inject Engine object to Car class
Changes to the Engine class will not
affect to Car class.
What will happen when we have
another engine type? (E.g., Hybrid
Engine, Electricity Engine, ...)
Example
Injection Types
• Constructor Injection
• Property Injection
• Method Injection
The Spring’s IOC Container
• Remind: IoC is also known as dependency injection
(DI) - a process whereby objects define their
dependencies through constructor arguments,
arguments to a factory method, or properties.
• The org.springframework.beans and
org.springframework.context packages are the
basis for Spring Framework’s IoC container.
• The BeanFactory object provides the configuration
framework and basic functionality, and the
ApplicationContext object adds more enterprise-
specific functionality.
The Spring’s IOC Container
• In Spring, the objects that
form the backbone of your
application and that are
managed by the Spring IoC
container are called beans.
• A bean is an object that is
instantiated, assembled, and
managed by a Spring IoC
container.
• Beans, and the dependencies
among them, are reflected in
the configuration metadata
used by a container.
The Spring’s IoC container
The primary job of the ApplicationContext is to manage beans.
Spring Bean
• In Spring, a bean is an object that the Spring
container instantiates, assembles, and manages.
• Any Java POJO class can be a Spring Bean if
configured and initialized through the container by
providing configuration information.
• We should define beans for service layer objects,
data access objects (DAOs), presentation objects,
infrastructure objects such as Hibernate
SessionFactories, JMS Queues, and so forth.
Spring Bean Scope
• Singleton: (default) Only one instance of the bean will
be created per container. This is the default scope for
spring bean.
• Prototype: An instance of the bean will be created for
each request.
WEB-CONTEXT
• Request: same as prototype scope, but for web
application, an instance of bean will be created for each
HTTP request.
• Session: Each bean instance will be created for each
HTTP Session
• Global-Session: Used to create global session beans for
Portlet applications.
Configuring Beans in the Container
• The primary job of the ApplicationContext is to
manage beans → application must provide the
bean configuration to the ApplicationContext
container.
• Type of configurations:
• XML-Based Configuration
• Java-Based Configuration
• Annotation-Based Configuration
implementation 'org.springframework:spring-context:6.0.6'
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
Gradle
Maven
Configuring Beans in the Container
XML-Based Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/
beans http://www.springframework.org/schema/beans/spring-
beans.xsd">
<bean id="st1" class="org.example.Student">
<property name="id" value="001"/>
<property name="name" value="than thi det"/>
</bean>
</beans>
Beans.xml
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student st1 = context.getBean("st1", Student.class);
System.out.println(st1);
}
}
Object Injection
Object Injection (cont.)
Collection Injection
Literal Values Injection
Spring’s Auto-wiring
• Autowiring feature of spring framework enables you to inject the
object dependency implicitly. It internally uses setter or
constructor injection.
• Autowiring can't be used to inject primitive and string values. It
works with reference only.
No. Mode Description
1 no
The default autowiring mode. It means no auto-wiring by
default.
2 byName
The byName mode injects the object dependency according to
name of the bean. In such case, property name and bean name
must be same. It internally calls setter method.
3 byType
The byType mode injects the object dependency according to
type. So property name and bean name can be different. It
internally calls setter method.
4 constructor
The constructor mode injects the dependency by calling the
constructor of the class. It calls the constructor having large
number of parameters.
5 autodetect deprecated since Spring 3.
Table: Autowiring Modes
Spring’s Auto-wiring
Find bean with the id “faculty”
can be omitted
explicit
Spring’s Auto-wiring
Find bean with the type “Faculty”
Find bean with the type “Faculty”
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx =
new AnnotationConfigApplicationContext(UserServices.class);
User u = ctx.getBean(User.class);
System.out.println(u);
}
}
Configuring Beans in the Container
Java-Based Configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserServices {
@Bean
public Group groupService() {
return new Group("Admin Group");
}
@Bean
public User userService() {
return new User("teo", "123", groupService());
}
}
Configuring Beans in the Container
Annotation-Based Configuration
<context:annotation-config/> only looks for annotations on
beans in the same application context in which it is defined.
@Configuration
@ComponentScan(“your.package")
public class AppConfig {
//no-op
}
You can also use @Configuration
annotation with the same purpose
The @Autowired annotation
• The @Autowired annotation allows Spring to
resolve and inject collaborating beans into another
bean.
public static void main(String[] args) {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
User us = (User) ctx.getBean("user");
us.doFoo();
System.out.println(us);
}
Field Constructor Setter
@Autowired xml config
@Autowired Java-based config
@Component
public class MyNumberFormatter {
public String format(double number) {
return "My Number Format - " + number;
}
}
@Component
public class MyNumberFormatService {
private final MyNumberFormatter myNumberFormatter;
//@Autowired - not required. SpringFX is smart enough to known.
public MyNumberFormatService(MyNumberFormatter myNumberFormatter) {
this.myNumberFormatter = myNumberFormatter;
}
public void printFormat(double number) {
System.out.println(myNumberFormatter.format(number));
}
}
@Configuration
@ComponentScan("org.example.autowired")
public class AppConfig {
//no-op
}
@Autowire Disambiguation
@Component
public class MyFormatService {
private MyFormatter formatter;
public MyFormatService(MyFormatter formatter) {
this.formatter = formatter;
}
public void printFormat() {
System.out.println(formatter.format());
}
}
Error
Disambiguation solution: @Qualifier
When there are multiple beans of the same type → use @Qualifier to avoid ambiguity.
Spring uses the bean's name as a default qualifier value.
Disambiguation solution: @Primary
• @Primary indicates that a particular bean should
be given preference when multiple beans are
candidates to be autowired to a single-valued
dependency.
Automatically select MyNumberFormater class
Inject resources with @Value
@Configuration
@ComponentScan("org.example.resources")
@PropertySource("classpath:application.properties")
public class AppConfig {
@Bean
public ClientBean clientBean() {
return new ClientBean();
}
}
public class ClientBean {
@Value("classpath:beans.xml")
private Resource myResource;
@Value("${foo.permission}")
private String permission;
public void doSomething() throws IOException {
File file = myResource.getFile();
String s = new
String(Files.readAllBytes(file.toPath()));
System.out.println(s);
System.out.println(permission);
}
}
public class Main {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context =
new
AnnotationConfigApplicationContext(AppConfig.class);
ClientBean bean = context.getBean(ClientBean.class);
bean.doSomething();
}
}
@Configuration
@ComponentScan("org.example.resources")
@PropertySource("classpath:application.properties")
public class AppConfig {
@Bean
public ClientBean clientBean() {
return new ClientBean();
}
}
2-0. Spring ecosytem.pdf

More Related Content

PPT
Spring framework
PPTX
Spring IOC and DAO
PPTX
Skillwise-Spring framework 1
PDF
Introduction to Spring Framework
PPTX
Spring introduction
PPTX
Spring framework
PPT
Spring training
ODT
Spring IOC advantages and developing spring application sample
Spring framework
Spring IOC and DAO
Skillwise-Spring framework 1
Introduction to Spring Framework
Spring introduction
Spring framework
Spring training
Spring IOC advantages and developing spring application sample

Similar to 2-0. Spring ecosytem.pdf (20)

ODP
Java EE web project introduction
PPSX
Spring - Part 1 - IoC, Di and Beans
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PPT
Spring training
PPT
Os Johnson
PPT
Spring Basics
PPTX
Spring framework
PPT
Spring - a framework written by developers
PPTX
PPTX
Spring core
PPTX
Spring MVC 5 & Hibernate 5 Integration
ODP
Spring User Guide
PPTX
Spring boot Introduction
PDF
The Basic Concept Of IOC
PPTX
Spring MVC framework
PPTX
Introduction to Spring Framework
PPTX
Spring (1)
DOCX
02 java spring-hibernate-experience-questions
PPTX
The Spring Framework: A brief introduction to Inversion of Control
PPTX
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java EE web project introduction
Spring - Part 1 - IoC, Di and Beans
Session 43 - Spring - Part 1 - IoC DI Beans
Spring training
Os Johnson
Spring Basics
Spring framework
Spring - a framework written by developers
Spring core
Spring MVC 5 & Hibernate 5 Integration
Spring User Guide
Spring boot Introduction
The Basic Concept Of IOC
Spring MVC framework
Introduction to Spring Framework
Spring (1)
02 java spring-hibernate-experience-questions
The Spring Framework: A brief introduction to Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Ad

Recently uploaded (20)

PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Insiders guide to clinical Medicine.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Pre independence Education in Inndia.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Insiders guide to clinical Medicine.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pre independence Education in Inndia.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
Ad

2-0. Spring ecosytem.pdf

  • 1. Spring ecosystem By Vo Van Hai vovanhai@iuh.edu.vn https://docs.spring.io/spring-framework/docs/6.0.6/reference/html/
  • 2. Introduction • The Spring ecosystem provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any deployment platform. • A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.
  • 4. Spring History Version Date Notes 0.9 2003 1.0 March 24, 2004 First production release. 2.0 2006 3.0 2009 4.0 2013 5.0 2017 6.0 November 16, 2022 • Rod Johnson • “Expert One-on-One J2EE Design and Development,” Wrox. ISBN 0-7645-4385- 7, 2002 • “Expert One-on-One J2EE Development without EJB,” Wrox. ISBN 0-7645-5831-5, 2024
  • 5. Spring Framework Overview • Although Spring is an ecosystem, the heart of all other projects is based on the Spring Framework. • Spring makes it easy to create Java enterprise applications. It provides everything you need to embrace the Java language in an enterprise environment, and with the flexibility to create many kinds of architectures depending on an application’s needs. • As of Spring Framework 6.0, Spring requires Java 17+. "Spring“: different things in different contexts
  • 7. Spring Framework Core • Is the core of the Spring ecosystem. • Based on it, other projects are developed. • Foremost amongst these is the Spring Framework’s Inversion of Control (IoC) container.
  • 9. Dependency injection (DI) • Dependency Inject is a technique (a design pattern) that removes hard-code dependencies and makes your application easier to extend and maintain.
  • 10. Without DI package vvh.ioc.example; public class ICEngine { private float cylinder_capacity; private String type; public void start() { System.out.println("Engine is started"); } } package vvh.ioc.example; public class Car { private ICEngine engine; public void start(){ engine =new Engine(); engine.start(); } } package vvh.ioc.example; public class MyApp { public static void main(String[] args) { Car c =new Car(); c.start(); } } Tightly-coupled package vvh.ioc.example; public class ICEngine { private float cylinder_capacity; private String type; public ICEngine(float cylinder_capacity, String type) { this.cylinder_capacity = cylinder_capacity; this.type = type; } public void start() { System.out.println("Engine is started"); } } What will happen when the Engine class is changed?
  • 11. With DI package vvh.ioc.example; public class ICEngine { private float cylinder_capacity; private String type; public void start() { System.out.println("Engine is started"); } } package vvh.ioc.example; public class Car { private ICEngine engine; public Car(ICEngine engine) { this.engine = engine; } public void start(){ engine.start(); } } package vvh.ioc.example; public class MyApp { public static void main(String[] args) { ICEngine engine = new ICEngine(); Car c =new Car(engine); c.start(); } } Inject Engine object to Car class Changes to the Engine class will not affect to Car class. What will happen when we have another engine type? (E.g., Hybrid Engine, Electricity Engine, ...)
  • 13. Injection Types • Constructor Injection • Property Injection • Method Injection
  • 14. The Spring’s IOC Container • Remind: IoC is also known as dependency injection (DI) - a process whereby objects define their dependencies through constructor arguments, arguments to a factory method, or properties. • The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s IoC container. • The BeanFactory object provides the configuration framework and basic functionality, and the ApplicationContext object adds more enterprise- specific functionality.
  • 15. The Spring’s IOC Container • In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. • A bean is an object that is instantiated, assembled, and managed by a Spring IoC container. • Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.
  • 16. The Spring’s IoC container The primary job of the ApplicationContext is to manage beans.
  • 17. Spring Bean • In Spring, a bean is an object that the Spring container instantiates, assembles, and manages. • Any Java POJO class can be a Spring Bean if configured and initialized through the container by providing configuration information. • We should define beans for service layer objects, data access objects (DAOs), presentation objects, infrastructure objects such as Hibernate SessionFactories, JMS Queues, and so forth.
  • 18. Spring Bean Scope • Singleton: (default) Only one instance of the bean will be created per container. This is the default scope for spring bean. • Prototype: An instance of the bean will be created for each request. WEB-CONTEXT • Request: same as prototype scope, but for web application, an instance of bean will be created for each HTTP request. • Session: Each bean instance will be created for each HTTP Session • Global-Session: Used to create global session beans for Portlet applications.
  • 19. Configuring Beans in the Container • The primary job of the ApplicationContext is to manage beans → application must provide the bean configuration to the ApplicationContext container. • Type of configurations: • XML-Based Configuration • Java-Based Configuration • Annotation-Based Configuration implementation 'org.springframework:spring-context:6.0.6' <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.6</version> </dependency> Gradle Maven
  • 20. Configuring Beans in the Container XML-Based Configuration <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/ beans http://www.springframework.org/schema/beans/spring- beans.xsd"> <bean id="st1" class="org.example.Student"> <property name="id" value="001"/> <property name="name" value="than thi det"/> </bean> </beans> Beans.xml import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Student st1 = context.getBean("st1", Student.class); System.out.println(st1); } }
  • 25. Spring’s Auto-wiring • Autowiring feature of spring framework enables you to inject the object dependency implicitly. It internally uses setter or constructor injection. • Autowiring can't be used to inject primitive and string values. It works with reference only. No. Mode Description 1 no The default autowiring mode. It means no auto-wiring by default. 2 byName The byName mode injects the object dependency according to name of the bean. In such case, property name and bean name must be same. It internally calls setter method. 3 byType The byType mode injects the object dependency according to type. So property name and bean name can be different. It internally calls setter method. 4 constructor The constructor mode injects the dependency by calling the constructor of the class. It calls the constructor having large number of parameters. 5 autodetect deprecated since Spring 3. Table: Autowiring Modes
  • 26. Spring’s Auto-wiring Find bean with the id “faculty” can be omitted explicit
  • 27. Spring’s Auto-wiring Find bean with the type “Faculty” Find bean with the type “Faculty”
  • 28. import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(UserServices.class); User u = ctx.getBean(User.class); System.out.println(u); } } Configuring Beans in the Container Java-Based Configuration import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class UserServices { @Bean public Group groupService() { return new Group("Admin Group"); } @Bean public User userService() { return new User("teo", "123", groupService()); } }
  • 29. Configuring Beans in the Container Annotation-Based Configuration <context:annotation-config/> only looks for annotations on beans in the same application context in which it is defined. @Configuration @ComponentScan(“your.package") public class AppConfig { //no-op } You can also use @Configuration annotation with the same purpose
  • 30. The @Autowired annotation • The @Autowired annotation allows Spring to resolve and inject collaborating beans into another bean.
  • 31. public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); User us = (User) ctx.getBean("user"); us.doFoo(); System.out.println(us); } Field Constructor Setter @Autowired xml config
  • 32. @Autowired Java-based config @Component public class MyNumberFormatter { public String format(double number) { return "My Number Format - " + number; } } @Component public class MyNumberFormatService { private final MyNumberFormatter myNumberFormatter; //@Autowired - not required. SpringFX is smart enough to known. public MyNumberFormatService(MyNumberFormatter myNumberFormatter) { this.myNumberFormatter = myNumberFormatter; } public void printFormat(double number) { System.out.println(myNumberFormatter.format(number)); } } @Configuration @ComponentScan("org.example.autowired") public class AppConfig { //no-op }
  • 33. @Autowire Disambiguation @Component public class MyFormatService { private MyFormatter formatter; public MyFormatService(MyFormatter formatter) { this.formatter = formatter; } public void printFormat() { System.out.println(formatter.format()); } } Error
  • 34. Disambiguation solution: @Qualifier When there are multiple beans of the same type → use @Qualifier to avoid ambiguity. Spring uses the bean's name as a default qualifier value.
  • 35. Disambiguation solution: @Primary • @Primary indicates that a particular bean should be given preference when multiple beans are candidates to be autowired to a single-valued dependency. Automatically select MyNumberFormater class
  • 36. Inject resources with @Value @Configuration @ComponentScan("org.example.resources") @PropertySource("classpath:application.properties") public class AppConfig { @Bean public ClientBean clientBean() { return new ClientBean(); } } public class ClientBean { @Value("classpath:beans.xml") private Resource myResource; @Value("${foo.permission}") private String permission; public void doSomething() throws IOException { File file = myResource.getFile(); String s = new String(Files.readAllBytes(file.toPath())); System.out.println(s); System.out.println(permission); } } public class Main { public static void main(String[] args) throws IOException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); ClientBean bean = context.getBean(ClientBean.class); bean.doSomething(); } } @Configuration @ComponentScan("org.example.resources") @PropertySource("classpath:application.properties") public class AppConfig { @Bean public ClientBean clientBean() { return new ClientBean(); } }