Spring Tutorial (i)

Source: Internet
Author: User

first, what is Spring?Spring in fact refers to the spring framework, which is a sub-project under Spring, and Spring has developed a number of other projects around the core project of the spring framework, such as spring security,spring Data,spring Webflow and so on. Spring is born to simplify Java EE development, and the spring framework is the most used in Java EE, and then we are mainly learning the spring framework. The spring framework includes his core solution IOC container, Spring AOP. In addition, there is support for the web, data access layer. Here is the Spring Framework frame composition:   ii. Spring IOC containerSpring IoC (inversion of control, inversion of controls) is the most central part of the spring framework. The so-called inversion of control means that the management of dependency relationships by using IOC container objects is reversed, that is, the dependencies between objects are managed by the IOC container, and the IOC container completes the injection of the object by means of a dependency injection (di,dependency injection). In the development of spring, we need to register all classes in spring's IOC container, tell spring what you are, what you need, and then the IOC container will create this object for you when you need it and other object instances that the object depends on (this is dependency injection). The creation and destruction of these classes is managed by the IOC container and is no longer maintained by the object referencing it. Transforms the previous "Object-object" dependency pattern into a dependency pattern for the object-IOC container-object. Spring provides two ways to register an IOC container: XML and annotations. iii. XML configuration of the Spring IOC1. Add Spring Basic Dependency Package
Aopalliance-1.0.jarcommons-logging-1.1.1.jarspring-aop-3.2.0.release.jarspring-beans-3.2.0.release.jarspring-contex T-3.2.0.release.jarspring-core-3.2.0.release.jarspring-expression-3.2.0.release.jar
2. Add Spring configuration file Applicationcontext.xml
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  5. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
  6. <Bean id= "Userdao" class="Com.boya.spring.ioc.UserDao" />
  7. <Bean id= "examplebean" class="Com.boya.spring.ioc.ExampleBean">
  8. <property name="name" value="Boya" />
  9. <property name= "Userdao" ref="Userdao" />
  10. </Bean>
  11. </Beans>
3. Create the user class and the Examplebean class user class, respectively:
    1. Public class Userdao {
    2. Public String GetName () {
    3. return "Boya";
    4. }
    5. }
Examplebean class:
  1. Public class Examplebean {
  2. private String name;
  3. private Userdao Userdao;
  4. public void print () {
  5. System.out.println ("Name is:" +name);
  6. }
  7. public void Userprint () {
  8. System.out.println ("User name is:" +userdao.getname ());
  9. }
  10. //Omit getter, setter method
  11. }
4. IOC container test
    1. ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
    2. Examplebean Examplebean = Context.getbean ("Examplebean", Examplebean.   Class);
    3. Examplebean.print ();
    4. Examplebean.userprint ();
name Is:boyauser name Is:boya
iv. Annotations of the Spring IOCSpring's dependency configuration is loosely coupled to the core of the spring framework itself. However, until Spring 3.0, using XML for dependency configuration was almost the only option. The advent of Spring 3.0 has changed this situation by providing a series of annotations for dependency injection, which makes the spring IoC a viable alternative to XML files. The following describes how to use these annotations for configuration-dependent management.   I divided the annotations into two categories, the first for property assembly, and the second for class registration. There are two types of annotations used in   attribute assembly, namely autowired, Resource. @Autowired1, @Autowired are injected by default by type matching (bytype) 3, @Autowired annotations can be used for member variables, setter methods, constructor functions, etc. 4, With @autowired annotations There must be only one bean matching it, and when no matching bean is found or there are multiple matching beans, the spring container throws an exception 5, Spring allows us to specify the name of the injected Bean by @Qualifier comment. When @Autowired and @Qualifier are used together, the automatic injection strategy transforms from Bytype to ByName   @Resource1, @Resource is equivalent to @Autowired, but @Autowired by B YType automatic injection, @Resource by default by ByName automatic injection 2, @Resource has two properties, respectively, name and type,spring the Name property of the @Resource annotation to the name of the Bean, and type property is resolved to the type of the Bean. So if you use the Name property, you use the ByName Auto-injection policy, and the Type property uses the Bytype auto-injection policy.   When a class needs to be registered with an IOC container, you can use @component, @Repository, @Service and @Controller   @Component1, @ Component is a common form of all spring-managed components, while @repository, @Service, and @Controller are @component refinements to represent more specific use cases (respectively, the persistence layer, the service layer, and the presentation layer) 2, Bea defined using the @component annotationN, the default name (ID) is the unqualified class name beginning with lowercase. The bean name as defined by the Userdao class is Userdao. You can also specify the name of the Bean: @Component ("abc")   below, we'll change the above example to the form of a callout configuration. First, you need to modify the configuration file Applicationcontext.xml as follows:
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="Http://www.springframework.org/schema/context"
  5. xsi:schemalocation= "Http://www.springframework.org/schema/beans
  6. Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. Http://www.springframework.org/schema/context
  8. Http://www.springframework.org/schema/context/spring-context-3.0.xsd ">
  9. <context:component-scan base-package="com.boya.spring.ioc" />
  10. </Beans>
The Base-package property of the <context:component-scan/> Specifies the class package that needs to be scanned, and annotations used by all classes in the class package and its recursive child packages are processed. 2. Add the annotation Userdao class to the class:
    1. @Repository
    2. Public class Userdao {
    3. Public String GetName () {
    4. return "Boya";
    5. }
    6. }
Examplebean class:
  1. @Service
  2. Public class Examplebean {
  3. @Resource
  4. @Value ("Boya")
  5. private String name;
  6. @Resource
  7. private Userdao Userdao;
  8. public void print () {
  9. System.out.println ("Name is:" +name);
  10. }
  11. public void Userprint () {
  12. System.out.println ("User name is:" +userdao.getname ());
  13. }
  14. }
As explained earlier, annotations @autowired and @service for assembly properties are available for member variables, so when we set these two annotations on the member variable, the setter method can be removed. 3. IOC container test
    1. ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
    2. Examplebean Examplebean = Context.getbean ("Examplebean", Examplebean.   Class);
    3. Examplebean.print ();
    4. Examplebean.userprint ();
Output Result:
name Is:boyauser name Is:boya

Spring Tutorial (i)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.