Spring framework knowledge summary-various exceptions injected into beans
Recently, we encountered a series of exceptions when integrating the spring and hibernate frameworks. This time we mainly explain possible exceptions and solutions in the spring framework.
We use the powerful bean container management mechanism of sping to easily implement the life cycle management of javabean through BeanFactory. However, we will inevitably encounter some exceptions during configuration management:
Exception 1: No qualifying bean of type […] found for dependency
For example, BeanB is automatically injected into BeanA
@Component
public class BeanA {
@Autowired
private BeanB dependency;
…
}
If BeanB is not defined in sping’s Cntextl at this time, then at startup An exception will be thrown: the no such bean definition exception:
org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [org.baeldung.packageB.BeanB] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
The reason for the problem is simple expected at least 1 bean which qualifies as autowire candidate for this dependency. There needs to be at least one defined bean for dependency injection. Of course, another possible reason is that we did not set the path for package scanning when using annotations and configuring annotation scanning. Otherwise, there should be no bean defined.
Exception 2: No qualifying bean of type […] is defined
This exception means that there is no qualifying bean of type. The reason is that we have defined two or more identical beans instead of a unique bean. For example, there is a Interface IBeanB, its two implementation classes Bean1 and Bean2
@Component
public class BeanB1 implements IBeanB {
//
}
@Component
public class BeanB2 implements IBeanB {
//
}
If BeanA is injected at this time With the interface IBeanB, Spring does not know which implementation class to use to inject
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
…
}
At this time, the BeanFactory will throw an exception NoSuchBeanDefinitionException
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [org.baeldung.packageB.IBeanB] is defined:
expected single matching bean but found 2: beanB1,beanB2
from exception expected single matching bean but found 2". It can be seen that the only bean was not found.
Then it can be solved by the following method
@Component
public class BeanA {
@Autowired
@Qualifier("beanB2")
private IBeanB dependency;
…
}
sping will clearly know which Bean is used as the object for injection.
Exception 3: No Bean Named […] is defined
When this exception occurs, go to the Sping context to find the bean by name. , an exception NoSuchBeanDefinitionException may occur
@Component
public class BeanA implements InitializingBean {
@Autowired
private ApplicationContext context;
@Override
public void afterPropertiesSet() {
context.getBean("someBeanName");
}
}
When searching here, there is no definition named someBeanName, causing an exception
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No bean named 'someBeanName' is defined
It is clearly pointed out that it does not exist The bean definition of this name.
Exception 4: Proxied Beans
When a bean uses the JDK dynamic proxy mechanism in the spring context, then the proxy class does not inherit the target object, but it implements the same interface. For this reason, if a Bean is injected with an interface , then there will be no problem, but if an implementation class is injected, the Sping container will not be able to find the bean at this time because the proxy class does not inherit the target class. A very common scenario why a bean is proxied is to use spring's transaction support function. You can use the @Transactional annotation to indicate a transaction, or you can set it in the configuration file.
For example, if ServiceA injects ServiceB, and both services are configured with transactions at the same time, problems will occur through class injection.
@Service
@Transactional
public class ServiceA implements IServiceA{
@Autowired
private ServiceB serviceB;
…
}
@Service
@Transactional
public class ServiceB implements IServiceB{
…
}
Same as this Both Services will run normally if interface injection is used.
@Service
@Transactional
public class ServiceA implements IServiceA{
@Autowired
private IServiceB serviceB;
…
}
@Service
@Transactional
public class ServiceB implements IServiceB{
…
}

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor
