spring: Common notes

configuration dependent bean.xml

 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.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context.xsd">
     <! -109 
When the container is created to inform spring packages to be scanned, the configuration is not required in the label bound in beans, but a name
 . 11      context namespace and constraints ->
 12 is      <context: Scan-base- Component Package = "CN .flypig666 "> </ context: Component-Scan>
 13 is  
14 </ Beans>

* Function: the class object for the current stored in the spring container * Properties: * value: specifies the bean id. When we do not write, its default value is the current class name, and change the first letter lowercase * Controller: generally used in the presentation layer * Service: generally used for business layer * Repository: persistence layer is generally used in more than three notes they * Component property to the role and is exactly the same * the three of them are spring provide a clear framework for the three notes we use to make our three objects more clearly * * * for injecting data


















* Their role is and bean tag in the XML configuration file written a role in <property> tag is the same as
* Autowired:
* Role: automatic according to the type of injection. As long as there is only one variable type matching and object types to be injected into the bean container, you can inject success
* If the IOC container does not match any variable type and the type of bean to be injected, the error
* If there are multiple IOC container when the type of match:
* arise location:
* can be on a variable, it can be a method
* details:
* when using annotation injection, set method is not a must
* Qualifier:
* role: Foundations in accordance with the class injection on re-injected by name. It can not be used alone to when the injection members of the class, but in a method of injection parameters may
* Properties:
* value: specifies the bean injected id
* the Resource
* action: direct injection according to the bean id. It can be used independently
* attributes:
* name: used to specify the bean id
* The above three injection can only inject other bean types of data, and the basic types and String types can not be used to achieve the above comment
* In addition, the collection type of injection can only be achieved through XML
*
* Value
* Role: basic types for injection data type String and
* attributes:
* value: a value for the specified data. It may be used in SpEL spring (i.e. the spring el expression)
* SpEL wording: expression $ {}
* range for changing the role of
* effect on their properties and use the scope bean tag functions implemented in the same of
* the scope
* Function: used to specify the scope bean
* properties:
* value: the value of the specified range. Common values: singleton prototype (default Singleton)
* and understand the lifecycle
* their role is in use and bean tag init-method and destroy-methode effect is the same
* PreDestroy
* Function: Used to specify the methods of destruction
* PostConstruct
* Function: Used to specify initialization method
 1 @Service("accountService")
 2 public class AccountServiceImpl implements IAccountService {
 3 
 4 //    @Autowired
 5 //    @Qualifier("accountDao1")
 6     @Resource(name = "accountDao1")
 7     private IAccountDao accountDao2 =null;
 8 
 9     public AccountServiceImpl(){
10 
11         System.out.println("service对象创建了");
12     }
13 
14     public void saveAccount(){
15          accountDao2.saveAccount ();
 16      }
 . 17  
18 is      @PostConstruct
 . 19      public  void the init () {
 20 is          System.out.println ( "initializes the ....." );
 21 is      }
 22 is  
23 is      @PreDestroy
 24      public  void Destory ( ) {
 25          System.out.println ( "target destroyed ....." );
 26      }
 27 }

 



Guess you like

Origin www.cnblogs.com/flypig666/p/11515612.html