Spring common annotations, automatic scanning of assembly beans

Source: Internet
Author: User

1 Introduce the context namespace (in spring's configuration file) with the following configuration file:

XML code
    1. xmlns:context="Http://www.springframework.org/schema/context"
    2. Http://www.springframework.org/schema/context
    3. Http://www.springframework.org/schema/context/spring-context-2.5.xsd

Open the configuration <context:component-scan base-package= "package name (scan this package and child package)"/>

Spring will automatically scan the Cn.pic package with the annotated class below to complete the bean assembly.

XML code
  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-2.5.xsd
  7. Http://www.springframework.org/schema/context
  8. Http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
  9. <context:component-scan base-package="cn.pic"/>
  10. </Beans>

2 Add a jar package for annotations in Classpath

Lib\j2ee\common-annotations.jar

Spring's Context:component-scan scan supports the method of scanning jar packages:

Eclipse comes with the Jar Packager, the default package when there is an option <Add directory entries> is not checked, as long as the tick, you can.

-----------Common Annotations--------

--Define the annotations for the bean

@Controller

@Controller ("Name of the Bean")

Define a control layer bean, such as action

@Service

@Service ("Name of the Bean")

Defining Business Layer Beans

@Repository

@Repository ("Name of the Bean")

Defining DAO Layer Beans

@Component

Defines the bean, which is not used when classifying.

--Automatic assembly of Beans (select an annotation to do)

@Autowired (provided by srping)

Default by type matching, automatic assembly (srping provided), can be written on member properties, or written on setter methods

@Autowired (Required=true)

Be sure to find a matching bean, otherwise throw an exception. The default value is True

@Autowired

@Qualifier ("Bean's name")

Assemble beans by name, used in combination with @autowired, to resolve multiple bean problems by type matching.

@Resource provided by JSR-250

The default is assembled by name when no bean matching the name is found and then assembled by type.

Can be written on member properties, or written on setter methods

You can specify the name of the injected Bean by @resource (name= "Beanname"), and if you do not specify the Name property, the variable name of the member property is used by default, and the Name property is generally not written.

The @Resource (name= "Beanname") specifies the name attribute, injected by name but not found, and is no longer assembled by type.

@Inject was provided by JSR-330.

Assemble by type, less function than @autowired, no need to use.

--Define the scope and life process of the bean

@Scope ("prototype")

Values are: singleton,prototype,session,request,session,globalsession

@PostConstruct

Equivalent to Init-method, used on the method, when the bean is initialized to execute.

@PreDestroy

Equivalent to Destory-method, used on the method, when the bean is destroyed when executed.

--Declarative Transactions

@Transactional

Spring common annotations, automatic scanning of assembly beans

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.