Spring-IOC (annotation)

1.1 Project directory

1.2 Code example

Persistence layer (Dao)

CustomerDao

package cn.guardwhy.dao;
/**
 * 客户dao接口
 */
public interface CustomerDao {
    
    
    /**
     * 保存客户操作
     */
    void saveCustomer();
}

CustomerDaoImpl

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;
import org.springframework.stereotype.Component;

/**
 * 客户dao实现类
 */
@Component("customerDao")
public class CustomerDaoImpl implements CustomerDao {
    
    
    /**
     * 保存客户操作
     */
    @Override
    public void saveCustomer() {
    
    
        System.out.println("保存客户操作");
    }
}

Service

CustomerService

package cn.guardwhy.service;
/**
 * 客户service接口
 */
public interface CustomerService {
    
    
    /**
     * 保存客户操作
     */
    void saveCustomer();
}

CustomerServiceImpl

package cn.guardwhy.service.impl;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.service.CustomerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 客户service实现类
 */
@Component("customerService")
public class CustomerServiceImpl implements CustomerService {
    
    
    // 定义客户dao
    @Autowired
    private CustomerDao customerDao;

    /**
     * 保存客户操作
     */
    @Override
    public void saveCustomer() {
    
    
        customerDao.saveCustomer();
    }
}

Configure bean.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">

    <!--配置包扫描注解配置dao/service
        第一步:导入context名称空间和约束
        第二步:
            通过<context:component-scan>标签配置包扫描。spring框架在创建IOC容器的时候,
            会扫描指定的包,和它的子包
    -->
    <context:component-scan base-package="cn.guardwhy"></context:component-scan>
</beans>

Presentation layer (Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.service.CustomerService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    
        // 1.加载spring配置文件,创建spring ioc容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.实例工厂方法实例化对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.保存用户
        customerService.saveCustomer();
    }
}

Results of the

1.3 Common notes

1.3.1 Create Object Annotation

@Component

# Action :
Configure javaBean object. It is equivalent to the bean tag in the xml configuration mode.

#Properties :
value: Give the bean a unique identifying name.

# Details :
1.value attribute can be omitted.
2. By default, the first letter of the name of the class is lowercased as the name of the bean.

Code example

@Controller、@Service、@Repository

Three annotations of @Component evolution

  • @Controller: Generally used in the presentation layer
  • @Service: generally used in the business layer
  • @Repository: generally used for the persistence layer

Code example

1.3.2 Bean scope annotation

@Scope

#Function :
Set the scope of the bean. Equivalent to the scope attribute of the bean tag in the xml configuration mode

#Attribute :
value: specify the scope of the value


#Attribute value : singleton: singleton. Default value
prototype: multiple cases

Code example

1.3.3 Injecting data annotations

@Autowired

#Function :
Inject data according to the type of bean by default

# Properties :
required: whether the target bean must exist in the spring of IOC container (true must exist; false: there may be no; default true)

#Details :

  • In the spring container, if there are multiple bean instance objects of the same type.
  • Inject first according to the type of bean, and then match according to the name of the bean.
  • The injection succeeds on the match; the injection fails if the match fails.

Code example

@Resource

#Function :
Inject data according to the name of the bean by default

#Properties :
name: specify the name of the bean to inject data
type: specify the type of bean to inject data

#Details :
By default, the injected data is matched according to the name of the bean. If the injection fails, inject again according to the bean type.

Code example

@Value

Inject data into java simple type member variables

Code example

1.3.4 Bean life cycle annotation

@PostConstruct @PreDestroy

#@PostConstruct :
Initialization operation, equivalent to the init-method attribute of the bean tag in the xml configuration mode.

#@PreDestroy :
Destroy operation, which is equivalent to the destroy-method attribute of the bean tag in the xml configuration mode.

Code example

1.4 Code example

Persistence layer (Dao)

CustomerDaoImpl

package cn.guardwhy.dao.impl;

import cn.guardwhy.dao.CustomerDao;
import org.springframework.stereotype.Repository;

/**
 * 客户dao实现类
 */
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
    
    
    /**
     * 初始化方法
     */
    public void init(){
    
    
        System.out.println("正在执行初始化操作......");
    }

    /**
     * 销毁方法
     */
    public void destroy(){
    
    
        System.out.println("正在执行销毁操作......");
    }

    /**
     * 无参数构造方法
     */
    public  CustomerDaoImpl(){
    
    
        System.out.println("正在创建客户CustomerDaoImpl对象.");
    }
    /**
     * 保存客户操作
     */
    @Override
    public void saveCustomer() {
    
    
        System.out.println("保存客户操作");
    }
}

Service

CustomerServiceImpl

package cn.guardwhy.service.impl;

import cn.guardwhy.dao.CustomerDao;
import cn.guardwhy.dao.impl.CustomerDaoImpl;
import cn.guardwhy.service.CustomerService;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

 /**
 * 客户service实现类:
 */
@Service("customerService")
@Scope(value = "singleton")
public class CustomerServiceImpl implements CustomerService {
    
    
    // 定义客户dao
    // @Resource(name="customerDao")
    @Resource(type = CustomerDaoImpl.class)
    private CustomerDao customerDao;

    // 2.简单类型成员变量
    @Value("1")
    private int id;
    @Value("小明")
    private String name;

    // 初始化操作
    @PostConstruct
    public void init(){
    
    
        System.out.println("正在执行初始化操作");
    }

    // 销毁操作
    @PreDestroy
    public void destroy(){
    
    
        System.out.println("正在执行销毁操作....");
    }

    /**
     * 保存客户操作
     */
    @Override
    public void saveCustomer() {
    
    
        System.out.println("id="+id+",name="+name);
        customerDao.saveCustomer();
    }
}

Presentation layer (Controller)

CustomerController

package cn.guardwhy.controller;

import cn.guardwhy.service.CustomerService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客户controller
 */
public class CustomerController {
    
    
    public static void main(String[] args) {
    
    
        // 1.加载spring配置文件,创建spring ioc容器
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        // 2.实例工厂方法实例化对象
        CustomerService customerService = (CustomerService) context.getBean("customerService");
        // 3.保存用户
        customerService.saveCustomer();
        // 4.销毁容器
        context.close();
    }
}

Results of the

Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/114993544