1 Introduce the context namespace (in spring's configuration file) with the following configuration file:
XML code
- xmlns:context="Http://www.springframework.org/schema/context"
- Http://www.springframework.org/schema/context
- 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
- <? 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-2.5.xsd
- Http://www.springframework.org/schema/context
- Http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
- <context:component-scan base-package="cn.pic"/>
- </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