[Spring] works spring and injection method Introduction

Spring Works

         a spring framework for web services, and may be used to hibernate struts2 integration, and can be configured a variety of bean, and maintenance of bean and bean relationship between , mainly for the section (AOP) and inversion of control ( the IOC ) approach, i.e. reflected, reducing the degree of coupling items.

Spring injection method

     Here mainly introduce three, respectively

  1. set way
  2. constructor way
  3. autowired way

     1.set way

       I.e. by injecting property set methods, as shown in FIG.

                                  

 2.constructor way

  

 

3. Notes mode

           This comment has not been the best start to take effect, it is the lack of a key a <context: Annotation-config /> . In fact, what it did not comment substantial operations, mainly by the configuration as a tool to show what needs to be scanned injection. The main role is to activate registered in the spring of bean.

         Annotation and another annotation very similar to the <context: component-scan> addition <context: annotation-config /> function, but also has automatically objects with @ component, @ service, @ Repository annotation, etc. registered the spring container function, that is to say after we configured to scan package, you can not write any of the bean

<context:component-scan base-package="com.xxx"/>

If both are configured, then <context: annotation-config /> are ignored, and no problem replicate injections.

For detailed configuration, as shown in FIG.

   

<?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

">
    <! -
Each configuration file must be part of the spring is simply .-->
    <! - declare the default namespace xml file, are not used all the default labels other namespaces namespace .- ->
    <! - declare XML Schema instance schemaLocation attribute declaration can be used after a .-->
    <- xsi:! schemaLaction part: xsd specifications file for the specified namespace configuration above, so you making the following specific the allocation of time will be given in accordance with these specifications xsd file the appropriate prompt, ->
    <! - for example, each label is how to write, what are some attributes that can be smart tips, ->
    <! - - to prevent configuration errors and less easy troubleshooting, the configuration will be verified according to xsd specifications at the time of starting the service .-->
    <! - but here you need to mvc above configuration xmlns inside, aop, tx xsd configuration specification file so .-->
 
  <context: Annotation-config />
    <the bean ID = "Student" class = "bean.Student" the autowire = "the byType">
        <Property name = "name" value = "the Hello World! "> </ Property> </ bean>
    <bean the above mentioned id ="helloWorld" class="bean.HelloWorld"></bean>
</beans>

 

 

 

Published 47 original articles · won praise 8 · views 30000 +

Guess you like

Origin blog.csdn.net/nanchengyu/article/details/85916793