Spring: automatically assemble User and springbean according to Bean name
This example describes how to automatically assemble a User object by Bean name!
The autowire attribute of the
But it also has a negative impact: After automatic assembly is used, you cannot read the attributes required by the JavaBean from the
. 4.1 principle
The user specifies the method that the bean injects when it is injected, and then calls this method to get it using the bean. The spring container automatically replaces the method body with Cglib and dynamically calls Getbean to fetch the bean and return it. Look at the source code:
1. Parse the Lookup
This article from: http://blog.csdn.net/dqatsh/article/details/3478000
I found a good article http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/ which gives a more detailed description than this article.Springframework 2.5 introduces a complete annotaion configuration annotation. Using these annotation can greatly reduce bean definitions and make Program Development easier and easier to maintain.Of course, you need to use Java 5 or later to
In this chapter we will discuss spring's containers and beans.Containers for 1.Spring(1) beanfactoryThis is made possible by org.springframework.beans.factory.BeanFactory.(2) ApplicationContextThis is made possible by Org.springframework.context.ApplicationContext.In general programming, we are mainly based on ApplicationContext, because it provides a more convenient method.2.ApplicationContextWe generally use three ways to invoke the configuration fi
1. In spring, configure the following 2. Spring beans are as followsUse @configurable to annotate, so that we can directly new Realtimestatistictask, then Realtimestaticdao can also be injected into the normal.3. Copy the Spring-instrument-tomcat-4.1.0.release.jar to the Tomcat\lib directory4. Create a new configuration file in Tomcat\conf\catalina\localhost, {pr
Spring Bean naming code details, springbean
This article mainly describes the bean naming method in spring, and introduces six methods through simple examples, as shown below.
Generally, you must specify an id attribute as the Bean name When configuring a
This example describes how to automatically assemble a User object according to the Bean's name !The Autowire property of the However, there are also negative effects: after using automatic assembly, it is not possible to read from the configuration file what attributes JavaBean needs.1. Write the user object with the following code: Packagecom.importnew; Public classUser {PrivateString name= "Test"; PrivateString sex= "Male"; Private intage=29; Priva
The bean creation process:Spring Beanfactory's Factory mode implementation:Parse People o = (people) Factory.getbean ("P");Because the map has an object type, you need to force the type conversion when you remove itBean's life cycle:The purpose of spring aware is to let the bean get the service of the spring container,
configuring Beans in Spring's IOC containerConfiguration Bean form: based on XML file mode, annotation-based approachTo configure the bean through the bean node in the XML file:XML version= "1.0" encoding= "UTF-8"?>Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springf
an XML file with ClasspathxmlapplicationcontextGet the specific bean by passing in the parameter to the Context.getbean () method, which is the ID name in the XML file;The method in the computer class can be called by instance object p to get the value set for the Computer class property in the configuration file. Packagecom.example.demo.computerTest;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classComputertest {
The first one is the configuration of the internal bean:The first is to understand the principle, and then to operate it is very simple, the following cousin to everyone to say their own point of view (a bit simple, but cousin I first study when watching the video on the code is really a little confused, record their own moment of the time).In fact, you know the meaning of it is very simple, do not be fooled by the name (very advanced name, cousin on the popular understanding),The so-called inte
1.bean LabelIn spring, to configure a bean in XML, you must use the bean tag, which contains two important attributes:ID: The bean tag is unique within the containerClass type of Class:beanScope: Represents the number of instances of the
Spring is a lightweight bean container solution. The core solution is bean creation and bean dependency injection.There are three methods for constructing bean instances in spring.(1) construct an instance through the constructor.
If you need to perform specific initialization and cleanup operations when the bean is created or destroyed, there are two options in spring.
1. Define the init-method and destroy-method attributes of bean in XML.
Execute the init and destroy Methods unforcibly, and configure them freely in XML. The dependency with the
1.Bean creation: The Beanfactory container looks for the bean's definition information, reads the bean definition file, and instantiates it, generating individual bean instances.2. Attribute injection: Using dependency injection, spring configures all bean properties accordi
First, Bean life cyclethe life cycle of Spring beans is easy to understand. when a bean is instantiated, it may be necessary to perform some initialization to get it into a usable state. Similarly, some cleanup may be required when the bean is no longer needed and the bean
The scope property of the bean in spring has the following 5 types:
Singleton represents a singleton in a spring container that always returns a unique instance when the bean is obtained through the spring container
Prototype means a new object is generated each
Spring allows the inheritance of the bean's configuration (specified by the Bean's parent property, such as parent= "Teacher"),The inherited Bean is called the parent bean.The Bean that inherits this parent bean is called a child bean.I child bean inherits the configuration
Add annotations on the class above, on the method above, on the attributes, and with the bean;Writing method: @ annotation Name (attribute name = value)First step: Import the jar packageImport Spring-aop.jar (Spring annotations package);Step two: Create an entity class (annotation object) PackageCOM.AOP;ImportOrg.sprin
It 's written in front .
This demo demonstrates how to troubleshoot a common spring expected single matching bean but found 2 exception.
Https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-expected-single Debugging Troubleshooting expected single Matching Bean but found 2 error
To import the project into
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.