In Springframework, we cannot @autowired a static variable to make it a spring bean, such as the following:Java code@Autowiredprivate static YourClass YourClass;Can try, yourclass in this state can not be dependent injection, will throw run-time exception java.lang.NullPointerException, why? The static variable/class variable is not an object's property, but a property of a class. Spring is based on
Spring IOC dependency injection instance
Operation steps: 1. Create all classes in beans. xml 2. Inject dependent classes 2. 1. Create corresponding Getter and setter methods for each dependent class
2. constructor Injection
. ByName is generally used in automatic injection (not commonly used) development.
Autowire = "default", byName: Injection by name (setUserDao, named userDao); byT
this time is immediately loaded .2. Lazy loading: Instead of creating an object while reading a configuration file, an object is created when the object is used . Benefits: Memory savings can be achieved3. Configuring Lazy Loading --------------------------------------------------------------------------------# # # IOC (important): controlled inversion (inversion of control), originally created objects and Management objects are completed by program code, after using
)
The type of dependency range:
Compile: Compile tie range, default , for compiling, test, execute three kinds of classpath are effective, such as: Spring-core
Test: test-dependent range, only valid for test classpath, such as: JUnit
Provided: The dependency range has been provided, which is effective for compiling and test
object) in a way that becomes an external injection.
implementing Ioc/di with the Spring framework
1, import spring framework;
2. Create an instance in the configuration file
Class: Represents the name of the category.
Id/name: Instance name 3, obtaining instances of spring configuration
1 ApplicationContext class.
Newfilesystemxmlapplicationcontext ("Src/appl
A reference to a member variable of a generic type that can be injected into a subclass for a subclass in Spring 4.x (so that the generic classes corresponding to subclasses and subclasses automatically establish a relationship)Specific Description:Generic injection: Bean1 and Bean2 are injected with generics, and Bean1 and Bean2 establish dependencies, which are automatically established by class Bean3 (inheritance Bean1) and Bean4 (inheritance bean2
What is dependency injection?In a dependency injection pattern, the creation of the callee's work is no longer done by the caller, and the creation of the callee instance is usually done by the spring container and then injected into the caller.Why use Dependency Injection?In order to achieve loose coupling between cod
As we all know, spring function is very powerful ~ mainly in two aspects: one is the IOC control inversion, and the other is AOP oriented aspect thinking.
Spring can host hibernate as well as transactions, and it also provides hibernatetemplate,hibernatedaosupport,hibernatecallback three classes to simplify the operation of Hibernante
Their roles are:
Hibernatetemplate: The façade class for all persisted op
IntroductionSuppose that a singleton pattern of bean a needs to refer to another bean B that is not a singleton pattern, so that we can get the latest bean B every time we reference, we could let the bean A by implementing Applicationcontextware to perceive ApplicationContext (that is, the container context can be obtained), so that it can be run through Applicationcontext.getbean (String Beanname) method to get the latest bean B. But if you use the Applicationcontextaware interface, let us be c
1, dependent injection-dependency injectionThe Bean object relies on the spring container creation, and the resources that the Bean object relies on are set and assembled by the container
L who depends on who: the application relies on the IOC container;
Why you need to rely on: The application needs an IOC container to provide the external resources required by the object;
L who injects wh
Spring MVC framework. The bank example shows how to model and build simple applications. The sample application contains some technologies (such as dependency injection) that have been learned, but mainly demonstrates the features of spring MVC.
Before you start, download t
See also: http://blog.csdn.net/fei641327936/article/details/52015121Mybatis:Implement the IOC's lightweight container for a beaninversion of control inversion : The relationship between container control programs, not program code operationsDepend Inject Dependency InjectionAspect oriented programming programming for facetsSpring can help us create and assemble dependencies between objects based on the configuration file;
One, a long time ago, when we did not @autowire annotations, dependency injection either through setter method, or through the construction method;Need to configure a lot of property-ref in the configuration file ....Second, if the use of annotations, the code is more intrusive, the late change is also very painful;Three, how to do? -----> with default-autowire!!Nonsense not much to say, directly on the code:1, configuration file:1 Beansxmlns= "Http:/
Spring commonly used two kinds of dependency injection method: One is to set the value injection method, the bean's setter method sets the Bean attribute value, the other is the construction injection, by giving the bean's construction method to pass the parameters to implement the Bean attribute assignment value;1. Set Value injection modeDirectly on the code example, the tree structure of the example is s
CompanyServicelocalCompanyService=(CompanyService)getBean("companyService");
Note: the contents of the configuration file about Companyservice:
123
id = " Companyservice " class = " Com.kaiwii.service.system.impl.CompanyServiceImpl " > NBSP;NBSP;NBSP;NBSP;NBSP;NBSP;NBSP;NBSP; Code class= "XML keyword" >constructor-arg index = ref = Code class= "XML string" "Companydao" /> bean >
Used in projects:Sendforoaapprove Sendforapprove =
In Springframework, we cannot @autowired static variables, making spring beans, for example, not so:@Autowiredprivate static YourClass YourClass;Can try, yourclass in such a state can not be dependent injection, will throw the execution of the exception java.lang.NullPointerException, why? A static variable/class variable is not an object's property, but a property of a class. Spring is based on
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.