Spring Bean Management (annotation management)

Source: Internet
Author: User

The IOC introductory steps for spring annotations:Step one: Download the Spring development package

Http://repo.spring.io/webapp/search/artifact/?0&q=spring-framework

Unzip: (Spring directory structure:)

* Docs: API and Development specifications.

* Libs: Jar package and source code.

* Schema: Constraint.

Step Two: Create a Web project and introduce the Spring development jar Package

Core Jar pack of four: Bean/core/context/expression Language

Two development packages: Log log packet/log4j package

Step three: Create a package structure

Com.itheima.spring.demo1

* UserService

* Userserviceimpl

Step four: Create a configuration file

Naming method:applicationcontext.xml

Constraint file Lookup path:

E:\ various jar Packages \spring\spring-framework-3.2.0.release-dist\spring-framework-3.2.0.release\docs\ spring-framework-reference\html----->xsd-config.html----> Find a context Scheam

annotation XML file:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:       Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context.xsd "><!--bean definitions here--><!--turn on spring component scan annotations--><context:component-scan Base-package= "COM.MICKEYMOUSE.IOC" ></context:component-scan></beans>
Step five: Turn on annotations on classes
Package Com.mickeymouse.ioc;import org.springframework.stereotype.Component; Annotations generate an object similar to the one in the configuration file://<bean id= "Studentservice" class= "Com.mickeymouse.ioc.StudentServiceImpl" ></bean > @Component ("Studentservice") public class Studentserviceimpl implements Studentservice{public void Addstudent () { System.out.println ("Add student Success");}}
Step Six: Write a test class
/** * Spring Management Bean Annotation method */@Testpublic void Test9 () {//Get configuration file string path = "Applicationcontext.xml";//Load configuration file Abstractapplicationcontext ApplicationContext = new Classpathxmlapplicationcontext (path); Studentservice Studentservice = (studentservice) applicationcontext.getbean ("Studentservice"); Studentservice.addstudent ();}


notes commonly used in Spring Bean Management: (emphasis)@Componet: Components (acting on a Class)

Three derivative annotations of @componet in spring: ( functionality is now consistent )


                         1. @Conntroller  : Presentation layer

     Span style= "font-family: ' Microsoft yahei UI '; font-size:10.5pt; line-height:1.5; " >                    2. @Service         &NBSP;: Business layer

     Span style= "font-family: ' Microsoft yahei UI '; font-size:10.5pt; line-height:1.5; " >                    3. @Repository  : Persistence layer

These three annotation purposes: in order to make the label class clearer, spring will strengthen it in subsequent versions

Annotation of attribute injection (using annotation properties to assign a value.) You can not provide a set method

@value : Used to inject an attribute value of a normal type


@Antowired : Automatic assembly

The default is to assemble by type

inject by name:

@Qualifier: Forcibly use name injection (and @Antowired public)

@Resource equivalent: Used to inject object references

Business layer:


DAO layer


Test class:

public class TestContext {/** * traditional way the business layer calls the persistence layer method */@Testpublic void test1 () {ApplicationContext ApplicationContext = new Clas Spathxmlapplicationcontext ("Applicationcontext.xml"); Productsservice Productsservice = Applicationcontext.getbean ("Productsservice", Productsservice.class); Productsservice.addproducts ();}}

Results:


Note on the scope of the bean:

@Scope ("singleton ")

* Singleton :---> Single-case

* prototype:--> Multiple examples of




The Bean's life cycle annotations:


@PostConstruct: equivalent to Init-method

@PreDestroy : equivalent to Destroy-method


Can write two methods to perform initial and destroy

There is also an integrated development of XML and annotations in real-world development:

* Bean has XML configuration. But the attributes used are injected with annotations.

Annotation integration development needs to be used:<context:annotation-config/>



From for notes (Wiz)



Spring Bean Management (annotation management)

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.