09 spring Annotation-based assembly method

So far, are acquired from the respective container through Bean instance getBean ().

Container Bean scopes:

  1. singleton: Single-state mode. I.e. the entire Spring container, a singleton Bean will be defined in the single embodiment, only one instance. The default is monomorphic.
  2. prototype: prototype model. That same method of acquiring each use getBean  <bean /> example is a new instance.
  3. request: For each HTTP request, both will generate a different Bean instance.
  4. session: for each different HTTP session, will have a different Bean instance.
  5. global session: each corresponding to a global HTTP session Bean instance. Typically, the only effective when using a cluster portlet, multiple Web applications to share a single session. General applications, global-session and the session is identical.

Note:

  1. For values ​​of request scope, session with the global session, when only using Spring in a Web application, the scope to be effective.
  2. For the scope is singleton, the Bean is created when the container is to be better equipped.
  3. For the scope of prototype, Bean example is of use when assembled in the code of the Bean.
  4. The default is singleton

Annotation-based assembly method

Bean instances in the spring do not need to declare a configuration file.

spring-context.xml

<?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.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">

    <context:annotation-config />
    <context:component-scan base-package="com.funtl.leeshop"/>
</beans>

Then use annotations in the class declaration Bean can slightly

spring notes

Statement Bean:

@Repository to annotate DAO

@Service of Service notes

@Controller comment on Controller

@Component addition to other classes of three or more notes 

       Annotation used for specifying the value attribute value id of the bean

 

@Scope used to specify the scope

@scope("prototype")

 

Assembly Bean

By type automatic assembly @Autowired

@Resource melancholy brother name attribute specifies the bean       

@Resource(name = "userService")

 

Guess you like

Origin blog.csdn.net/shmily_syw/article/details/91836346