IOC operation bean management

IOC operation Bean Management (XML Automatic Assembly)

1. What is automatic assembly

(1) According to the specified assembly rules (attribute name or attribute type), Spring automatically injects the matching attribute values

2. Demonstrate the automatic assembly process

(1) Inject according to attribute name

<!--Realize automatic assembly, bean Label properties autowire,Configure automatic assembly,
                           autowire Attribute usually has two values;
                             byName Inject according to attribute name ,Injection value bean of id The value is the same as the class attribute name
                             byType Inject according to the attribute type,
    <bean id="emp" class="com.spring5.autowire.Emp" autowire="byName"></bean>
    <bean id="dept" class="com.spring5.autowire.Dept"></bean>

(2) Inject according to attribute type

Note that when using attribute type injection, beans with the same attribute type cannot inject multiple beans at the same time!

<bean id="emp" class="com.spring5.autowire.Emp" autowire="byType">
<bean id="dept" class="com.spring5.autowire.Dept"></bean>

IOC operation Bean Management (external property file)

1. Directly configure database information

(1) Configure driver connection pool

(2) Introducing driver connection pool dependent jar package

2. Import an external property file to configure the database connection pool

(1) Create external property files, properties format files, and database information

(2) Import the external properties property file into the spring configuration file

Introduce context namespace

Then use the tag in the spring configuration file to import the external property file

IOC operation Bean Management (annotation based)

1. What is annotation

(1) Annotation is a special mark in the code. Format: @ annotation name (attribute name = attribute value, attribute name = attribute value...)

(2) Use annotations, which act on classes, methods and attributes

(3) Purpose of using annotation: to simplify xml configuration

2.Spring provides annotations for creating objects in Bean management

(1) @ Component normal

(2) @ Service business logic layer

(3) @ Controller web layer

(4) @ Repository dao layer

The above four annotation functions are the same and can be used to create bean instances

3. Create objects based on annotation

Step 1: introduce dependency

Step 2: start component scanning

<context:component-scan base-package="com.spring5.dao,com.spring5.service"></context:component-scan>

Step 3: create a class and add an object annotation on it

/In the notes value Property value can not be written
//The default value is the class name, with lowercase initial
//UserService -- userService
@Component(value = "userService") //<bean id="userService" class="..">
public class UserService {

    public void add(){
        System.out.println("service add....");

Step 4: open the component scanning details configuration

<!-- Example 1
     use-default-filters="false" Indicates that the default is not used now filter,Self configuration filter
     context:include—filter,Set what to scan
    <context:component-scan base-package="com.spring5" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!--Example 2
    Next, configure all contents of the scanning package
    context:exclude-filter: Set what is not scanned
    <context:component-scan base-package="com.spring5" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

5. Annotation based method is to realize attribute injection

(1) @ AutoWired: auto assemble according to attribute type

The first step is to create the service and dao objects, and add the creation object annotation in the service and dao classes

The second step is to inject dao objects into the service, add dao type attributes to the service class, and use annotations on the attributes

(2) @ Qualifier: inject according to attribute name

The @ Qualifier annotation is used together with @ AutoWired above < / u

//Define dao type properties

//There is no need to add a set method

(3) @ Resource: according to the injection type and injection attribute

(4) @ Value: inject common type attribute

private String name;

6. Full annotation development

(1) Create a configuration class to replace the xml configuration file

(2) Write test class

Keywords: Spring

Added by dacs on Wed, 02 Mar 2022 14:48:09 +0200