zoukankan      html  css  js  c++  java
  • [Java Spring] Spring Annotation Configuration Using XML

    Add context to our application.

    main/resources/applicationContext.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.pluralsight" />
    </beans>

    Using annotation for @Service @Repository @Controller:

    /repository/HibernateCustomerRepositoryImpl.java:

    package com.pluralsight.repository;
    
    import com.pluralsight.model.Customer;
    
    import org.springframework.stereotype.Repository;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Repository("customerRepository")
    public class HibernateCustomerRepositoryImpl implements CustomerRepository {
    
        @Override
        public List<Customer> findAll() {
            List<Customer> customers = new ArrayList<>();
            Customer customer = new Customer();
    
            customer.setFirstname("Byran");
            customer.setLastname("John");
    
            customers.add(customer);
    
            return customers;
        }
    }

    /service/CustomerServiceImpl.java:

    We use @Autowrie for the member of Repository.

    package com.pluralsight.service;
    
    import com.pluralsight.model.Customer;
    import com.pluralsight.repository.CustomerRepository;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service("customerService")
    public class CustomerServiceImpl implements CustomerService {
    
        @Autowired
        private CustomerRepository customerRepository;
    
        @Override
        public List<Customer> findAll() {
            return customerRepository.findAll();
        }
    
    }

    We can also use Setter Injection:

        private CustomerRepository customerRepository;
    
        @Autowired
        public CustomerServiceImpl (CustomerRepository customerRepository) {
            this.customerRepository = customerRepository;
        }

    We can also use Constructor Injection:

        private CustomerRepository customerRepository;
    
        @Autowired
        public CustomerServiceImpl (CustomerRepository customerRepository) {
            this.customerRepository = customerRepository;
        }

    After using Annotations & Autowired, we code looks simple and decoupled

  • 相关阅读:
    IIS 设置IP地址和域名限制
    docker数据持久化
    用户远程登录空闲时间自动断开
    基于python的性能测试工具–locust
    Python代码发送post请求接口测试--转载
    loadrunner监控mysql服务性能
    jmeter for each,循环控制器 遍历结果
    jmeter 如何将上一个请求的结果作为下一个请求的参数——使用正则提取器
    在pycharm中链接MySql数据库并进行操作
    Python—pycharm连接数据库---自创
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9515394.html
Copyright © 2011-2022 走看看