- Spring – BeanFactory
- Guide to the Spring BeanFactory
- Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:
- 1. Introduction
- 2. Basics – Beans and Containers
- 3. Maven Dependencies
- 4. The BeanFactory Interface
- 4.1. The getBean() APIs
- 4.2. The containsBean() API
- 4.3. The isSingleton() API
- 4.4. The isPrototype() API
- 4.5. Other APIs
- 5. BeanFactory API
- 6. Defining the Bean
- 7. Configuring the BeanFactory with XML
- 8. BeanFactory with ClassPathResource
- 9. Conclusion
Spring – BeanFactory
The first foremost thing when we talk about spring is dependency injection which is possible because spring is actually a container and behaves as a factory of Beans. Just like the BeanFactory interface is the simplest container providing an advanced configuration mechanism to instantiate, configure and manage the life cycle of beans. Beans are java objects that are configured at run-time by Spring IoC Container. BeanFactory represents a basic IoC container which is a parent interface of ApplicationContext. BeanFactory uses Beans and their dependencies metadata to create and configure them at run-time. BeanFactory loads the bean definitions and dependency amongst the beans based on a configuration file(XML) or the beans can be directly returned when required using Java Configuration. There are other types of configuration files like LDAP, RDMS, properties file, etc. BeanFactory does not support Annotation-based configuration whereas ApplicationContext does.
Let us do first go through some of the methods of Bean factory before landing up on implementation which are shown below in tabular format below as follows:
- Creating a Spring project using start.spring.io.
- Creating a POJO class.
- Configure the Student bean in the bean-factory-demo.xml file.
- Writing it to application class.
Implementation:
Step 1: Bean Definition: Create a Student POJO class.
// Java Program where we are creating a POJO class // POJO class public class Student < // Member variables private String name; private String age; // Constructor 1 public Student() < >// Constructor 2 public Student(String name, String age) < this.name = name; this.age = age; >// Method inside POJO class @Override public String toString() < // Print student class attributes return "Student'; > >
Step 2: XML Bean Configuration: Configure the Student bean in the bean-factory-demo.xml file.
Step 3: Main Class
// Application class @SpringBootApplication // Main class public class DemoApplication < // Main driver method public static void main(String[] args) < // Creating object in a spring container (Beans) BeanFactory factory = new ClassPathXmlApplicationContext("bean-factory-demo.xml"); Student student = (Student) factory.getBean("student"); System.out.println(student); >>
Note: XmlBeanFactory class is deprecated.
Guide to the Spring BeanFactory
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We rely on other people’s code in our own work. Every day.
It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.
The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.
Lightrun is a new kind of debugger.
It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.
Learn more in this quick, 5-minute Lightrun tutorial:
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:
DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.
The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.
And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:
We’re looking for a new Java technical editor to help review new articles for the site.
1. Introduction
This article will focus on exploring the Spring BeanFactory API.
BeanFactory interface provides a simple, yet flexible configuration mechanism to manage objects of any nature via the Spring IoC container. Let’s have a look at some basics before diving deep into this central Spring API.
2. Basics – Beans and Containers
Simply put, beans are the java objects which form the backbone of a Spring application and are managed by Spring IoC container. Other than being managed by the container, there is nothing special about a bean (in all other respects it’s one of many objects in the application).
The Spring container is responsible for instantiating, configuring, and assembling the beans. The container gets its information on what objects to instantiate, configure, and manage by reading configuration metadata we define for the application.
3. Maven Dependencies
Let’s add the required Maven dependency to the pom.xml file. We will be using Spring Beans dependency to set up the BeanFactory:
org.springframework spring-beans 5.2.8.RELEASE
4. The BeanFactory Interface
It’s interesting to start by having a look at the interface definition in org.springframework.beans.factory package and discuss some of its important APIs here.
4.1. The getBean() APIs
Various versions of getBean() method return an instance of the specified bean, which may be shared or independent across the application.
4.2. The containsBean() API
This method confirms if this bean factory contains a bean with the given name. More specifically, it confirms if the getBean(java.lang.String) able to obtain a bean instance with the given name.
4.3. The isSingleton() API
The isSingleton API can be used to query if this bean is a shared singleton. That is if getBean(java.lang.String) will always return the same instance.
4.4. The isPrototype() API
This API will confirm if getBean(java.lang.String) returns independent instances – meaning a bean configured with the prototype scope, or not.
The important thing to note is this method returning false does not clearly indicate a singleton object. It indicates non-independent instances, which may correspond to other scopes as well.
We need to use the isSingleton(java.lang.String) operation to explicitly check for a shared singleton instance.
4.5. Other APIs
While the isTypeMatch(String name, Class targetType) method checks whether the bean with the given name matches the specified type, getType ( String name ) is useful in identifying the type of the bean with the given name.
Finally, getAliases ( String name ) return the aliases for the given bean name, if any.
5. BeanFactory API
BeanFactory holds bean definitions and instantiates them whenever asked for by the client application – which means:
- It takes care of the lifecycle of a bean by instantiating it and calling appropriate destruction methods
- It is capable of creating associations between dependent object while instantiating them
- It is important to point that BeanFactory does not support the Annotation-based dependency Injection whereas ApplicationContext, a superset of BeanFactory does
Do have a read on Application Context to find out what it can do extra.
6. Defining the Bean
Let’s define a simple bean:
7. Configuring the BeanFactory with XML
We can configure the BeanFactory with XML. Let’s create a file bean factory-example.xml:
Note that we’ve also created an alias for the employee bean.
8. BeanFactory with ClassPathResource
ClassPathResource belongs to the org.springframework.core.io package. Let’s run a quick test and initialize XmlBeanFactory using ClassPathResource as shown below:
public class BeanFactoryWithClassPathResourceTest < @Test public void createBeanFactoryAndCheckEmployeeBean() < Resource res = new ClassPathResource("beanfactory-example.xml"); BeanFactory factory = new XmlBeanFactory(res); Employee emp = (Employee) factory.getBean("employee"); assertTrue(factory.isSingleton("employee")); assertTrue(factory.getBean("employee") instanceof Employee); assertTrue(factory.isTypeMatch("employee", Employee.class)); assertTrue(factory.getAliases("employee").length >0); > >
9. Conclusion
In this quick article, we learned about the main methods Spring BeanFactory API offers and an example to illustrate the configuration and its usage.
The code backing these examples is all available over on GitHub.
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes: